# Write-Ahead Logging: How Databases Survive Crashes Interviewers sometimes dig below "the database is durable" and ask how. The answer is almost always a write-ahead log (WAL). ## The rule Before a change is applied to the main data files, a record describing it is appended to a log and flushed to disk (`fsync`). Only then is the transaction acknowledged. The data pages themselves can be written later, lazily, in any order. Why this is fast: appending to one sequential file is far cheaper than updating random pages scattered across disk. ## Recovery After a crash, the database reads the log from the last **checkpoint**: 1. **Redo** changes from committed transactions whose pages may not have reached disk. 2. **Undo** changes from transactions that never committed (ARIES-style systems log enough to roll them back). Checkpoints periodically flush dirty pages and record a position, so recovery doesn't replay the whole history and old log segments can be deleted. ## Tuning knobs worth knowing - **Group commit**: batch fsyncs from many transactions into one, raising throughput. - **Synchronous vs asynchronous commit**: acknowledging before the fsync is faster but can lose the last few transactions on a crash. - Put the log on fast storage; it is on the critical path of every write. ## The log as a building block - **Replication**: PostgreSQL streams WAL records to replicas; MySQL uses its binlog. - **Change data capture**: tools like Debezium read the log to publish row changes to Kafka. - **LSM-tree stores** (RocksDB, Cassandra) write to a commit log plus an in-memory table, then flush sorted files. - Kafka is, in a sense, a WAL offered as a service. Linking these ideas together is a strong signal in a storage-focused interview. Disclosure: I work on Linkjob AI (https://www.linkjob.ai/), an AI assistant for interview prep.