map / systems / io-stack

Systems · principle 6 of 7

The I/O stack

A write() usually lands in the page cache and returns; durability only happens when data is flushed to the device.

01 · Why it’s true

When a program calls write(), the kernel usually copies the data into the page cache in memory and returns right away. The data reaches the disk later, in the background.

That makes writes fast, but it means a successful write doesn’t mean the data is on disk. A crash or power loss can lose recent writes unless the program calls fsync(), which waits until the storage device confirms. Databases call fsync on commit for exactly this reason — which is why commit latency depends on storage latency.

02 · Picture it

1write() returnsdata copied into memory — fast2Page cache (RAM)dirty pages wait to be flushed3Filesystemmaps file offsets to disk blocks4Block layer + queuerequests wait for the device5Disk / SSDdurable only once fsync() returnswrite path: success at the top, durability only at the bottomA crash between write() and the flush loses data. fsync() waits for the bottom layer —that wait is why commit latency tracks disk latency.
1write() returnsdata copied into memory — fast2Page cache (RAM)dirty pages wait to be flushed3Filesystemmaps file offsets to diskblocks4Block layer + queuerequests wait for the device5Disk / SSDdurable only once fsync()returnswrite path: success at the top,durability only at the bottomA crash between write() and the flushloses data. fsync() waits for thebottom layer — that wait is why commitlatency tracks disk latency.
A write is fast because it stops in memory; durability needs fsync.

03 · In the interview

  • Explaining what a database commit actually guarantees, and what it costs.
  • Diagnosing slow disks: iostat latency and utilization, and I/O wait.
  • Explaining why the second read of a file is faster than the first.

04 · Practice question

“After a power loss, an application lost its last few seconds of data, even though every write returned success. Why?”

A strong answer covers

  1. A successful write() usually means the data reached the page cache, not the disk.
  2. Without fsync() (or equivalent options), dirty pages are flushed later, so a crash can lose them.
  3. Check whether the app or its database syncs on commit, and whether the storage has a volatile write cache.
  4. Trade-off: syncing every write adds latency, so batch or group commits where it’s safe.