# The Write-Ahead Log (WAL) As we learned in [[Chapter 5 - Safety Without Sweating]], the Lazy Elephant's biggest fear is **effort**. When a user asks to change a single digit in a ten-million-row [[Table]], a "responsible" database would find the exact [[Page]] on the spinning piece of rust (the hard drive), wait for the disk to rotate into place, and carefully etch the new digit into the metal. This is physically exhausting for three reasons: 1. **Random IO is Painful:** Finding a specific point on a disk is like trying to find a specific grain of sand on a beach while wearing a blindfold. It requires the disk arm to physically move (Seek Time), which is the slowest thing in the universe. ![[ex_seek.svg|100]] 2. **Confrontation is Scary:** To change that page, you'd have to lock everyone else out of it. If thousands of people are trying to change different grains of sand, the elephant would spend all day just managing the queue. 3. **Atomic Fear:** What if the power goes out *halfway* through etching the digit? Your sand grain is now half-melted, and the whole beach is corrupted. ## The Sequential Scribble To avoid this, the elephant carries a **WAL (Write-Ahead Log)**—a simple, sequential paper diary tucked into its pocket. Instead of walking all the way to the beach to engrave a stone, the elephant stays exactly where it is and scribbles a quick note in its diary: *"At 5:00 PM, I changed row #402 on Page #89 to say 'Sarah'."* Because the diary is just one long, continuous scroll of paper, the elephant never has to "seek." It just keeps its pen on the paper and writes in a straight line. This is called **Sequential IO**, and it is thousands of times faster than hunting for random sand grains. As soon as that ink is dry in the diary, the elephant tells the user "It's safe!" and goes back to napping. It hasn't touched the actual table yet, but it doesn't matter. Even if the elephant is suddenly struck by lightning, the diary is held in a special, durable pocket. When the elephant wakes up, it just reads the last few pages of the diary and applies the notes to the stone tablets. ## The Dirty Page & The Checkpoint While the elephant is scribbling in its diary, it *does* keep a copy of the changed data in its [[Memory]] (aka the Snack Pile). These are called "Dirty Pages"—they are different from the versions currently sitting on the cold, hard disk. Eventually, the elephant's diary gets too long, or it just feels a sudden burst of misplaced productivity (usually every 5 minutes by default). It performs a **Checkpoint**. During a Checkpoint, the elephant: 1. Stops scribbling for a brief moment. 2. Looks at all the "Dirty Pages" on its messy desk. 3. Walks over to the filing cabinet and efficiently shoves all those changes into the stone tablets in one big batch. 4. Marks a spot in the diary saying "I’m caught up to here," allowing it to eventually throw away the old, used-up pages of the scroll. ## Full Page Writes: The Safety Net There is one weird quirk to the elephant's laziness. The very first time it makes a change to a Page after a checkpoint, it doesn't just scribble the change in the diary. It panics and photocopies the **entire 8KB container** into the diary. This is called a **Full Page Write**. It seems like a lot of work, but it’s the only way to protect against "Partial Page Writes" (if the power dies while the disk is halfway through writing a block). If the stone tablet is cracked and unreadable, the elephant can just pull the perfect photocopy out of its diary and start over. Doing the least work possible today (scribbling in a notebook) ensures we can survive the hard work of tomorrow (rebuilding the world after a crash).