What Is a Write-Ahead Log?
What is a write-ahead log, and how does it provide durability and crash recovery?
Answer out loud in under a minute: a definition, one concrete example, and one pitfall or trade-off.
Hints
Hint 1
Order the two writes — log entry and data page — and ask what a crash between them leaves behind.
Solution
Show the approach
The short version
Before a change touches the data structure, a record describing it is appended to a sequential log and flushed to stable storage. After a crash, the log is replayed to redo committed changes that never reached the data files, and to undo uncommitted ones. Sequential appends are cheap, so durability costs one flush per commit rather than a random write per page.
Example
A transaction updates three pages. The three log records and a commit record are flushed; the pages are written later, lazily. If the machine dies before the pages are written, recovery replays the three records.
Pitfall
The log grows forever unless checkpoints mark how far the data files have caught up; and fsync behaviour varies by file system and disk, so a log that is not actually flushed provides no durability at all.