Write-Ahead Logging (WAL): Survive Crashes by Journaling First
Think of it as a journal of intentions. Before changing data, a database writes the intended change to a log file first. This ensures that if the system crashes, it can recover by replaying the log, guaranteeing no writes are lost.
WHY IT EXISTS: Databases need to be both fast and safe. Writing changes directly to complex data files on disk is slow and risky; a crash mid-write can corrupt the entire database. WAL solves this by separating the intention to write from the actual, slower modification of data structures.
THE MENTAL MODEL: A write-ahead log is like a pilot's pre-flight checklist. Before taking off (modifying the main data), the pilot logs every intended action. If something goes wrong, the log provides a complete, ordered record of what was supposed to happen, allowing for a safe recovery. You write to a simple, append-only journal first, then do the complex work later.
HOW IT WORKS: When a transaction requests a data modification, the database doesn't immediately change the main data files. First, it writes a record describing the change (e.g., "change value X to Y at location Z") to the end of the WAL file on disk. This is a very fast, sequential I/O operation. Only after this log entry is safely persisted does the database apply the change to its in-memory copy of the data. These in-memory changes are flushed to the main disk files later, in an optimized batch. If the server crashes, upon restart it simply reads the WAL and replays any changes that hadn't made it to the main data files, ensuring atomicity and durability.
WHEN TO USE IT: WAL is the default mechanism for almost all modern transactional databases that require ACID compliance, especially atomicity and durability. It's fundamental to systems like PostgreSQL, MySQL (InnoDB), Oracle, and SQL Server. It's the bedrock of reliable data management.
WHEN NOT TO USE IT: You might not need WAL in systems where data loss is acceptable or where durability is not a requirement. For example, some in-memory caches or analytics databases designed for high-speed, volatile data might disable it to squeeze out maximum performance, accepting the risk of data loss on a crash.
ONE CANONICAL EXAMPLE: PostgreSQL heavily relies on WAL. Every single modification to the database's data is first written to the WAL. This not only provides crash safety but also enables powerful features like streaming replication (where standby servers read the primary's WAL to stay in sync) and Point-In-Time Recovery (PITR), which allows administrators to restore a database to any specific moment by replaying the log from a backup.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.