System design lab · Episode 12

Write-ahead log: surviving a power cut

A bank transfer is two writes: take money from one account, add it to another. If the power goes off between them, the money is gone. A database avoids this by writing the whole change to a log first. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
ABCD
A coin in the air: taken from one account, not yet given to the otherCoin stacks A to D: four accounts on the disk, 10 each coinThe tray at the back: the log, one card per transfer

Controls

Write-ahead log
Time

Want the details?

What is a write-ahead log?

A change in a database often touches several places on the disk: two accounts, a row and its index, a page and the page after it. The disk writes them one by one. If the power fails or the program crashes in between, the data is left half-changed and no longer makes sense.

A write-ahead log (WAL) fixes this with one rule: before changing the data, write a description of the whole change to the end of a log file, and make sure it is on the disk. Appending one record is a single, fast write. After a crash, the database reads the log and finishes every change it finds there (it replays the log). A change that never reached the log was never confirmed to anyone, so it is safe to forget.

How to read the bench
Part on the benchWhat it means in a real system
Box at the leftThe client, sending transfers. Its lamp is lit while a transfer is in progress.
Coin stacks A to DData on the disk: four account balances. One coin is 10.
Coin in the airThe dangerous moment: the debit is on the disk, the credit is not.
Cards in the trayRecords in the log. A pale card's change is already applied to the accounts.
Switch at the frontThe power. Lever down and lamp off: the machine is off and everything is dim.
The numbers
  • 4 accounts with 100 each: 400 in total. Every transfer moves 10.
  • A new transfer starts about every 1.4 s; each step (log, confirm, debit, credit) takes 0.35 s.
  • A power cut lasts 2.5 s, then the machine restarts.
  • Without the log, a cut between debit and credit turns 400 into 390 for good.
  • With the log, the test pulls the plug at hundreds of random moments: the total is 400 every time the power is back, and every confirmed transfer is applied.
In real systems
  • PostgreSQL calls it the WAL; MySQL InnoDB the redo log; SQLite has a WAL mode and an older "rollback journal". The idea is the same.
  • fsync. The log record only counts once the operating system confirms it is on the physical disk, not just in a memory buffer. Skipping this makes databases fast and unsafe.
  • Checkpoints write the changed pages to disk and let the database remove old log records, so restarts stay quick.
  • Replication often sends the same log to other servers (the replication lab): the log is the single story of what happened.
  • Event sourcing takes the idea further: the log of events is the main data, and the tables are built from it.
Words to know
Atomic
All or nothing: a transfer either happens completely or not at all.
Durable
Once confirmed, a change survives a crash.
Write-ahead log (WAL)
A file where every change is written before the data itself is changed.
Replay (redo)
Reading the log after a crash and finishing the changes it lists.
Checkpoint
A point after which old log records are no longer needed.