Chapter 5. WAL: Write the Log First

Chapter 4 covered the four properties of a transaction (ACID). WAL, the subject of this chapter, is the mechanism that guarantees two of them: atomicity and durability. A database must run around the clock, but hardware-level failures such as a power outage or someone accidentally switching off the server can happen at any time. Even at such moments, the database must not lose committed data, because losing even a single record can mean a large financial loss.

To keep data from being lost or corrupted no matter when the power is cut, databases use WAL. WAL stands for Write-Ahead Log: the record of changes (the log) is written before the data pages (on disk) are modified. Every commercial RDBMS supports WAL, and it is the de facto standard way to store data safely. It is also a technique that has been studied and proven over many years. This chapter looks at what problem WAL solves, in what order the log is written, and how that log is used for recovery.