Write-Ahead Log (WAL)
The WAL answers one question: how do you make a write durable without flushing your entire data structure on every operation?
Answer: write your intent first, then apply the change.
The guarantee
Section titled “The guarantee”If a record exists in the WAL, it can always be replayed after a crash.
Write path
Section titled “Write path”- Append log record to WAL
fsync()the log ← the durability checkpoint- Apply change in memory
- Acknowledge to client
Working Python simulation
Section titled “Working Python simulation”import json, osfrom pathlib import Pathfrom dataclasses import dataclass, asdict
@dataclassclass WALRecord: lsn: int; operation: str; key: str; new_value: str
class WAL: def __init__(self, path="/tmp/wal.jsonl"): self.path = Path(path); self.lsn = 0
def append(self, record: WALRecord) -> int: self.lsn += 1; record.lsn = self.lsn with open(self.path, "a") as f: f.write(json.dumps(asdict(record)) + "\n") f.flush() os.fsync(f.fileno()) # force to physical disk return self.lsnWAL in production systems
Section titled “WAL in production systems”| System | WAL | Default fsync |
|---|---|---|
| PostgreSQL | pg_wal/ | every commit |
| MySQL InnoDB | ib_logfile | every commit |
| RocksDB | .log files | configurable |
| Kafka | log segments | configurable |