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 File Handling: Read and Write Files
Learn to read and write files in Python using open() and the with statement. Covers text and binary modes, reading line by line, appending, and safe file handling.
Read More ProgrammingHow to Write Clean Python Functions
Clean Python functions are small, do one thing, have clear names, and few parameters. Learn practical rules for writing functions that are easy to read and test.
Read More ProgrammingHow to Write Your First Unit Test
A unit test checks one small piece of code in isolation. Learn to write your first test with the Arrange-Act-Assert pattern using a modern JavaScript test runner.
Read More Cloud & CybersecurityWhat Is a Dockerfile and How to Write One
A Dockerfile is a recipe of instructions that builds a container image. Learn the key instructions and how to write a small, fast, secure Dockerfile.
Read More