MySQL Redo Log

In the InnoDB storage engine, Redo Logs are the backbone of durability and crash‑recovery. They implement a write‑ahead logging (WAL) strategy: before any data page is flushed to the physical storage, the modification is first recorded in the log. This approach decouples the cost of writing user data from the cost of ensuring that the change can survive a power loss or an unexpected shutdown.

How Redo Logging Works

When a client issues an INSERT, UPDATE, or DELETE, InnoDB does not immediately write the altered page to disk. Instead, it:

  1. Modifies the page in the Buffer Pool – the in‑memory cache of data pages.
  2. Creates a physical description of the change (the redo record) and places it into the Log Buffer, a memory area dedicated to holding pending log entries.

The Log Buffer is flushed to the on‑disk redo files under two conditions:

  • The buffer reaches the size defined by innodb_log_buffer_size.
  • A transaction commits, forcing the associated log records to be persisted.

Only after the redo records have been safely written does InnoDB consider the transaction durable. The actual data pages are written later, during a checkpoint operation.

Redo Log File Layout and Circular Writing

InnoDB stores redo information in a set of fixed‑size files, typically named ib_logfile0, ib_logfile1, etc. The engine treats these files as a circular buffer:

Concept Description
Write pointer Marks the position where the next log record will be appended.
Checkpoint pointer Points to the oldest log record whose changes have already been flushed to the data files.

As the write pointer advances, it eventually catches up to the checkpoint pointer. When this happens, InnoDB forces dirty pages from the Buffer Pool to be flushed, moving the checkpoint forward and freeing space for new log entries. If the write speed outpaces the checkpoint advancement, the engine will pause log writes until enough space becomes available, preventing overwriting un‑checkpointed data.

Key Configuration Parameters

Tuning the redo log subsystem can have a dramatic impact on write throughput and recovery time.

  • innodb_log_file_size – Size of each redo file. Larger files reduce checkpoint frequency, lowering I/O pressure, but they also increase the amount of work required during crash recovery.
  • innodb_log_files_in_group – Number of redo files in the circular set. Two or four files are common choices; more files can smooth out write spikes but consume additional disk space.
  • innodb_log_buffer_size – Capacity of the in‑memory Log Buffer. In high‑concurrency environments, a bigger buffer reduces the number of flushes, improving overall latency.

When adjusting these values, remember that the total redo log size (innodb_log_file_size * innodb_log_files_in_group) should be roughly 1–2 × the size of the Buffer Pool for write‑intensive workloads. This rule of thumb balances checkpoint overhead against recovery speed.

Crash Recovery Process

If MySQL terminates abruptly, InnoDB initiates a recovery routine on the next startup:

  1. Locate the latest checkpoint – The engine scans the redo files to find the most recent checkpoint position.
  2. Replay redo records – Starting from the checkpoint, InnoDB reads each log entry and reapplies the corresponding page modifications to the in‑memory copies of the data pages.
  3. Flush the repaired pages – Once all redo entries have been processed, the updated pages are written to the data files, guaranteeing persistence.
  4. Rollback uncommitted transactions – Any transaction that did not reach the commit point before the crash is undone using the Undo Log, restoring atomicity.

The entire sequence is automated and typically completes within seconds for modest log sizes, but it can stretch to minutes when the redo log is very large.

Best‑Practice Recommendations

  • Combine redo logs with regular backups – Redo logs protect against sudden crashes, but they do not replace logical backups. Schedule full backups (e.g., using mysqldump or mysqlpump) and supplement them with binary logs for point‑in‑time recovery.
  • Monitor checkpoint activity – The command SHOW ENGINE INNODB STATUS reports the current checkpoint position and the amount of log space used. Set up alerts if the distance between the write pointer and checkpoint shrinks below a safe threshold.
  • Adjust log sizes based on workload – For write‑heavy applications, increase innodb_log_file_size so that checkpoints occur less frequently. Conversely, for environments where fast crash recovery is critical, keep the total log size modest.
  • Avoid frequent log file recreation – Changing innodb_log_file_size requires shutting down MySQL, removing the old log files, and restarting. Plan size changes during maintenance windows to minimize downtime.
  • Tune the Log Buffer – In systems with many concurrent transactions, a larger innodb_log_buffer_size reduces the number of flushes to disk, decreasing latency and improving overall throughput.

Summary

Redo logs are the invisible workhorse that lets InnoDB deliver ACID guarantees while maintaining high write performance. By recording physical page changes before they reach the data files, the engine can safely defer expensive disk I/O, recover from crashes by replaying the log, and keep the Buffer Pool clean through checkpointing. Proper configuration of innodb_log_file_size, innodb_log_files_in_group, and innodb_log_buffer_size, coupled with vigilant monitoring and regular backups, equips database administrators to extract the maximum performance from MySQL while safeguarding data integrity under even the most adverse conditions.