Skip to content

WAL — Write-Ahead Logging

Before any change is written to the actual database files, it is first written to a log. "Write Ahead" = write to log FIRST, update actual data AFTER. The log is the source of truth.

How WAL Works Step by Step

Transaction: UPDATE balance = 800 WHERE id = 1

Step 1: Write to WAL log first
  [LSN=001, TX=101, UPDATE, id=1, old=500, new=800, status=IN_PROGRESS]

Step 2: WAL flushed to disk (fsync) -- log is now durable

Step 3: Update in-memory buffer (fast)
  buffer pool: id=1 → 800

Step 4: Return SUCCESS to user

Step 5: Later — dirty pages written to actual data files (async)
  [background checkpoint process]

WAL Structure

LSNTxIDOperationPageIDBeforeAfter
001TX1UPDATEP001500800
002TX1UPDATEP002300100
003TX1COMMIT
004TX2INSERTP003newrow
005TX2ROLLBACK

Sequential writes → much faster than random disk I/O

Crash Recovery Using WAL

  • ANALYSIS phase: scan WAL from last checkpoint, identify which TXs were in progress
  • REDO phase: replay ALL committed transactions (even if already written to disk)
  • UNDO phase: rollback all UNCOMMITTED transactions using "before" values in WAL

REDO vs UNDO Logs

Log TypeUsed ForWhen Used
REDO log (After image)REAPPLY committed changesCommitted TX not yet in data file
UNDO log (Before image)REVERSE uncommitted changesTX was in progress during crash

Checkpoints

WAL grows forever if never cleaned up. Checkpoint = periodic sync of dirty pages to disk. Recovery after crash only needs to replay WAL from last checkpoint — not from the beginning of time.

  • PostgreSQL: checkpoint every 5 minutes or 1GB WAL
  • MySQL: similar via innodb_log_file_size

WAL and Replication

Master writes changes to WAL → WAL shipped to replicas (WAL streaming). Replica receives WAL records and replays them. This IS master-slave replication.

WAL Write Modes — Durability Tradeoff

ModeBehaviorSafety
fsync = ON (default)WAL flushed to disk before confirming write. Slower.Zero data loss
fsync = OFFWAL stays in OS buffer cache. Much faster.Dangerous. Data corruption on OS crash
Group commitBatch multiple TXs, flush WAL for all at once.Good balance

Point in Time Recovery (PITR)

Base backup taken at Sunday midnight
WAL archived continuously since then
Database corrupted on Wednesday 2pm

PITR:
→ restore base backup (Sunday)
→ replay WAL from Sunday → Wednesday 1:59pm
→ stop just before corruption point
→ database restored to exact moment

WAL Enables

ACID Property/FeatureHow WAL Enables It
AtomicityUndo uncommitted TXs on crash
DurabilityRedo committed TXs after crash
ReplicationShip WAL to replicas
MVCCUndo log serves old versions to readers
Point in Time RecoveryReplay WAL to any point in time