Skip to content
System Design Explorer
storage

Write-Ahead Log

By Ankur NayakReviewed

A write-ahead log (WAL) records every change in an append-only log before the change is applied to the data itself. A commit only has to flush the sequential log, not the scattered data pages, and after a crash the database replays the log to redo anything that hadn't reached the data files. Replication protocols ship the same log to replicas.

Write-Ahead Log diagramComponents: Transaction, Write-Ahead Log, Data Pages. Connections: Transaction to Write-Ahead Log (append + flush); Write-Ahead Log to Data Pages (apply / redo).TransactionServiceWrite-Ahead LogSQL databaseData PagesSQL database
Commit and recover with a WAL

How it works

The rule: changes to data files may be written only after those changes have been logged and the log flushed to durable storage. If the machine crashes, nothing is lost: every committed change is in the log, and recovery replays (redoes) the records that hadn't been applied to the data pages yet.

It is faster as well as safer. To guarantee a commit, only the WAL file needs to be flushed to disk, and it is written sequentially, which is far cheaper than flushing many data pages scattered across the disk. One flush can commit many concurrent transactions at once.

The log is also the natural unit of replication. In DynamoDB, the leader of a partition's replication group writes a write-ahead log record and acknowledges the write once a quorum of replicas has persisted it; some replicas store only recent log entries. Spanner keeps each tablet's state in B-tree-like files plus a write-ahead log.

Key points

  • Log first, then apply
  • Commit = flush the log (sequential, cheap)
  • Crash recovery = replay the log (REDO)
  • The same log feeds replicas and point-in-time recovery

When to use it, and pitfalls

Use it for

  • Any durable database or storage engine
  • Replicating state between nodes
  • Change data capture and point-in-time recovery

Watch out for

  • Letting the log grow forever: checkpoint data pages and truncate old segments
  • Acknowledging a write before the log record is durable
  • Slow log disks put a ceiling on commit throughput

Trade-offs

Synchronous vs. batched log flushes

Flush on every commit for durability; group-commit many transactions per flush for throughput.

Pros: Group commit keeps durability with far fewer disk syncs

Cons: Each transaction may wait a little for its batch

Used in

Frequently asked questions

Why does a write-ahead log make databases faster?

Because a commit only has to flush the log, which is written sequentially, instead of every changed data page, which would mean many scattered writes. One log flush can also commit many concurrent transactions together.

What happens to the WAL after a crash?

The database replays it on restart. Every committed change is in the log, so any change that hadn't yet been applied to the data files is redone from the log records, a process called roll-forward or REDO recovery.

Sources