Write-ahead log
An append-only file that records every change before the change is applied to the data structures it describes. It exists because a crash can happen between modifying several pages, and replaying a sequential log is the cheapest way to make a set of writes atomic and durable.
What it costs
| Operation | Cost | Note |
|---|---|---|
| Append a record | O(1), sequential write | A few hundred bytes describing the change (page id, offset, new bytes) plus a checksum. |
| Commit | 1 fsync, 0.1–2 ms | fsync forces the OS to put the buffered log bytes on the physical disk; the transaction is durable only after it returns. Group commit shares one fsync across many transactions. |
| Crash recovery | O(log since last checkpoint) | Redo every record after the last checkpoint; undo any transaction without a commit record. |
| Ship to a replica | O(bytes), sequential read | The replica applies the same records in the same order; its position is tracked by log sequence number. |
| Truncate | O(1) | Records before a checkpoint are no longer needed for recovery, so old segments are archived or deleted. |
Memory: A few MB of in-memory log buffer; on disk, one or more fixed-size segment files (16 MB each in Postgres) kept until checkpointed and archived.
Log first, pages later, replica and archive follow the same stream
A commit is durable once its log record is fsynced. Dirty pages are written back lazily by checkpoints; recovery replays the log from the last checkpoint. Replicas and the archive consume the identical byte stream by LSN.
How it works
- When a transaction changes a row, the database first writes a log record describing the change: which page, which bytes, old and new values, and the transaction id. The record is appended to an in-memory log buffer, not yet to disk.
- The actual data page in memory is modified. It is now dirty (different from the copy on disk) and will be written back later, in no particular order.
- On COMMIT the database writes a commit record, then calls fsync on the log file so the operating system pushes those bytes to the physical disk. Only when fsync returns is the client told the commit succeeded. Dirty data pages may still be in memory at this point; that is allowed because the log is enough to rebuild them.
- Every record has a log sequence number, the byte offset in the log stream. Each data page stores the LSN of the last record applied to it, so recovery can tell whether a page already has a change or still needs it.
- Periodically a checkpoint writes all dirty pages to disk and records the LSN it reached. On restart the database reads the log from the last checkpoint: it redoes every record whose page LSN is older than the record, and undoes any transaction that has no commit record.
- The same stream is sent over the network to replicas, which apply it in order; a replica’s applied LSN compared with the primary’s current LSN is the replication lag. Archived segments plus a base backup allow replay to any moment, which is point-in-time recovery.
Why it is shaped this way
- One sequential fsync per commit (or per group of commits) is far cheaper than fsyncing every dirty page, and sequential writes are the fastest thing a disk does.
- Because the log is the single source of truth for what happened and in what order, the same bytes can serve crash recovery, replication and change data capture without three mechanisms.
Worked example
The trip table in the ride-matching service
- 15K trip state changes per second at peak, each an UPDATE of one row. Each log record is about 150 B: 2.25 MB per second of log, 194 GB per day before archiving.
- Without group commit, 15K fsyncs per second at 0.5 ms each would need 7.5 seconds of fsync time per second: impossible on one drive. With group commit the engine flushes every 2 ms, so about 30 transactions share one fsync; 500 fsyncs per second, well within an NVMe drive’s budget.
- Checkpoint every 5 minutes: on a crash, recovery replays at most 5 × 60 × 2.25 MB = 675 MB of log at several hundred MB per second, so the database is back in a few seconds.
- A read replica in the same region applies the stream with 10–50 ms of lag. If a rider reads their trip status 20 ms after the driver accepts, the replica may not have the update yet; that read is routed to the primary.
- Segments are archived to object storage; a base backup from Sunday plus the archived log lets the team restore to 14:03:27 on Wednesday, one second before a bad migration ran.
When it is the wrong choice
- Data you can afford to lose or rebuild (a cache, a derived index): skipping the log and its fsync roughly doubles write throughput, which is why Redis offers fsync-every-second and Memcached has no log at all.
- Workloads that are already append-only files (object storage, immutable segments): the data itself is the log, so a second log adds nothing.
- Where the bottleneck is fsync latency on slow disks and the application cannot batch commits: either accept a small loss window (asynchronous commit) or move the log to faster storage.
Interviewer follow-ups
Answer out loud before you open each one.
Why is it safe to acknowledge a commit before the data pages reach disk?
Because the log record for that change is already on disk. If the process crashes, recovery reads the log and reapplies the change to the page. The data page is a cache of the log’s effects; the log is the record of truth.
What is group commit and why does it matter?
Instead of one fsync per transaction, the database waits a millisecond or two and fsyncs once for all transactions that committed in that window. Fsync cost is per call, not per byte, so sharing it raises commit throughput by an order of magnitude at the cost of a millisecond of added latency.
How does the same log give you replication and point-in-time recovery?
A replica is just another consumer of the log stream that applies records in order; its lag is the difference in LSN. Archiving old segments to object storage keeps the whole history, so a base backup plus the archived log can be replayed up to any LSN, which is a restore to any second.
What is the difference between physical and logical logging?
Physical records describe byte changes to pages, which replay quickly but only on an identical storage layout. Logical records describe row-level operations (insert this row), which are slower to apply but can be consumed by a different system, which is what change data capture tools decode.
Where it shows up
- Building blocks
- Spread reads across replicas · Survive losing a node · Recover from bad data, not just dead disks · React to database changes · Keep events as a replayable log · Let users see their own writes · Write-optimised vs read-optimised storage
- Problems
- Ride Matching (Uber / Lyft) · URL Shortener (TinyURL / bit.ly) · Chat App (WhatsApp / Messenger)