Write-ahead log and recovery
In short: With a write-ahead log (WAL) every change is first written to a log on disk before the actual data is changed. After a crash the state can be restored from it.
In more detail: The log is append-only (sequential, fast). A commit counts as durable once the log entry is safely written (fsync). The data pages may follow later and in bulk, because in an emergency recovery replays the log.
In Depth
- Principle: First log, then data. This ensures atomicity and durability (ACID).
- Checkpoint: Marks up to where everything is on disk so the log can be truncated.
- Recovery (ARIES): Analysis, redo (catch up missing changes), undo (roll back unfinished ones).
- Replication: The WAL is also streamed to replicas (for example PostgreSQL).
- Point-in-time recovery: Back to any moment by replaying up to it.
- Cost:
fsyncis expensive. Group commit bundles several transactions.
See also: Database transactions and ACID, LSM tree and sorted string tables