Dev Learning Lab

The labs on this site need JavaScript to run. The same content is written out below.

Learn › Database › Storage and recovery

Undo and redo logs

Why does the data survive even if the power goes out right after a commit?

Why it exists — If changes were written straight to the data files, every commit would have to modify several places on disk, which is slow, and if the server went down in the middle of writing, there would be no way to know how far the changes had been applied. The undo log and redo log record the value before the change and the content of the change separately, so that writing to the data files can be postponed, while after a failure, committed changes can be filled in again and uncommitted changes can be rolled back.

Where it's used today — In MySQL InnoDB, rollback and the reading of earlier versions in MVCC use the undo log, and recovery after the server goes down uses both logs, and developers do not need to record either of them separately. In operation you meet them in settings such as innodb_flush_log_at_trx_commit, which decide whether the redo log is reliably written to disk at every commit. A value other than 1 makes writes faster, but some recent commits can be lost in a failure.

When a database changes data, it does not modify the data file right away. It changes the data in memory first and leaves two records: one needed to undo the change (undo) and one needed to apply it again (redo).

Checked against real execution · All four cases were run on MySQL 8.4.11 in a dedicated container. The failure was created by forcibly terminating mysqld (SIGKILL), and the value in the data file and the recovery log were checked directly

Concepts in this lab

Undo log and redo log
An undo log records data as it was before a change. It is used to roll back a transaction, and in InnoDB it is also used to show past versions to other transactions (MVCC). A redo log records the changes themselves. If a failure occurs before committed changes are fully written to the data files, the changes are applied again at restart to recover them.
ACID
The four properties that a transaction must guarantee. Atomicity means everything succeeds or everything fails. Consistency means the data satisfies the defined rules before and after the transaction. Isolation means transactions that run at the same time do not interfere with each other. Durability means committed results are kept even if a failure occurs.
MVCC (multiversion concurrency control)
Why it exists — If you keep isolation with locks alone, while one transaction modifies a row, a transaction that wants to read that row must also wait until it finishes. MVCC removes this waiting by keeping the version from before the change and showing that version to the reading side.
Where it's used today — In MySQL InnoDB's default isolation level, an ordinary SELECT run without FOR UPDATE reads this way, so saving an order is not blocked even while a long statistics query is running. When you modify data based on a value you read, you protect it separately with FOR UPDATE or @Version.
A method that keeps earlier versions of data when the data changes, so that a reading transaction can see the version that matches the time of its snapshot. The snapshot time depends on the isolation level: in MySQL, REPEATABLE READ takes it at the first read of the transaction, and READ COMMITTED takes a new one each time a statement runs. Reads do not wait for writes and writes do not wait for reads, so concurrent performance is high. MySQL InnoDB, PostgreSQL, and Oracle all use this method.
Transaction
A way to group several data changes into a single unit so that either all of them are applied (commit) or all of them are canceled (rollback). In a bank transfer, it prevents an intermediate state where only one of the withdrawal and the deposit is applied. It is the most basic means a database has for keeping data consistent.

Try these

Running with the default settings

  1. The balance of account 1 is 1000. This value is in the data file on disk, and the same value is also loaded in memory (the buffer pool).
  2. The UPDATE starts. First, the value before the change, 1000, is recorded in the undo log. It is used when the change has to be undone, and when another transaction has to read the value from before the change, an earlier version (MVCC).
  3. The data held in memory is changed to 500. The data file on disk is still 1000. Changing the data file right away every time would require writes to many places on the disk, which is slow, so the changes are written later in a batch.
  4. What was changed, and how, is recorded as redo log. It is first written to the log buffer in memory. It is written to the redo log file on disk at commit at the latest, and background threads can also write the contents of the log buffer to disk before that. A log only needs to be appended to the end of a file, so this is much faster than changing the data file.
  5. COMMIT: The redo log is written to disk for certain (fsync). Only after this is done does the server reply "commit succeeded" to the user. Even at this point the data file is still 1000. What is written to disk at commit time is only the log.
  6. The server went down right after the commit reply, before the changes were written to the data file. The 500 in memory is lost, and the data file still holds 1000. The user has already received "success".
  7. The server starts again. Recovery reads the redo log on disk from the last checkpoint.
  8. The redo log has "account 1 set to 500" and the record that it committed. This change is applied again. The balance is recovered as 500. No committed data was lost.

Result of this run

Code

START TRANSACTION;
UPDATE account SET balance = 500 WHERE id = 1;   -- it was 1000
COMMIT;
-- (the server goes down right after the commit reply)

Assumptions of this model

This is a simplified version of the structure of MySQL InnoDB. Details are left out, such as the fact that changes to the undo log are themselves protected by the redo log, and the several stages of writing to disk (such as the operating system cache and the doublewrite buffer). I ran the four cases on MySQL 8.4.11 (a dedicated container with the default settings: innodb_flush_log_at_trx_commit=1 and the binary log enabled) and compared the results. "The server stops" means that the mysqld process was forcibly terminated (SIGKILL), not that the power was cut. So I could not directly confirm what fsync protects in a power failure, where even the contents that were only in the operating system cache are lost; I confirmed only that an fsync of the redo log happens at commit and that right after the commit response the amount of redo log not yet written to disk is 0 bytes. To see what is in the data file, I copied out the file of the account table (account.ibd) and read the 4 bytes that store the balance. When the server was terminated right after the commit response, the data file had 1000, the error log of the restarted server recorded that the redo log was read from the checkpoint and applied again (with 0 transactions rolled back), and the balance was 500. When it was terminated before the commit, the log recorded that the redo log was applied again and then 1 row of 1 transaction was rolled back, and the balance was 1000. With ROLLBACK, the number of undo log entries went from 1 to 0 and the value returned to 1000. I also confirmed that after the UPDATE and before the commit, another session reads the previous value 1000 (MVCC). With a normal commit, there were 0 writes to the data file and 1 dirty page at the time of the commit, and the data file was written later (over several runs it was often written by itself within 2 seconds, and once it was still not written after 60 seconds, so I forced the write). The runs also showed things that differ from the simple picture on the screen. The redo log of an uncommitted change was also written to disk by background threads soon afterward (between a few milliseconds and 0.2 seconds, depending on the run). An uncommitted change was sometimes written to the data file first (after waiting 0.2 to 0.6 seconds after the UPDATE, the data file had 500). Even when the server was terminated in that state, recovery rolled the change back with the undo log and the balance was 1000. Conversely, in one run the server was terminated right after the commit response, but the data file had already been written with 500 in that interval (about 0.2 seconds); in that case I ran it again until the situation on the screen occurred (the data file still has 1000). The explanation that appending to a log is faster than changing the data file was not confirmed by measuring speed.

Previous lab: Deadlocks in the database · Next lab: Buffer pool and pages

Concepts in this lab

What this lab shows

Settings

Code


      
    

Diagram

Run comparison

Try these