Databases 19 min read

How InnoDB Redo Logs Safeguard MySQL: Generation, Buffering, and Flushing Explained

This article explains how MySQL 8.0's InnoDB generates Redo logs, groups them into mini‑transactions, writes them concurrently to the log buffer, and uses log_writer and log_flusher threads together with LSN, slots, and recent_written structures to ensure durable, ordered persistence.

dbaplus Community
dbaplus Community
dbaplus Community
How InnoDB Redo Logs Safeguard MySQL: Generation, Buffering, and Flushing Explained

Overview

InnoDB must generate Redo logs for every DML and DDL operation to guarantee data durability. Although creating and writing these logs consumes extra resources, they become essential when a crash occurs.

Redo Log Generation

Each simple INSERT produces two Redo log records: one storing the auto‑increment column's maximum value and another storing the row's field values. Logs are temporarily held in an m_log linked list of 512‑byte blocks within a mini‑transaction (mtr), which groups related operations.

Writing to the Log Buffer

In MySQL 8.0, multiple threads can write Redo logs to the log buffer in parallel. Each thread reserves a contiguous LSN range ( start_sn to end_sn) calculated atomically, then converts it to an LSN range ( start_lsn to end_lsn) that maps to a slot in recent_written.m_links. The slot index is start_lsn % SLOT_COUNT (default 1,048,576). After a thread finishes its mtr, it writes end_lsn into the corresponding slot, allowing the log_writer to identify continuous log regions.

m_log block structure
m_log block structure

Writing to the Log File

The log_writer thread copies Redo logs from the log buffer to the log file buffer, but only for continuous LSN intervals. It uses recent_written.m_tail to track the highest LSN that forms a continuous sequence. When a slot contains zero or an older end_lsn, the continuous region ends, and the writer flushes up to m_tail and updates log_sys.write_lsn. After flushing, it notifies waiting user threads via log.write_events.

log_writer finding continuous LSN range
log_writer finding continuous LSN range

Log File Flushing

The log_flusher thread periodically (≈1 s) writes the log file buffer to disk. It compares log_sys.write_lsn with log_sys.flushed_to_disk_lsn; if the former is larger, flushing is required. Flushing can also be triggered immediately by transaction commit events (e.g., innodb_flush_log_at_trx_commit=1), which signal log.flusher_event. After a flush, the thread updates flushed_to_disk_lsn and notifies waiting threads via log.flush_events.

log_flusher flushing process
log_flusher flushing process

Summary

Redo logs are written in groups (mtr) to the log buffer without locks, using pre‑allocated LSN ranges. The log_writer ensures only continuous log segments are persisted to the log file, while the log_flusher guarantees those segments reach durable storage. Together, these mechanisms provide crash‑safe recovery for MySQL.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

concurrencyInnoDBmysqlDatabase Recoveryredo logLog Buffer
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.