Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

MySQL Transaction Logs: Redo and Undo Mechanisms

Tech Nov 29 12

Redo Log

Purpose of Redo Log

Buffer pools help bridge the CPU-disk gap, but checkpoint mechanisms don't trigger with every change. If a crash occurs after transaction commit but before data persistence, redo logs ensure durability by recording all modifications.

Redo Log Characteristics

  • Advantages:
    • Reduces disk write frequency
    • Occupies minimal storage space
  • Features:
    • Sequential disk writes
    • Continuous logging during transaction execution

Redo Log Components

  1. In-Memory Buffer:
    • innodb_log_buffer_size controls size (default 16MB)
  2. Persistent Files:
    • Disk-based log files

Redo Log Workflow

  1. Read original data into memory
  2. Generate redo logs in buffer (recording post-modification values)
  3. On commit: flush buffer to log files (append-only)
  4. Periodically flush modified data to disk

Flush Strategies

innodb_flush_log_at_trx_commit controls commit behavior:

  • 0: No immediate flush (sync every 1s via master thread)
  • 1: Full flush on commmit (default)
  • 2: Write to OS cache only

Log File Configuration

  • innodb_log_group_home_dir: Log file location
  • innodb_log_files_in_group: Number of log files (default 2)
  • innodb_log_file_size: Individual log file size (default 48MB)

Undo Log

Purpose of Undo Log

Ensures atomicity by enabling rollback through:

  1. Recording pre-modification data
  2. Supporting MVCC (Multi-Version Concurrency Control)

Undo Log Storage

  • Organized in rollback segments (128 max)
  • Each segment contains 1024 undo slots
  • Stored in dedicated undo tablespaces

Undo Log Types

  1. Insert Undo Log:
    • Transaction-specific
    • Discardabel after commit
  2. Update Undo Log:
    • Required for MVCC
    • Retained post-commit for potential rollbacks

Lifecycle Example

For an UPDATE operation:

  1. Create undo record with previous values
  2. Modify data in buffer pool
  3. On rollback:
    • Aply undo records in reverse order
    • Restore original state

Key Differences

  • Redo logs: Physical records of page changes
  • Undo logs: Logical records of data modifications

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.