Skip to content

Lesson 9 of 13 · 15m

I/O Latency and the fsync Tax

Buffered writes lie, `fsync` is the real cost, and `iostat` await plus `biolatency` histograms show what averages hide.

A write() returns after copying into the page cache. The data reaches the device later, when flusher threads write back dirty pages — bounded by vm.dirty_ratio/dirty_bytes and the background thresholds. That is why "we wrote it, so it's safe" is false, and it is also why storage latency is invisible when you time the write call: the cost shows up in reads that miss cache, in fsync, and in writeback stalls when the dirty thresholds trip.

The durability tax

fsync is the syscall that pays for durability — it blocks until the device confirms the data. On NVMe that is tens to hundreds of microseconds; on spinning disks, single-digit milliseconds. Call it per record and you have converted asynchronous writeback into synchronous storage latency inside the request path.

Every database solves this identically: group commit. PostgreSQL batches WAL flushes (commit_delay and commit_siblings exist deliberately to trade a little latency for batching), and InnoDB does the same for redo. The alternative — one fsync per commit — caps throughput at 1 / fsync_latency regardless of how fast the CPU is. fdatasync skips the inode metadata update when file size is unchanged, and it is cheaper. O_DSYNC at open time makes every write durable but keeps you paying per write. The real decision is where the durability boundary sits: per request, per batch, or trust writeback and accept the loss window on power failure.

Reading the disk

iostat -xz 1 is the baseline. r_await and w_await give average service time per operation, aqu-sz the average queue depth, %util the fraction of time the device had work outstanding. That last number lies on modern hardware — an NVMe device with deep parallel queues reports 100% util long before saturation. Rising await is the saturation signal, not util.

biolatency-bpfcc prints a per-device histogram of block I/O latency via eBPF — it shows the distribution that iostat's averages hide. A bimodal graph with a fast hump (cache, healthy NVMe) and a slow hump (retries, queueing, a degrading disk) is a diagnosis in one screen. biosnoop-bpfcc shows per-operation latency, opensnoop-bpfcc shows file churn, iotop -o shows who is doing it.

The read side and the direct path

Reads hit the same cache from the other direction: a read() of a cached page is a memcpy, not a disk access — which is why I/O wait only appears on cache misses and first reads. O_DIRECT bypasses the page cache deliberately for engines that do their own caching (databases, VM disk images); it trades the kernel's readahead and write coalescing for predictability, and used casually it makes everything slower while looking honest.

/proc/pressure/io gives the same PSI view as CPU — some/full stall averages for threads blocked on storage — and ioping is a device-level latency probe for a number before the application is involved. Queue tuning (nr_requests, the mq-deadline/none elevator choice on NVMe) comes last: it moves latency by microseconds, while fsync storms move it by milliseconds.

Evidence

The classic application shape: p99 latency tracking a log statement that writes then fsyncs per line — fixed by buffering, not faster disks. At kernel scale, the multiqueue blk-mq rework exists because a single block queue saturated long before the SSDs behind it did; the legacy single-queue path was removed entirely in kernel 5.0.

The practice

  • iostat -xz 1: await and aqu-sz before %util; 100% util on NVMe is a busy sign, not a full sign.
  • biolatency-bpfcc for the distribution — bimodality beats averages.
  • Count fsync/fdatasync frequency with strace -c; batch deliberately, not by accident.
  • opensnoop finds path churn — the stat/open storm masquerading as disk slowness.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.