Patterns
35 items
35 items
Durability through logging before committing
Write-Ahead Log (WAL) is a durability technique where all modifications are written to a sequential log before being applied to the main data structure. If the system crashes, the log is replayed to recover to a consistent state. WAL enables atomic commits (either fully logged or not at all), point-in-time recovery, and replication. Every major database uses WAL: PostgreSQL (pg_wal), MySQL (redo log), SQLite, and distributed systems like Kafka and etcd. Understanding WAL is fundamental to understanding database internals.
Write changes to sequential log first. Only after log is durable, apply to main storage. On crash, replay log to recover. This ensures no committed data is lost.
Log is append-only, sequential writes. Much faster than random writes to B-tree pages. Batch multiple changes in single log write for efficiency.
Transaction is committed when log record is durable. Either entire transaction is in log (committed) or none of it (aborted). No partial transactions.
Log Record Contents: - Transaction ID - Operation type (INSERT, UPDATE, DELETE) - Before image (old value, for UNDO) - After image (new value, for REDO) - Table/page identifiers - Timestamp/LSN (Log Sequence Number)