Lesson 8 · Internals & data structures
WAL: the write-ahead log
Durability, crash recovery — and the log that literally feeds your Kafka events.
Your win: explain what the WAL is, what it guarantees, and why it's fast — plus the concrete tie between a Postgres commit and a Kafka message in this repo.
Write the log before the data
A crash mid-write could leave data files half-updated. Postgres prevents that with the write-ahead log: before changing a data page, it appends a record describing the change to the WAL. The rule — log first, then data — is where the name comes from.1
COMMIT, the change is
durable — it survives a crash, because recovery replays the WAL. The
data files can lag behind safely. The WAL is also the basis of replication
(stream it to a replica) and point-in-time recovery.
Why it's fast
The WAL is a sequential append — the disk head (or SSD) just writes forward, far cheaper than scattering random in-place writes across data files at commit time. Batching many changes into one sequential log is the trick.
ExecInTx COMMIT is durable precisely because its WAL was
flushed. And in the local stack, Postgres runs logical replication off the
WAL to feed Debezium → Kafka CDC events (see the repo map). So a
committed row change in emails becomes a Kafka message via the WAL —
your Postgres and Kafka courses meeting in one pipeline.
PostgreSQL — Write-Ahead Logging (WAL) + Rogov Internals Part II
The reliability chapter on why WAL exists, then Rogov's deep treatment of WAL & the buffer cache.
→ postgresql.org/docs — WAL intro
→ PostgreSQL 14 Internals — Part II (Buffer Cache & WAL)
Check yourself (from memory)
Q1. The WAL is written…
Q2. The WAL primarily provides…
Q3. Writing the WAL is fast because it is…
synchronous_commit, or how streaming
replication rides the WAL? Ask me.