I/O Systems & Disks

The kernel I/O stack, blocking vs non-blocking vs async I/O, epoll and io_uring, HDD and SSD internals, disk scheduling, and RAID.

Intermediate 4 min readLesson 14 of 16

I/O is usually the slowest part of any system. This lesson covers how the OS structures I/O, the programming models for waiting on it, and how storage devices actually behave. The hardware side (interrupts, DMA) is in I/O & interrupts.

The I/O stack

Applicationread() / write() / send()VFS + page cachefiles → pages, caching, read-aheadFile systemext4, XFS — files → block numbersBlock layerI/O scheduler, request merging, queuesDevice driverNVMe, SATA, virtioDeviceSSD / HDD / NIC

The page cache is the most important layer for performance: reads that hit it never touch the disk, and writes are absorbed and flushed in the background. Linux uses all free RAM for it, which is why free shows little "free" memory on a healthy system.

I/O models

ModelBehaviourExample
BlockingThe call waits until the data is readyread() on a socket in a thread-per-connection server
Non-blockingReturns immediately with EAGAIN if not readyO_NONBLOCK sockets
I/O multiplexingWait for any of many descriptors to become ready, then do non-blocking I/Oselect, poll, epoll (Linux), kqueue (BSD/macOS)
AsynchronousSubmit the operation; get notified when it completesio_uring, Windows IOCP
Readiness (epoll)

"Socket 7 is readable now" — you then call read yourself. Great for network sockets. Regular files are always "ready", so this doesn't help disk I/O.

Completion (io_uring, IOCP)

"Your read into buffer X finished, here are 4096 bytes". Works for files and sockets, batches syscalls via shared ring buffers, and can need almost no syscalls under load.

Event loops — Node.js (libuv), nginx, Redis, Netty, Python's asyncio — are built on epoll/kqueue/IOCP. A single thread can serve tens of thousands of connections because it never blocks: it waits for readiness on all of them at once. See The event loop.

Hard disk drives

Seekmove the arm: ~4–10 msRotationwait for the sector: ~2–4 msTransfer~200 MB/s
An HDD random read costs ~10 ms; sequential reads are ~100× faster per byte.

Because seeks dominate, disk scheduling reorders requests to minimise head movement:

AlgorithmIdeaNote
FCFSIn arrival orderFair but lots of seeking
SSTFNearest request nextCan starve far requests
SCAN (elevator)Sweep one way serving requests, then reverseFair, predictable
C-SCANSweep one way, jump back to the startMore uniform waiting times
LOOK / C-LOOKLike SCAN but only as far as the last requestPractical variants

Example: head at 53, requests 98, 183, 37, 122, 14, 124, 65, 67. FCFS moves 640 cylinders; SSTF moves 236.

Solid-state drives

SSDs have no moving parts: random reads take ~20–100 µs and NVMe drives sustain hundreds of thousands of IOPS. But flash has quirks:

  • Data is written in pages (4–16 KB), but can only be erased in large blocks (MBs). You can't overwrite a page in place.
  • The flash translation layer (FTL) remaps logical blocks to fresh pages and runs garbage collection to reclaim blocks, which causes write amplification.
  • Cells wear out after a limited number of program/erase cycles; wear levelling spreads writes.
  • TRIM tells the SSD which blocks are free so GC can skip copying dead data.

For SSDs, Linux uses simple schedulers (none, mq-deadline): reordering for seek time no longer matters, but deep parallel queues (NVMe supports 64K queues) do.

RAID

RAID combines disks for performance, redundancy, or both:

LevelLayoutUsable capacitySurvivesGood for
RAID 0Striping100%No failuresSpeed, scratch data
RAID 1Mirroring50%1 disk (per mirror)Simple redundancy, read speed
RAID 5Striping + distributed parity(n−1)/n1 diskCapacity-efficient; slow small writes
RAID 6Double parity(n−2)/n2 disksLarge arrays, long rebuilds
RAID 10Mirrors, then striped50%1 per mirror pairDatabases: fast and redundant

Parity uses XOR: with data blocks A and B, parity P = A ⊕ B, so a lost A = P ⊕ B.

Measuring I/O

Shell
iostat -x 1          # utilisation, queue depth, latency per device
iotop                # which processes are doing I/O
fio --name=randread --rw=randread --bs=4k --iodepth=32 --size=1G   # benchmark

Key metrics: IOPS (operations per second), throughput (MB/s), latency (especially p99), and queue depth.

Key takeaways
  • The I/O stack: app → VFS/page cache → file system → block layer → driver → device.
  • Blocking, non-blocking, multiplexed (epoll/kqueue) and asynchronous (io_uring/IOCP) models.
  • HDDs are dominated by seek and rotation; schedulers like SCAN reduce head movement.
  • SSDs use an FTL, garbage collection and wear levelling; sequential writes are still kinder.
  • RAID gives speed and/or redundancy, but isn't a backup.