The labs on this site need JavaScript to run. The same content is written out below.
Learn › Database › Storage and recovery
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
START TRANSACTION; UPDATE account SET balance = 500 WHERE id = 1; -- it was 1000 COMMIT; -- (the server goes down right after the commit reply)
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
Struck-through text was removed from the first version of the code; highlighted text was added.