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.
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
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
| Model | Behaviour | Example |
|---|---|---|
| Blocking | The call waits until the data is ready | read() on a socket in a thread-per-connection server |
| Non-blocking | Returns immediately with EAGAIN if not ready | O_NONBLOCK sockets |
| I/O multiplexing | Wait for any of many descriptors to become ready, then do non-blocking I/O | select, poll, epoll (Linux), kqueue (BSD/macOS) |
| Asynchronous | Submit the operation; get notified when it completes | io_uring, Windows IOCP |
"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.
"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
Because seeks dominate, disk scheduling reorders requests to minimise head movement:
| Algorithm | Idea | Note |
|---|---|---|
| FCFS | In arrival order | Fair but lots of seeking |
| SSTF | Nearest request next | Can starve far requests |
| SCAN (elevator) | Sweep one way serving requests, then reverse | Fair, predictable |
| C-SCAN | Sweep one way, jump back to the start | More uniform waiting times |
| LOOK / C-LOOK | Like SCAN but only as far as the last request | Practical 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:
| Level | Layout | Usable capacity | Survives | Good for |
|---|---|---|---|---|
| RAID 0 | Striping | 100% | No failures | Speed, scratch data |
| RAID 1 | Mirroring | 50% | 1 disk (per mirror) | Simple redundancy, read speed |
| RAID 5 | Striping + distributed parity | (n−1)/n | 1 disk | Capacity-efficient; slow small writes |
| RAID 6 | Double parity | (n−2)/n | 2 disks | Large arrays, long rebuilds |
| RAID 10 | Mirrors, then striped | 50% | 1 per mirror pair | Databases: 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
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 # benchmarkKey metrics: IOPS (operations per second), throughput (MB/s), latency (especially p99), and queue depth.
- 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.