6.1 Storage Layers on the Write Path
From the moment the database decides to write a log record to disk until the data is actually recorded in the disk's magnetic material or flash cells, the data passes through four layers.
First, the log buffer. This is space inside the database process's memory, and log records accumulate here as soon as they are created. They have not yet been handed to the operating system.
Second, the operating system page cache. When the database hands the log buffer contents to the operating system with the write system call, the operating system keeps them in its own memory cache and immediately returns "success." Nothing has gone to disk yet. The operating system sends the data to the disk device according to its own criteria, usually within a few seconds. This is the operating-system-side cache of the double caching described in Section 1.6.
Third, the disk device cache. When the operating system sends a write command to the disk device, the device may keep the data in a small internal memory and respond "done." Both hard disks and SSDs have such a cache, and the device uses it to gather multiple writes and process them efficiently.
Fourth, the persistent storage medium of the disk. It is the only layer where data survives a power loss.
Figure 6-1. The four storage layers
flowchart TB
subgraph VOL ["Layers that vanish on power loss"]
direction TB
L1["① Log buffer<br/>database process memory"]
L2["② OS page cache<br/>operating system memory"]
L3["③ Disk device cache<br/>device internal memory"]
end
L4["④ Persistent medium<br/>magnetic material or flash cells"]
L1 -->|"write system call"| L2
L2 -->|"OS sends within seconds"| L3
L3 -->|"device writes in batches"| L4
L2 -.->|"write success response"| L1
L3 -.->|"device may respond done"| L2
This is the path log data takes from top to bottom. Solid lines are the data moving to the next layer, and dotted lines are the "success" or "done" responses coming back up from each layer.
- ① to ②: The
writesystem call moves the data to the OS page cache, and the operating system returns success at this point. - ② to ③: The operating system sends it to the disk device according to its own criteria. The device may respond done once it has the data in its own cache.
- ③ to ④: The device gathers the cached writes and records them on the persistent medium.
①, ②, and ③, shown grouped together, are all memory, so their contents vanish on power loss. Only ④ remains after a power cut.
The problem is that the first three layers are all memory that disappears when the power goes out, and when a write call returns success, the data is usually in the second layer. If the database sees only the write success and responds "committed" to the user, and the power fails right after, that log disappears along with the operating system cache. The user received the transfer complete response, but the log on disk has no record of that transaction, and the restarted server cannot even know that the transfer took place. Even if the WAL rule of writing the log first is followed, durability is not guaranteed unless it is verified that the log reached the persistent medium.