Write the change to a log first, so a crash never loses it. The trick behind durability and replication.
A write-ahead log (WAL) is a rule databases follow: write down what you are about to do, in a safe log, before you do it. If the machine crashes, the log tells the database how to finish or undo the work.
A database keeps its data in pages on disk, and works on a copy of the hot pages in memory because memory is far faster. When you change a row, the page in memory changes first. Saving every changed page to disk straight away would be slow, because the pages are scattered all over the disk. But if the database waits, a crash would lose the changes that were only in memory, even ones the user was already told were saved. That would break the D (Durability) in ACID.
The fix is the write-ahead log. Before a change goes anywhere else, a short record describing it ("transaction 7: account B, 300 → 400") is appended to the log. A commit is not complete until its log records have been flushed to disk (the operating system call is often called fsync). Only then is the user told "OK". The data pages themselves can be written later, in the background, at a convenient time.
After a crash, the database starts and reads the log. For every transaction that has a COMMIT record, it makes sure the change is in the data pages (redo). For every transaction without one, it makes sure the change is not (undo). The rule that keeps this safe: a log record must reach disk before the data page it describes.
Each record has a position (often called a log sequence number, LSN) that only goes up, which gives a total order to every change. The main kinds of record are:
LSN 101 BEGIN T1 LSN 102 UPDATE T1 A: 500 -> 400 LSN 103 UPDATE T1 B: 300 -> 400 LSN 104 COMMIT T1 <- the promise is made here LSN 105 UPDATE T2 C: 10 -> 20 LSN 106 CHECKPOINT <- pages up to here are on disk LSN 107 (crash) <- T2 never committed
| Case after a crash | What the log says | What recovery does |
|---|---|---|
| Committed, but pages not yet written | COMMIT is in the log | Redo: apply the change to the page |
| Not committed, but the page was already saved | No COMMIT in the log | Undo: put the old value back |
| Committed and already on disk | Before the last checkpoint | Nothing to do |
| Not committed and not on disk | No COMMIT | Nothing to do, forget it |
Flushing to a physical disk is the slow part of a commit. Group commit lets many transactions that commit at nearly the same moment share one flush, which gives much better throughput. Some databases also allow a relaxed setting that flushes the log every moment instead of every commit. That is faster, but a crash can lose the last moment of commits, so it is a conscious trade of durability for speed.
A simplified teaching model with made-up values. With the log off, the page of A reaching disk before the page of B is just one possible outcome of a crash. Real databases write many pages, in any order.
| Term | Meaning in easy words |
|---|---|
| Write-ahead log (WAL) | A log of changes that is saved to disk before the data pages are changed |
| Log record | One entry describing a change, a commit or a checkpoint |
| LSN | Log sequence number: a number that only goes up and gives every record its place in order |
| fsync | A request to the operating system to really write the data to the physical disk |
| Commit record | The log entry that makes a transaction official |
| Redo | Replay a committed change that is missing from the data pages |
| Undo | Reverse a change from a transaction that never committed |
| Checkpoint | A moment when the changed pages are saved, so recovery can start from there |
| Crash recovery | The steps after a restart that use the log to restore a consistent state |
| Group commit | Several transactions sharing one log flush to raise throughput |
| Log shipping | Sending the log to another machine so it can replay it |
| Choice | We gain | We lose |
|---|---|---|
| Flush on every commit | No committed data is lost | Slower commits |
| Flush every moment | Higher throughput | Last moment of commits can be lost in a crash |
| Frequent checkpoints | Fast recovery | More background writing |
| Rare checkpoints | Less writing | Longer recovery, bigger log |
| Keep the log for a long time | Point-in-time recovery, replicas can catch up | More disk space |
| Group commit | Much better throughput | A tiny extra wait for each commit |