Your database says done. One second later, the power goes out. When the power comes back, the change is still there. This article shows why, with one order and one small note: the write-ahead log.
The video tells the same story in six minutes. This is the written version, with the SQL.
One change, two copies of a page
Alex's pizza arrives, and the rider marks the order delivered:
UPDATE orders SET status = 'delivered' WHERE order_id = 4473;
MySQL stores rows on pages of 16 KB, with a few hundred rows on each one. Say Alex's order is on page 61. MySQL changes that page in memory and marks it dirty. The copy of page 61 on disk still says the order is on the way.
A power cut wipes memory. If nothing else happened, the change would be gone, and the rider would not get paid.
Why not write the whole page?
You'd expect MySQL to write the page to disk before it says done. But the page is 16 KB, and only one word changed. Every page also has its own place on disk, so a busy database would make many small writes all over the disk. Small scattered writes are the slow kind.
A note instead of the page
So MySQL writes a note instead. The note says which page changed, where on that page the change happened, and what the new value is. For a change like this one, that is a few dozen bytes.
Every note goes to the same place: the end of the log. That turns many changes into one long write, and disks handle one long write quickly.
Write-ahead: the order matters
The sequence is the whole idea:
- The note reaches the disk.
- Only then does the database say done.
- The page is written later, in the background.
The log gets to disk ahead of the page, which is where the name comes from. MySQL calls this log the redo log. PostgreSQL calls it the WAL, and its documentation states the rule directly: "changes to data files (where tables and indexes reside) must be written only after those changes have been logged".
Written to a file is not on disk
Here's the part that surprises people. When a program writes to a file, the operating system can keep the data in its own memory and still say okay. A power cut wipes that memory too.
So the database asks for a flush. On Linux that is fsync, and its manual says: "The call blocks until the device reports that the transfer has completed." The database says done only after the flush finishes. Now done means the note is on disk.
That wait is the slowest part of the write. If the power goes out during the wait, the app never hears done, so it cannot tell whether the change reached the disk. It may need to ask again. That is why a retry should carry the same request id as the first try, so the second try can be recognised.
The power comes back
Before MySQL accepts new work, it reads through the log one note at a time, the oldest first. The manual says: "Redo log application is performed during initialization, before accepting any connections."
For each note, MySQL checks the page on disk. Every page records how far through the log its own changes go. If the page does not have the note's change yet, MySQL applies it again. If the page already has it, MySQL skips the note.
Page 61 on disk was out of date, so MySQL applies the change, and Alex's order says delivered, as the database promised. Because a change the page already has is skipped, the repair is safe to run twice.
The checkpoint
The log can't grow forever. Once a page is on disk, MySQL no longer needs the notes for changes on that page. It marks that point in the log, and the mark is called a checkpoint. After a power cut, MySQL only reads the notes after the checkpoint. In the manual's words, "InnoDB scans the log files forward from the checkpoint, applying the logged modifications to the database."
Flush every time, or once a second
You can see the choice on your own server:
SHOW VARIABLES LIKE 'innodb_flush_log_at_trx_commit';
-- innodb_flush_log_at_trx_commit | 1
1 is the default: the log is flushed at every commit. With 0 or 2, InnoDB flushes about once a second instead. Writes are faster that way, but a power cut can lose about the last second of work. If you're storing orders or payments, keep 1.
One idea, three names
MySQL calls it the redo log, Cassandra calls it the commit log, and PostgreSQL calls it the WAL. One difference is worth knowing: by default, Cassandra answers before its commit log is flushed, and flushes it every ten seconds. It leans on the copies on other machines instead.
The law
Done means the note is safely on disk. The page can catch up later.
Go deeper
- The redo log (MySQL 8.4 Reference Manual)
- Optimizing InnoDB transaction management (MySQL 8.4 Reference Manual): the flush at every commit, and the once-a-second settings
- InnoDB recovery and InnoDB checkpoints (MySQL 8.4 Reference Manual)
- fsync(2) (Linux manual)
- Write-ahead logging (PostgreSQL documentation)
- The storage engine (Apache Cassandra documentation): the commit log and its sync modes
Next
This is part 2 of Database Internals, a series on how a database works inside. Part 3 is out: Alex pays for the pizza, the money leaves his account, and the power goes out before it reaches the shop. Database Transactions: How a Payment Is All or Nothing.
Alex, his order, the order id and the page number are an invented example. Has a crash or a power cut ever cost you data? What did the database do when it came back?
Top comments (0)