Databases 4 min read

Ensuring Redo Log and Binlog Consistency in InnoDB with Two‑Phase Commit

This article explains how InnoDB’s two‑phase commit mechanism keeps the redo log and binlog logically consistent, illustrating failure scenarios, the impact on data recovery, and how the prepare‑commit split prevents inconsistencies in MySQL’s storage engine.

JavaEdge
JavaEdge
JavaEdge
Ensuring Redo Log and Binlog Consistency in InnoDB with Two‑Phase Commit

Introduction

The article builds on previous discussions of MySQL redo log and binlog, focusing on how the InnoDB storage engine guarantees logical consistency between these two logs.

Redo Log vs Binlog

Redo log provides crash‑recovery capability for InnoDB, while binlog ensures data consistency across MySQL replication clusters. Redo log records are written continuously during a transaction, whereas binlog entries are written only at commit time, leading to different write timings.

Inconsistency Example

Consider an UPDATE T SET c=1 WHERE id=2 statement. If the redo log is flushed but an exception occurs before the binlog entry is written, the binlog will lack the corresponding modification. During recovery, the binlog‑based restore will miss this update, leaving column c as 0, while redo‑log recovery will apply the change, resulting in a value of 1 and data inconsistency.

Two‑Phase Commit Solution

InnoDB resolves this by using a two‑phase commit. The redo‑log write is split into a prepare step and a commit step. This ensures that both redo log and binlog reach a consistent state before the transaction is considered committed.

Failure Scenarios and Recovery

If an exception occurs while writing the binlog after the redo log is in the prepare state, MySQL detects the missing binlog entry during recovery and rolls back the transaction, preserving consistency.

Additional Edge Cases

When an exception occurs during the redo‑log commit phase, the transaction is not rolled back. MySQL can locate the corresponding binlog entry via the transaction ID, treats the transaction as complete, and proceeds with data recovery, ensuring the transaction is committed.

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.

InnoDBmysqlBinlogtwo-phase commitredo logdatabase-consistency
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

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.