Scrivio Interview library
InternalsFoundations7 min read · updated 12 November 2026

What is the difference between InnoDB's redo log and undo log? What does innodb_flush_log_at_trx_commit control, and what are the risks of setting it to 2 or 0?

The redo log records the physical changes made to pages so that InnoDB can replay committed work after a crash; it is written sequentially. The undo log records how to reverse each change, and InnoDB uses it to roll transactions back and to rebuild older row versions for consistent reads. innodb_flush_log_at_trx_commit decides when the redo log reaches disk: 1 writes and fsyncs at every commit, 2 writes to the operating system at commit but fsyncs about once a second, and 0 does both about once a second, so 2 and 0 can lose roughly a second of committed transactions.

Why this question is asked

Every Java developer who writes to MySQL relies on these two logs, usually without knowing it. They are the reason a commit survives a power cut, the reason a rollback works, and the reason a long report sees a stable picture of the data while others write. Interviewers ask this question because it shows whether we understand what commit really promises and what we trade away when someone tunes a setting for speed. In this answer, we will recap why InnoDB needs logs at all, look at the redo log and the undo log in turn, and then see what innodb_flush_log_at_trx_commit controls, what 2 and 0 risk, and how sync_binlog fits in.

1Why InnoDB Needs Logs at All

InnoDB keeps table and index data in 16 KB pages. When we update a row, InnoDB changes the page in the buffer pool, its cache in memory, and marks the page dirty. It does not write the page to its data file straight away. Writing whole pages at random places on disk for every commit would be slow, and a crash in the middle of writing a page could leave it half old and half new.

So InnoDB follows the write-ahead logging rule: before a change is considered durable, a small description of it is written to a log, and the dirty pages themselves are written later, in the background. Two logs serve two different purposes. The redo log lets InnoDB move forward after a crash, and the undo log lets it move backwards.

2The Redo Log: Replaying Committed Work

A redo record says, in effect, change these bytes on page 5 of tablespace 12 to these values. Records are first placed in the log buffer in memory and then appended to the redo log files on disk. Because they are only ever appended, writing them is sequential I/O, which even a slow disk handles quickly.

If mysqld crashes, the data files may be missing changes that only existed in dirty pages. At startup, InnoDB reads the redo log from the last checkpoint, the point up to which all changes are known to be in the data files, and reapplies every change after it. Committed transactions come back; transactions that had not committed are then rolled back using the undo log. The redo log is reused in a circle, so InnoDB must flush dirty pages to advance the checkpoint before old redo space can be overwritten. Since MySQL 8.0.30 its size is set with innodb_redo_log_capacity.

3The Undo Log: Going Back to an Older Version

Before InnoDB changes a row, it writes the old values to an undo record and links the new row version to it through a hidden roll pointer. That link serves two purposes. If the transaction rolls back, InnoDB walks its undo records and restores the old values. And if another transaction started earlier and must not see the change yet, InnoDB follows the pointer to rebuild the version that transaction is allowed to see. This is MVCC, multi-version concurrency control.

SQL
-- Session A -- Session B
START TRANSACTION;
SELECT balance FROM account WHERE id = 1; -- 100
UPDATE account SET balance = 70 WHERE id = 1;
COMMIT;
SELECT balance FROM account WHERE id = 1; -- 100: rebuilt from the undo log
COMMIT;
SELECT balance FROM account WHERE id = 1; -- 70
Redo log and undo log side by side
Redo logUndo log
Recordsnew bytes for a pageold values of a row
Used forcrash recovery, moving forwardrollback and consistent reads
Writtensequentially, appendedinto undo tablespaces, like data
Removed whenthe checkpoint passes itpurge finds no reader needs it

Notice that session A keeps seeing 100 after B has committed 70. The page holds the new value, and A's second read is served by walking back through undo. Undo records can be removed only when no open transaction might need them, a job done by the purge threads.

One detail connects the two logs: undo records live in pages too, and changes to those pages are protected by redo. So after a crash InnoDB first replays redo, which also restores the undo, and then uses that undo to roll back unfinished transactions.

4What innodb_flush_log_at_trx_commit Controls

Getting a redo record onto disk takes two steps. A write() call copies the log buffer into the operating system's page cache, which is fast but still only memory. An fsync() call then asks the operating system to put that data on the physical device, which is slower and is what makes it survive a power cut. The setting decides when each step happens.

Java
SHOW VARIABLES LIKE 'innodb_flush_log_at_trx_commit'; -- 1 (the default)
SET GLOBAL innodb_flush_log_at_trx_commit = 2; -- dynamic, no restart
SHOW VARIABLES LIKE 'innodb_flush_log_at_timeout'; -- 1: seconds between background flushes
The three values and what each survives
ValueAt commitBackgroundCan lose commits on
1 (default)write and fsyncnothing, if the disk honours fsync
2write to OS cachefsync about every secondOS crash or power loss
0nothingwrite and fsync about every secondmysqld crash, OS crash, power loss

With 1, every commit waits for both write and fsync, so a commit reported to the application is on disk. InnoDB groups the fsyncs of transactions committing together, called group commit, so the cost is shared under load.

5The Risks of Setting It to 2 or 0

With 2, a crash of mysqld alone loses nothing, because the redo records are already in the operating system's cache and the kernel writes them out. But if the whole host crashes or loses power, up to about a second of commits disappears. At 2,000 commits per second, that is up to 2,000 transactions the application was told were committed. With 0, even a mysqld crash loses that window, because the records may still be in InnoDB's own log buffer.

There is a second, quieter risk. If the primary loses its last second but replicas already received those transactions through the binlog, the primary and its replicas no longer agree, and a failover can resurrect or drop data in confusing ways. So 2 or 0 belongs only where losing a second is acceptable: a cache that can be rebuilt, a staging database, or a bulk load we would restart anyway.

6How sync_binlog Interacts With It

The binary log, written by the server layer for replication and point-in-time recovery, is a separate log with its own setting. sync_binlog=1 fsyncs the binlog at every commit (in groups), 0 leaves it to the operating system, and N fsyncs every N commit groups. MySQL keeps the binlog and InnoDB consistent with an internal two-phase commit: InnoDB prepares the transaction, the binlog is written, then InnoDB commits. Crash recovery uses the binlog to decide whether each prepared transaction is committed or rolled back.

That is why the safe setting is both values at 1, often called double one. If InnoDB is at 1 but sync_binlog is 0, a crash can leave transactions in InnoDB that never reached the binlog, so replicas and point-in-time recovery silently miss them. The default for both has been 1 since MySQL 5.7.7, and that is what we keep for any data that matters.

Step throughA commit with innodb_flush_log_at_trx_commit=1 and sync_binlog=1
  1. InnoDB prepareredo for the prepare written and fsynced
  2. Write the binlog eventsfsync the binlog, in a group
  3. InnoDB commitcommit record goes to the redo log
  4. Return OK to the clientthe transaction survives a power cut

4 steps in order. Play them, or step through them one at a time.

7Follow-up questions

How does sync_binlog interact with innodb_flush_log_at_trx_commit?

They protect two different logs that are tied together by an internal two-phase commit. With both at 1, a transaction acknowledged to the client is in the redo log and in the binlog, so crash recovery and replicas agree. Relaxing either one opens a window: with sync_binlog=0, the binlog can lose transactions InnoDB kept, and with the InnoDB setting at 2 or 0, InnoDB can lose transactions the binlog or a replica already has.

Does setting it to 2 make reads faster?

No. It only changes how long a commit waits for the disk, so it helps write-heavy workloads with many small transactions. Reads are served from the buffer pool or data files and are not affected.

8Conclusion

In this answer, we saw that the redo log records new page changes sequentially so InnoDB can replay committed work after a crash, while the undo log records old values for rollback and for MVCC reads. innodb_flush_log_at_trx_commit decides when redo is written and fsynced: 1 at every commit, 2 written at commit but fsynced about once a second, and 0 both about once a second, so 2 risks losing a second on a host crash and 0 even on a mysqld crash. For full durability we keep it at 1 together with sync_binlog=1.

What to say in the interviewAbout 80 seconds aloud

Open with

The redo log and the undo log solve opposite problems. The redo log records the physical changes to pages, appended sequentially, so after a crash InnoDB can replay everything after the last checkpoint and bring committed transactions back even if the dirty pages were never written.

Then, if they want more

The undo log records the old values of each row change. InnoDB uses it to roll back a transaction and, through MVCC, to rebuild the older version of a row for a transaction whose snapshot should not see a newer change. innodb_flush_log_at_trx_commit controls when redo reaches disk.

The default 1 writes and fsyncs at every commit, using group commit to share the cost, so an acknowledged commit is durable. With 2, the commit writes into the operating system cache and the fsync happens about once a second, so a mysqld crash loses nothing but a host crash or power loss can lose about a second of commits.

With 0, both happen about once a second, so even a mysqld crash can lose that second. I would only relax it for data I can rebuild. I would also keep sync_binlog at 1, because the binlog and InnoDB are tied by a two-phase commit and relaxing either one lets the primary and its replicas disagree.

About this answer

Last updated
12 November 2026
Versions it names
MySQL 5.7, MySQL 8.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
MySQL reference manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between InnoDB's redo log and undo log? What does...": https://scrivio-interview-library.netlify.app/databases/mysql/220fa1698f/