Write-Ahead Log (WAL): Durability and Crash Recovery
DatabasesBefore changing data in place, append a description of the change to a sequential log and flush it to disk. After a crash, replay the log to rebuild what was lost.
A pilot writing the planned action in the logbook before doing it. If the plane lands unexpectedly, the logbook shows exactly what was in progress.
The problem. Updating a data file in place involves many scattered disk writes. A power cut halfway leaves a half-written, corrupt structure, and the in-memory changes that were never flushed are gone.
The rule. Write the intent to the log *first*, make sure it is durable (fsync), and only then acknowledge the commit. Data pages in the main files can be written later, lazily and in any order. Appending to a log is sequential I/O, which is far faster than random writes.
What a record holds. A log sequence number (LSN), a transaction id, and enough to redo the change (and sometimes to undo it): which key or page, the new value, a checksum. The checksum lets recovery detect a record that was cut off by the crash.
import json, os
class WAL:
def __init__(self, path):
self.f = open(path, 'a+')
def append(self, op, key, value=None):
self.f.write(json.dumps({'op': op, 'k': key, 'v': value}) + '\n')
self.f.flush()
os.fsync(self.f.fileno()) # durable before we acknowledgeRecovery. On restart, read the log from the last checkpoint and re-apply each record in order to the in-memory state or data files. A record whose checksum fails, or that stops mid-line, is the torn tail: discard it and everything after it, since its transaction was never acknowledged.
Checkpoints and truncation. If the log grew forever, recovery would take forever. A checkpoint flushes the data pages up to some LSN, after which older log records are no longer needed and can be deleted or archived. More frequent checkpoints mean faster recovery but more write work during normal operation.
Group commit. fsync is the expensive part. Batching several transactions' records into one fsync raises throughput at the cost of a tiny wait. Turning fsync off is fast but breaks the durability promise.
WAL everywhere. PostgreSQL, MySQL (redo log) and SQLite use it. Kafka's log is a WAL that is the data. Replication often just ships the WAL to replicas, who replay it, which is also how point-in-time recovery works. Idempotent replay (applying a record twice has no extra effect) keeps recovery safe if it crashes midway.
- Log first, fsync, then acknowledge; apply to data files later.
- Sequential appends are cheap; random in-place writes are not.
- Recovery = replay from the last checkpoint; discard a torn tail.
- Checkpoints bound recovery time by letting old log be truncated.
- Replication and point-in-time recovery are 'ship and replay the log'.
Curated Curation & Deep Insights
ScaleDojo CertifiedOur system architects are vetting high-quality, authorized video guides for Write-Ahead Log (WAL): Durability and Crash Recovery with zero third-party platform links.
We are preparing premium, zero-competitor deep-dives for Write-Ahead Log (WAL): Durability and Crash Recovery. Authorized reading references will appear automatically.