Write-Ahead Log
A technique where changes are written to a log before being applied to the database. Ensures durability and crash recovery.
What is Write-Ahead Log?
In short
A write-ahead log (WAL) is a durability technique where every change is appended to a sequential on-disk log and forced to stable storage before the change is applied to the main data files. If the system crashes, it replays the log on restart to recover any committed changes that had not yet reached the data files, guaranteeing no committed write is ever lost.
What a write-ahead log actually is
A write-ahead log is an append-only file that records the intent to change data before the change touches the actual table or page on disk. The rule it enforces is in the name: write to the log ahead of writing to the data. Postgres calls this the WAL, MySQL InnoDB calls it the redo log, SQLite calls it WAL mode, and SQL Server calls it the transaction log. They are all the same idea.
The reason it exists is that random writes to data files are slow and a crash in the middle of one leaves the file half-updated. Appending to a log is fast because it is sequential, and the log is the source of truth for what was supposed to happen. As long as the log entry made it to durable storage, the database can always finish or redo the work later.
Each entry usually describes a physical or logical change: page 4012 byte offset 80 changed from this to that, or this row was inserted into this table. Entries carry a monotonically increasing sequence number, called the LSN in Postgres and SQL Server, so recovery knows the exact order to replay them.
How it works under the hood
When a transaction modifies data, the database first writes the change into an in-memory log buffer and updates the page in the shared buffer cache. The actual data file on disk is not touched yet. When the transaction commits, the database calls fsync on the log file so the log records are physically on disk. Only after that fsync returns does the commit report success to the client.
The dirty data pages stay in memory and get written to the data files later, in the background, by a checkpoint process. A checkpoint flushes all dirty pages up to a known log position and records that position, so on recovery the database only needs to replay log entries after the last checkpoint instead of the entire history.
Recovery after a crash is a replay. The database reads the log from the last checkpoint forward, reapplies every committed change to the data files (redo), and rolls back any transaction that was in flight but never committed (undo). This is the ARIES algorithm that most relational engines follow. Because commit only depends on a sequential append plus one fsync, a write-ahead log also lets many transactions group their commits into a single disk flush, which raises throughput a lot.
When to use it and the trade-offs
You get a write-ahead log for free inside any serious database, so the real decisions are about tuning and about reusing the log. The big win beyond crash recovery is replication: the same log that recovers a crash can be streamed to replicas, which is exactly how Postgres streaming replication and MySQL binlog replication work. Change data capture tools like Debezium read the log to feed Kafka, so downstream systems see every change in order.
The cost is write amplification. Every change is written twice, once to the log and once to the data files, so a busy system does roughly double the disk writes. The commit fsync also adds latency, often a few milliseconds per commit on spinning disks, which is why group commit and fast SSDs matter. If you set the log to flush less aggressively, for example Postgres synchronous_commit off or MySQL innodb_flush_log_at_trx_commit set to 2, you trade a small window of possible data loss on a crash for much higher throughput.
Logs also need management. They grow until a checkpoint lets old segments be recycled or archived, and if archiving falls behind, the log can fill the disk and stall the database. A long-running transaction or a stuck replica can pin old log segments and cause the same problem.
A concrete example
Imagine a bank transfer that debits account A by 100 and credits account B by 100. Inside one transaction the database writes two log records, one for each row change, then a commit record, and fsyncs the log. At this point the data pages for A and B may still be sitting in memory only.
Now the power fails before those pages reach disk. On restart, the database finds the last checkpoint, reads forward, sees the commit record for the transfer, and redoes both row changes against the data files. The money moved exactly once and the books balance. If instead the crash happened after the debit record but before the commit record, recovery sees no commit, so it discards the partial work and account A keeps its 100. Either way the database is consistent, which is the whole point.
Where it is used in production
PostgreSQL
The WAL is the core durability mechanism and also the source for streaming replication and point-in-time recovery via archived WAL segments.
MySQL InnoDB
Uses a redo log for crash recovery plus a separate binlog that drives replication and tools like Debezium.
SQLite
Offers WAL journal mode so readers and a writer can run concurrently and commits are fast appends instead of rewriting the database file.
Apache Kafka
The entire broker is built as a durable append-only commit log, the same write-ahead idea promoted to be the primary storage abstraction.
Frequently asked questions
- Why write to a log first instead of straight to the data files?
- Appending to a log is sequential and fast, and it gives the database a durable record of intent. A crash partway through a slow random update to a data file would leave it corrupt, but if the log entry survived, recovery can always finish or redo the change. The log, not the data file, is the source of truth at commit time.
- What is the difference between a write-ahead log and a checkpoint?
- The WAL records every change as it happens. A checkpoint is a periodic event that flushes the dirty in-memory pages to the data files and records how far the log has been applied. Checkpoints bound recovery time so you only replay log written after the last checkpoint, not the entire log history.
- Does a write-ahead log slow down writes?
- It adds write amplification because each change is written to both the log and the data files, and a commit normally costs one fsync. On fast SSDs this is small, and group commit batches many commits into a single flush. You can relax the flush policy for more throughput at the cost of a small data-loss window on a crash.
- How is a WAL used for replication?
- Because the log already contains every change in order, the database can stream those log records to replica servers that apply them to stay in sync. Postgres streaming replication ships WAL, MySQL ships its binlog, and change-data-capture tools read the same log to publish events to systems like Kafka.
- What happens to the log after the data is safely written?
- Once a checkpoint confirms that all changes up to a log position are in the data files, older log segments are no longer needed for crash recovery and can be recycled or archived. If archiving or a replica falls behind, those segments must be kept, which can fill the disk and stall writes.
Learn Write-Ahead Log hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on Write-Ahead Log as part of a larger topic.
Write-Ahead Logging
Write changes to a sequential log before applying them, the foundation of database crash recovery
advanced · consistency models
Write-Ahead Log Replication
Postgres WAL, the foundation of crash recovery, replication, and point-in-time restore
intermediate · data replication distribution
See also
Related glossary terms you might want to look up next.
ACID
Four guarantees for database transactions: Atomicity (all or nothing), Consistency (valid states only), Isolation (no interference), Durability (changes persist).
Replication
Keeping copies of the same data on multiple servers. Improves read performance and provides fault tolerance if one server goes down.
Event Sourcing
Storing every state change as an immutable event instead of just the current state. You can rebuild any past state by replaying events.
Database Partitioning
Dividing a large table into smaller, more manageable pieces while keeping them in the same database. Sharding is partitioning across servers.
Read Replica
A copy of your database that handles read queries, reducing load on the primary database. Writes still go to the primary and replicate out.
Consistent Hashing
A hashing technique where adding or removing servers only moves a small fraction of keys. Used by Amazon DynamoDB and Cassandra for data distribution.