A Write-Ahead Log (WAL) is a durability protocol where all data modifications are first recorded as sequential, append-only entries in a persistent log file before being applied to the main database or memory structure. This process, known as write-ahead logging, guarantees that no committed transaction is lost during a system crash. The protocol operates in distinct phases:
- Log Write: When a transaction modifies data, the change is serialized into a log record. This record includes the transaction ID, the data to be changed (the redo information), and sometimes the old value (the undo information). This record is synchronously written (often
fsync'ed) to the WAL file on stable storage.
- Commit Point: Once the log record is durably stored, the transaction is considered committed, and an acknowledgment can be sent to the client.
- Application to Main Store: The actual modification is then applied to the in-memory data structures (like a B-tree or memtable) or the on-disk database files. This can happen asynchronously or in a batch.
- Checkpointing: Periodically, a checkpoint is created. This marks a point in the log where all prior transactions have been fully applied to the main store. Old log segments before the checkpoint can be safely archived or deleted.
This sequence ensures that if a crash occurs after step 1 but before step 3, the system can recover by replaying the log records from the last checkpoint, thereby reconstructing all committed changes.