Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How a write-ahead log works: log first, apply later, fsync on commit, checkpoints and crash recovery, and why sequential logging makes databases faster.
Quick answer: A write-ahead log (WAL) is an append-only file where a database records every change before applying it to the actual data files. When a transaction commits, the database only needs to flush that small log record to disk; the data pages themselves can be written later. If the server crashes, the database reads the log on restart, redoes any committed changes that had not reached the data files, and undoes any incomplete ones. The rule is simple: never change the data on disk until the description of the change is safely in the log.
Also Read: Database Sharding Explained: Scaling Past One Machine - How To's
A single transaction may change several pages: a table row here, two index entries there. Those pages live in memory and are written to disk at different times. A crash can strike at any moment, leaving:
Writing every changed page to disk synchronously at every commit would be safe but extremely slow, because those pages are scattered all over the disk.
The write-ahead log solves this by separating durability from updating the data files.
COMMIT, the database flushes the log to disk with fsync.As the PostgreSQL documentation puts it, changes to data files must be written only after those changes have been logged. If that holds, you never need to flush data pages at commit time, because the log can rebuild them.
Each log record carries a log sequence number (LSN), and each data page remembers the LSN of the last change applied to it. That lets recovery tell which changes a page already contains.
Writing everything twice sounds wasteful. It is the opposite:
When a program writes to a file, the operating system normally keeps the data in memory and writes it out later. See how file systems work. A power cut would lose it.
fsync asks the operating system to push the file's data all the way to stable storage and not return until it is done. It is the single most important, and most expensive, call in a database: it is what turns "written" into "durable". It crosses into the kernel as a system call and waits on the hardware.
This is also where durability settings come from. Databases let you relax the flush, for example by flushing once a second instead of at every commit. Throughput rises, and a crash can lose the last moments of acknowledged transactions.
If the log only ever grew, recovery after a crash would have to replay everything since the database was created. Checkpoints prevent that:
After a crash, recovery starts from the most recent checkpoint. More frequent checkpoints mean faster recovery but more I/O during normal operation.
On restart after an unclean shutdown, the database:
When recovery finishes, the data is exactly as if every committed transaction completed and every uncommitted one never happened. That is the "A" and "D" of ACID.
Also Read: Why Your Database Needs Connection Pooling
Torn pages are handled by logging a full copy of a page the first time it changes after a checkpoint (PostgreSQL's full-page writes) or by writing pages twice (InnoDB's doublewrite buffer).
Once you have an ordered record of every change, other features follow naturally:
DROP TABLE.| System | Where the pattern appears |
|---|---|
| PostgreSQL | WAL |
| MySQL InnoDB | Redo log (plus an undo log) |
| SQLite | WAL mode, an alternative to its rollback journal; see the SQLite WAL documentation |
| LSM-tree databases | A log protects the in-memory memtable; see B-trees vs LSM trees |
| File systems | Journaling in ext4, NTFS and XFS |
| Kafka | The log is the product |
| Raft and Paxos | A replicated log of commands |
| Redis | The append-only file |
"Write down what you are about to do, then do it" is one of the most widely reused ideas in systems design.
An append-only record of changes that a database writes and flushes before modifying its data files, so that committed work can be recovered after a crash.
It usually speeds them up. A small sequential log write replaces many random data-page writes at commit time.
The point at which the database has written all changed pages to the data files, so earlier log records are no longer needed for crash recovery.
Not committed data, provided the log is flushed at commit and the storage honours the flush. If you relax flushing for speed, or the disk itself is lost, you can. That is what replication and backups are for.
The write-ahead log is what lets a database say "committed" and mean it. By writing a compact description of each change to a sequential log before touching the data files, databases get crash safety and better performance at once, and gain replication and point-in-time recovery almost as side effects.