Overview
The write-ahead log is the foundation of durability in PostgreSQL, MySQL InnoDB, SQLite, Kafka and most databases. The rule is simple: the log record must reach disk before the change it describes.
How it works
Commit path and recovery in outline.
BEGIN
append WAL record (LSN 1042: set balance=90)
fsync WAL <- durable here
update page in memory (dirty)
COMMIT acknowledged
background: write dirty pages, checkpoint at LSN 1042
crash -> start from checkpoint LSN
-> redo records after it, undo uncommitted
Group commit batches several transactions into one fsync to raise throughput without weakening durability.
Log records carry a log sequence number (LSN) that orders all changes. Each data page remembers the LSN of its last change, so recovery can skip records already reflected on disk. Physical logging records page-level byte changes, while logical logging records row-level operations and is what replication and CDC consume. Durability is configurable: synchronous commit waits for fsync, while asynchronous commit acknowledges earlier and risks losing the last few milliseconds of commits on a crash. Choose per workload and document the choice.
Pitfalls
- Turning off fsync or using disks with volatile write caches that lie about durability.
- Forgetting that replication slots or archivers pin old log segments.
- Placing the log on the same slow device as data, so commits queue behind page writes.
- Setting checkpoints too far apart, making crash recovery take minutes.
Key takeaways
- Log first, apply second, acknowledge after the log is durable.
- Checkpoints bound recovery time and allow log recycling.
- The same log enables replication, CDC and point-in-time recovery.
- Monitor log volume and the oldest retained segment.