Best on a larger screen — the animation and controls have more room to breathe.
Inputs

WAL & Crash Recovery

Write-ahead logging (WAL) makes database changes recoverable by persisting their log records before the affected data pages. A transaction can commit while its data files still hold old values. After a crash, recovery uses the surviving log to reconstruct committed changes and, in the STEAL / NO-FORCE model here, remove uncommitted ones.

A page on disk is not a certificate of commitment. It may contain a change whose transaction never finished. Conversely, an old page does not prove that the latest committed change was lost. You need the log to tell those two stories apart.

How WAL Works

The log buffer and buffer pages live in volatile memory. Data files and the flushed log live in durable storage. Appending a record to the log buffer does not make it survive a crash. Each record has a log sequence number, or LSN, that orders the records. Flushing through LSN 3 persists the whole prefix through 3, not just one selected transaction.

Two ordering rules carry the guarantee:

  1. Before a modified page reaches its data file, the log records describing that modification must be durable. The before-image preserves what UNDO needs; the after-image supplies REDO.
  2. Before the database acknowledges a successful commit, the transaction's COMMIT record and its preceding update records must be durable. Its data pages need not have been flushed.

The buffer policies explain why recovery has work to do:

PolicyPermissionRecovery consequence
STEALWrite an uncommitted buffer page to its data fileUncommitted effects may need UNDO
NO-FORCECommit without flushing every modified data pageCommitted effects may need REDO

The teaching recovery scans the complete surviving log. First it finds transactions with a durable COMMIT. It then replays every UPDATE in forward order, including updates from losing transactions. Finally it walks the losing transactions' UPDATEs backward and restores their before-images. A loser is a transaction with updates but no durable COMMIT in this model.

REDO here assigns a recorded value. Replaying B = 80 when B is already 80 keeps it at 80; it does not add the original increment again. The resulting pages live in the recovery workspace until separate writes persist them.

WAL & Crash Recovery Step by Step

The default starts with two independent pages, A=100 and B=50. T1 changes A; T2 changes B. These are unitless example values, not a transfer between accounts.

LSNTransactionRecord
1T1UPDATE A: 100 → 120
2T1COMMIT
3T2UPDATE B: 50 → 80
4T2UPDATE B: 80 → 90
  1. Record T1's update. Append UPDATE 1 in memory, then change buffer A to 120. Flush UPDATE 1 to durable storage. The A data file still contains 100.
  2. Commit T1. Append COMMIT 2, flush it, then acknowledge success. T1 is durable although its A page has not reached the data file. This is NO-FORCE in action.
  3. Write T2's first value. Append UPDATE 3, set buffer B to 80, and flush the log through 3. The page-flush check now passes. Write B=80 to its data file while T2 remains uncommitted.
  4. Write T2's second value. Append UPDATE 4, set buffer B to 90, and flush the log through 4. Leave that newer page in memory. T2 has no COMMIT record.
  5. Crash. Memory disappears. Disk contains A=100 and B=80; all four log records survive. Load those disk pages into the recovery workspace. Rebuild transaction state from the log: T1 has COMMIT 2, while T2 has no COMMIT.
  6. Repeat history. REDO 1 restores A=120. REDO 3 writes B=80 again, without changing its value. REDO 4 writes B=90 even though T2 will be undone.
  7. Undo the loser backward. UNDO 4 restores B=80. UNDO 3 then restores B=50. Reversing this order would incorrectly leave B=80. T1's A=120 remains.
  8. Persist the recovered pages. Write A=120 and B=50 to their data files, one page at a time. Only after both writes have landed is the pictured recovery complete.

The persistence boundary stays fixed while copies cross it. At the crash, only the memory side disappears. During recovery the log cursor moves forward for REDO and backward for UNDO; the data-file cards stay unchanged until the final page writes.

Why Can a Commit Survive Without an Acknowledgment?

Compare Commit buffered with Commit durable. Both have UPDATE 1 on disk, A=120 in memory, and COMMIT 2 appended to the log buffer. The first crashes there and recovers A=100. The second adds one log flush, crashes before the acknowledgment, and recovers A=120.

The missing response leaves the client uncertain; it does not prove the database aborted. Conversely, sending success before flushing COMMIT can promise a change that recovery cannot keep. The position of one storage event changes the outcome.

Full-log recovery in this lesson costs O(L + P) work for L retained records and P represented pages. That describes the model's scans and page writes, not real I/O latency. Production recovery uses more metadata and checkpoints to avoid replaying the entire history.

Edge Cases

  • Buffered COMMIT: a commit request disappears with memory and does not count as committed.
  • Durable COMMIT, no response: recovery keeps the transaction despite the missing ACK.
  • Page already flushed: replaying its after-image leaves the value unchanged.
  • Write-ahead gate: an early page-flush attempt is blocked. After the log is flushed, the page may be written even without a commit; recovery subsequently undoes it.
  • Repeated losing writes: T2's two before-images must be restored in reverse order.

Common Mistakes

  • Treating a log append as a durable flush. The log buffer is still memory.
  • Inferring commitment from a data-file value. STEAL permits uncommitted page writes.
  • Replaying only committed transactions in a repeat-history recovery pass. This model replays the full surviving history before removing losers.
  • Re-executing arithmetic during REDO instead of restoring the recorded image.
  • Undoing forward, which can reintroduce an intermediate value from the losing transaction.

A Note on Simplification

This is a generic full-log teaching model, not a PostgreSQL implementation or complete ARIES. Transactions write separate scalar pages. BEGIN and END bookkeeping is omitted; a transaction first appears in its UPDATE record. Page and log flushes are atomic, storage survives the crash, and recovery runs uninterrupted with no concurrent readers.

The lesson omits checkpoints, pageLSN-based replay skips, dirty-page and active-transaction tables, compensation log records, crashes during recovery, torn-write protection, log truncation, MVCC storage rules, and engine-specific record formats. Production engines need additional machinery for those cases. WAL on surviving storage does not replace backups when the storage itself is lost, and crash recovery alone does not provide transaction isolation.

The persistence rules follow CMU's database logging notes. CMU's recovery notes explain the additional ARIES machinery. PostgreSQL's WAL introduction explains why data-page writes can lag durable commits; its engine does not use the physical UNDO procedure pictured here.