Write-Ahead Log
Database durability and crash recovery technique
A Write-Ahead Log (WAL) is a durability technique in which a database records every change to a sequential log file before applying that change to its main data structures, ensuring changes can be recovered after a crash.
Definition
A Write-Ahead Log (WAL) is a durability technique in which a database records every change to a sequential log file before applying that change to its main data structures, ensuring changes can be recovered after a crash.
Overview
The write-ahead logging principle is simple to state but foundational to how nearly every reliable database system guarantees durability: before a change is applied to the actual data pages on disk, a description of that change is first written to an append-only log file, and that log write is confirmed (flushed to durable storage) before the transaction is considered committed. If the database crashes at any point after that, it can replay the log on restart to reconstruct any changes that were committed but not yet reflected in the main data files. This ordering — log first, data second — is what gives WAL its name and its power. Writing sequentially to a log file is also far faster than writing randomly across scattered data pages, so WAL is as much a performance optimization as a durability mechanism: the database can defer the more expensive random-write work of updating actual data pages, batching and reordering it for efficiency, while still offering strong durability guarantees because the log has already captured everything needed to recover. PostgreSQL's WAL implementation is one of the most widely studied; it underlies not just crash recovery but also PostgreSQL's replication features, since replicas can receive and replay the same WAL stream the primary generates to stay in sync. MySQL's InnoDB storage engine uses an analogous redo log for the same purpose. SQLite offers WAL mode as an alternative to its default rollback-journal mode, improving concurrency by letting readers continue working from the last-known-good state while a writer appends to the WAL. WAL is closely related to, but distinct from, the broader ACID durability guarantee it helps implement, and it typically works alongside checkpointing, a periodic process that applies logged changes to the main data files and allows old log segments to be safely discarded or archived.
Key Concepts
- Logs changes sequentially before applying them to main data files
- Enables crash recovery by replaying the log on restart
- Sequential log writes are faster than random data-page writes
- Underpins ACID durability guarantees in most relational databases
- Used for streaming replication in systems like PostgreSQL
- Paired with periodic checkpointing to reclaim old log space
- Analogous mechanisms include MySQL InnoDB's redo log
Use Cases
Frequently Asked Questions
From the Blog
Python Error Handling: try, except, finally Explained
Errors are inevitable; crashes are not. This guide explains Python's exception system from first principles: how try/except/finally works, which exceptions to catch (and which to let propagate), how to raise your own exceptions, and how to write error handling that helps debugging rather than hiding bugs.
Read More ProgrammingTesting Python Code with pytest: A Beginner's Guide
Untested code is legacy code from the moment it's written. This guide explains how to write effective Python tests with pytest — from your first test function through fixtures, parametrize, mocking, and measuring coverage.
Read More