preface

Redo logs and binlogs are both redo logs and binlogs.

  • What is a redo log?
  • You must know the binlog

Today we’ll talk about how InnoDB keeps redo logs and binlogs logically consistent.

Two-phase commit

The redo log gives InnoDB storage engine crash recovery.

Binlog (archive log) ensures data consistency in MySQL cluster architecture.

Although they both fall under the category of persistence guarantees, the emphasis is different.

Redo log and binlog are recorded during the execution of an update statement. Redo log is written continuously during the execution of a transaction, while binlog is written only when a transaction is committed. Therefore, redo log and binlog write at different times.

Anyway, what happens when there is a logical inconsistency between redo logs and binlogs?

SQL > update T set c=1 where id=2;

What happens if an exception occurs during binlog writing after redo log writing?

There is no modification record in the binlog. Therefore, when the database is restored using the binlog, the c value of the restored line is 0, whereas the c value of the original line is 1 because of the redo log.

To solve the logical consistency problem between two logs, InnoDB storage engine uses a two-phase commit scheme.

The principle is simple: Split redo log writing into two steps prepare and commit.

After the two-phase commit is used, an exception occurs during the writing of the binlog. The transaction is not affected because when MySQL restores data from the redo log, the redo log is in the prepare state and there is no corresponding binlog.

If an exception occurs during the redo log commit phase, will the transaction be rolled back?

MySQL does not roll back the transaction. The redo log executes the logic outlined above. Although the redo log is in the prepare state, the binlog is found by the transaction ID, so MySQL considers it complete and commits the transaction recovery data.

Standing on the shoulders of giants

  • MySQL tutorial 45
  • MySQL From Scratch to become a Real Optimization expert
  • MySQL Innodb Storage Engine

MySQL good article recommended

  • Over the years CURD, have you learned about the architecture design of MySQL?
  • MySQL InnoDB memory component
  • What is a redo log?
  • No, no, nobody knows binlog?

White piao welfare

High quality welfare for fans, low-key receive ha ~

Click here to get the full tutorial from Zero to Java Employment, full tutorial for architect