Write-ahead log explained in plain terms means understanding a simple rule that keeps databases safe: write the change down first, and only then change the data. The central question is how a database can survive a crash in the middle of a write without losing data or corrupting what it holds, and the answer is a log, written to disk before anything else, that records every change in order.
A write-ahead log, usually shortened to WAL, is an append-only record of every change made to a database, written to durable storage before the change itself is applied to the data files. If the system crashes, the database replays the log on restart, redoing the changes that were recorded but not yet written to the data, and discarding the ones that were never completed.
The idea is simple: a sequential log is cheap to write, since appending to the end of a file needs no seeking, while updating data in place is expensive and can leave the data half-written if the power fails. By writing to the log first, the database guarantees that no committed change is ever lost. This article explains what a write-ahead log is, how it works, and where it is used.
Key takeaways
- A write-ahead log records every change before the data itself changes.
- The log is append-only, which makes writes fast and sequential.
- On recovery, the database replays the log to restore committed work.
- Checkpointing copies log changes into the data files and frees the log.
- Nearly every modern database relies on a write-ahead log.
What a write-ahead log is
A write-ahead log is a file, or a set of files, that a database appends to whenever data changes. Each entry, called a log record, describes a single change: which page of data was touched, what the old value was, and what the new value is, together with a sequence number that orders the records. The log is the database’s memory of what it did, in the exact order it did it.
The rule that gives the log its name is strict: a log record must be on disk before the data change it describes is allowed to reach disk. This is the write-ahead rule, and it is what makes recovery possible, since the log always contains a complete record of every change that could possibly be in the data files.
Write-ahead logging was developed as databases moved from simple file systems to systems that had to survive crashes without losing transactions. It is now the standard durability mechanism in relational databases, and the same idea appears in message queues, file systems, and distributed systems wherever a crash must not lose committed work. Our guide to ACID explains the durability guarantee that a write-ahead log provides, and our guide to SQL explains the databases that rely on it most.
How a write-ahead log works
When a transaction changes data, the database first writes a log record describing the change, and then changes the data in memory. The data in memory is not yet on disk, and it may stay there for a long time, but the log record is forced to disk before the transaction is allowed to commit, so that a crash at any point after the commit cannot lose the change.
Each log record carries a log sequence number, which increases with every record and gives the log a total order. The database also tracks, for each page of data, the sequence number of the last log record that touched it, so that on recovery it can tell whether a page already contains a change or whether that change still needs to be replayed.
Recovery has three phases. Analysis reads the log to find out which transactions were active when the crash happened and how far the log reached. Redo replays every log record from the last checkpoint onwards, applying each change to the data, so that all committed work is restored. Undo then rolls back any transaction that had started but not committed, using the old values stored in the log, leaving the database in a consistent state.
Checkpointing is how the log is kept under control. A checkpoint writes all the changed pages in memory out to the data files, records the point in the log up to which everything is safely stored, and then allows the log before that point to be reused or deleted. Without checkpoints, the log would grow forever and recovery would take longer and longer.
The log also supports replication. A standby server can receive a stream of log records from the primary and apply them in order, keeping an exact copy of the data without ever running the original transactions, which is how many databases provide high availability and read replicas.

Comparing a write-ahead log with other durability methods
A write-ahead log is one of several ways to make data durable, and each was built with a different emphasis. Shadow paging writes a new copy of the data and swaps it in, double-write buffers protect against torn pages, and in-memory databases rely on snapshots and replication, but each approach trades speed, safety, and complexity in a different way.
The table below sets out the main contrasts between the leading approaches.
| Aspect | Write-ahead log | Shadow paging | Snapshot and replay |
|---|---|---|---|
| How it protects data | Log written before data | New copy swapped in atomically | Periodic full copy saved |
| Crash recovery speed | Fast, replay the log | Fast, use the old copy | Slow, reload the snapshot |
| Write overhead | Low, sequential appends | High, whole pages copied | Low, until a snapshot runs |
| Supports replication | Yes, stream the log | No, easily | Yes, with extra work |
| Best for | Relational databases | Simple embedded stores | In-memory databases |
The practical result is that a write-ahead log is the right choice for most databases, shadow paging suits simple stores where atomic replacement is enough, and snapshots suit in-memory systems that can afford to reload their state.
Where a write-ahead log is used
Relational databases use write-ahead logging as their core durability mechanism. PostgreSQL, MySQL’s InnoDB engine, Oracle, and SQL Server all write changes to a log before the data, and all recover by replaying that log, which is why a database can survive a power cut in the middle of a transaction without losing committed work.
SQLite uses a write-ahead log as an optional mode, and it is now the recommended setting for most applications. In WAL mode, readers and writers can work at the same time, since readers use the data files while writers append to the log, which makes the small embedded database far more concurrent than it was in its original mode.
Message queues use write-ahead logs to make delivery durable. A broker writes each incoming message to its log before acknowledging it to the producer, so that a crash cannot lose a message that was already confirmed, and consumers can replay the log from any point to catch up.
File systems use journaling, which is the same idea under another name. A journaling file system writes its intended changes to a journal before applying them, so that a crash during a write can be repaired by replaying the journal, rather than leaving the file system in an inconsistent state that needs a long scan to fix.
Distributed systems use write-ahead logs in their consensus protocols. Raft and Paxos both keep a log of agreed commands, and a node that crashes recovers by replaying its log, which is why the log is the central data structure in modern distributed consensus.
Strengths and limits
The greatest strength of a write-ahead log is that it makes durability cheap. Appending to a log is sequential, which disks and flash storage handle quickly, while random writes to data files are slow, so a database can commit transactions fast while still guaranteeing that committed work survives a crash.
It is also a strength that the log does more than one job. The same log that provides crash recovery also drives replication, supports point-in-time recovery to any moment in the past, and gives auditors a complete record of what changed and when, which makes it one of the most useful structures in a database.
The limits are real, though. The log must be managed: it grows, it must be checkpointed and archived, and a log that fills its disk will stop the database entirely. Recovery time also grows with the amount of log to replay, so a system that checkpoints rarely will take longer to restart, and the log itself must be on reliable storage, since a lost log means lost data.
Write-ahead log checklist
- Put the log on fast, reliable storage, separate from the data files.
- Force log records to disk before the transaction commits.
- Checkpoint regularly so recovery stays fast.
- Archive old log segments before deleting them.
- Monitor log growth; a full log stops the database.
- Test crash recovery, not just clean shutdowns.
- Use log shipping for replicas and disaster recovery.
- Keep a retention policy for point-in-time recovery.
- Never edit or truncate the log by hand.
Frequently asked questions
What is a write-ahead log in simple terms?
It is a diary in which a database writes down every change before it makes the change to the data itself. If the system crashes, it reads the diary on restart and replays the changes, so nothing committed is ever lost.
Why must the log be written before the data?
Because the log is the only record of what happened. If the data were written first and the system crashed before the log, the database would have no way of knowing what changed, and the data could be left half-written and corrupt.
What is a checkpoint?
It is the point at which the database writes all changed data in memory out to the data files and records how far the log has been safely stored. After a checkpoint, the log before that point can be reused or deleted, and recovery only needs to replay the log since the last checkpoint.
How does a write-ahead log help replication?
A standby server can receive the stream of log records from the primary and apply them in order, keeping an exact copy of the data. The standby never runs the original transactions; it simply replays the log, which is faster and simpler.
What is the difference between a write-ahead log and a journal?
Very little: a journal in a file system is the same idea, a record of intended changes written before the changes themselves. The term write-ahead log is used in databases, while journal is used in file systems, but the principle is identical.
When should I not rely on a write-ahead log?
When durability does not matter, such as a cache that can be rebuilt, or when the log itself cannot be stored reliably. In those cases a simpler design, such as snapshots alone, may be enough, but committed data will be at risk.
The bottom line
Write-ahead log explained in one line is this: write the change down first, and the data can always be rebuilt.
It is the quiet mechanism behind almost every database, the reason a crash in the middle of a transaction loses nothing that was committed, and one of the most durable ideas in computing.
This article is general information about how write-ahead logs work, not advice about configuring durability for a particular system.







