Course map

Topic 068 / Advanced

Write-ahead logging and durability

Chapter 5.4 · Engines, Durability, Transactions, and Concurrency

What and why

A write that only changes an in-memory page is lost the instant the process crashes. Write-ahead logging (WAL) makes a write durable by appending it to a log, and fsyncing that log, before acknowledging the write or applying it in memory — so a crash can always be recovered from the log. Builds on: → 67 (storage engine fundamentals: pages, compaction, checkpoints).

Diagram

write request
     │
     ▼
append to WAL ──fsync──► durable on disk
     │
     ▼
acknowledge to client
     │
     ▼
apply to in-memory page ──(later, at checkpoint)──► flush page to disk

How it works

  1. Before a data page is modified, the change is appended to the write-ahead log.
  2. The log append is fsynced — it survives a crash the instant the sync completes.
  3. Only after the log entry is durable does the engine acknowledge the write to the client or apply it to the in-memory structure.
  4. On crash, the engine replays the WAL from the last checkpoint (→ 67) to reconstruct any state that wasn't yet flushed to a data page.
  5. The same log is the natural feed for replication (→ 72) — a follower can replay the identical sequence of changes.

Practical example

A crash right after the log entry is fsynced but before the data page is flushed: on restart, WAL replay reapplies the change — nothing acknowledged is lost. A crash before the log entry is fsynced, while the client is still waiting: the client never received an acknowledgment, so this is a correct "unknown outcome" for the caller to check and retry idempotently (→ 38) — not a lost committed write.

When to use it

  • Use when: any write must survive a crash — true for nearly all OLTP workloads.
  • Avoid when: a workload explicitly and consciously accepts weaker durability for throughput (e.g., an ephemeral cache) — that's a deliberate trade-off, not this topic's default.
  • Failure scenarios: disabling or delaying the fsync to improve throughput, then an outage wipes writes the application believed were acknowledged — a silent data-loss bug, not a performance tweak.
  • Simpler alternative: none — durability is foundational. The only real choice is which durability level to accept, consciously, for a given workload.

Practice

(15–20 min, lab Postgres). Run a small write-heavy workload against the lab database, then stop the container abruptly (docker compose kill, not a graceful shutdown) mid-workload and restart it. Confirm every transaction that was acknowledged before the kill is present, and nothing acknowledged is missing. Done when you can explain that result using the WAL replay sequence above.

Shared lab setup

Key takeaway

Durability means logged-and-synced before acknowledged — never the reverse. Done when you can: say exactly what state survives a crash at any point in the write sequence above. Used next in → 69 (durability as one ACID property), → 72 (replication ships this log), → 80 (point-in-time recovery replays WAL archives).

References

Kleppmann and Riccomini, Designing Data-Intensive Applications — see references.md.

Source directory

Search the guide