Question presented to candidate:
"What is a write-ahead log? Explain why databases write to a log before changing their data files, what happens during crash recovery, and where else you see the same idea in system design."
What a strong answer should cover:
The rule: a change is recorded in an append-only log, and the log is flushed to disk (fsync) before the transaction is reported committed and before the changed data pages may be written
Why it works: a crash can lose memory and half-written pages, but recovery replays the log (redo committed changes, discard or undo uncommitted ones) to reach a consistent state
Why it is fast: commits become sequential appends instead of random page writes; pages are written lazily; group commit shares one fsync across many transactions
Details that show depth: LSNs and page LSNs make redo idempotent, checksums detect a torn last record, checkpoints bound recovery time and let old log be recycled
The same log powers replication, point-in-time recovery and change data capture; the idea reappears in LSM-tree commit logs, Raft logs and Kafka
Knobs and trade-offs: PostgreSQL synchronous_commit, full_page_writes, checkpoint frequency vs recovery time
Code / implementation expected: Optional โ a small log with checksummed records and a redo recovery; the example builds one and crashes it.
wal
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Beginners to database internals โ you know a database stores data on disk and that a server can crash; everything else is defined as we go.
Difficulty: Medium
Verified with: Node 22.22 and Python 3.12 (both print identical output). PostgreSQL settings were read with SHOW on a local PostgreSQL 16.15 server; SQLite behaviour was checked with SQLite 3.45.1 from Python.
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the exact words an interviewer expects. Byte counts and recovery steps come from the example program; the Verified section quotes its full output.
1. Why This Even Matters โ A Story First
A small bank keeps a big ledger: one page per customer, each showing the balance. Moving 100 from Alice to Bob means changing two pages: Alice's (minus 100) and Bob's (p
The solution is hidden so you can try first
16 min read ยท 11 sections
4
Read the code
A write-ahead log from scratch: crash, redo recovery, torn record, checkpoint, group commit