File Systems, Storage & I/O
Disk and SSD characteristics, disk scheduling (FCFS, SSTF, SCAN, C-SCAN, LOOK) worked, file system internals (inodes, directories, links, journaling), RAID levels, the I/O stack, and I/O models — blocking, non-blocking, select/poll/epoll, io_uring, zero-copy.
The storage hierarchy
| Level | Typical latency | Typical size |
|---|---|---|
| CPU register | ~0.3 ns | bytes |
| L1 cache | ~1 ns | 32–64 KB / core |
| L2 cache | ~4 ns | 1–2 MB / core |
| L3 cache | ~15–40 ns | tens of MB |
| DRAM | ~80–100 ns | GBs–TBs |
| NVMe SSD (random read) | ~20–100 µs | TBs |
| SATA SSD | ~100 µs | TBs |
| HDD (random read) | ~5–10 ms | TBs |
| Network round trip (same DC) | ~0.5 ms | — |
Every factor of 1000 in that table explains a design decision: caches, page caches, buffer pools, batching, and sequential layouts.
Disks
HDD access time
access time = seek time + rotational latency + transfer time
rotational latency (avg) = ½ × (60 / RPM) s → 7200 RPM: ½ × 8.33 ms ≈ 4.17 ms
Random 4 KB reads on an HDD give ~100–200 IOPS; sequential reads stream at ~150–250 MB/s. Hence: sequential good, random bad.
SSD / NVMe
- No seek; random reads are fast (tens of µs), hugely parallel (NVMe: up to 64K queues × 64K commands).
- Writes go to erased pages; erasure happens per block (e.g. 256 pages). The flash translation layer remaps writes and runs garbage collection → write amplification and finite endurance (TBW / DWPD ratings).
TRIM/discardtells the SSD which blocks are free.
Disk scheduling (HDD)
Queue of cylinder requests: 98, 183, 37, 122, 14, 124, 65, 67; the head is at 53; cylinders 0–199.
| Algorithm | Order served | Total head movement |
|---|---|---|
| FCFS | 53→98→183→37→122→14→124→65→67 | 640 |
| SSTF (shortest seek time first) | 53→65→67→37→14→98→122→124→183 | 236 |
| SCAN (elevator), moving toward 0 | 53→37→14→0→65→67→98→122→124→183 | 236 |
| C-SCAN, moving toward 199 | 53→65→67→98→122→124→183→199→0→14→37 | 382 (counting the return sweep) |
| LOOK, moving toward 199 | 53→65→67→98→122→124→183→37→14 | 299 |
| C-LOOK, moving toward 199 | 53→65→67→98→122→124→183→14→37 | 322 (counting the jump 183→14) |
Calculations:
- FCFS: 45 + 85 + 146 + 85 + 108 + 110 + 59 + 2 = 640.
- SSTF: 12 + 2 + 30 + 23 + 84 + 24 + 2 + 59 = 236.
- SCAN (down): (53 − 0) + (183 − 0) = 236.
- C-SCAN (up): (199 − 53) + 199 + 37 = 382.
- LOOK (up): (183 − 53) + (183 − 14) = 299.
- C-LOOK (up): (183 − 53) + (183 − 14) + (37 − 14) = 322.
Trade-offs: SSTF can starve far requests; SCAN/C-SCAN give bounded waits, C-SCAN more uniform wait times. Modern Linux uses mq-deadline, bfq, kyber, or none (best for NVMe, where the device schedules internally).
File systems
What a file system provides
Files (named byte sequences), directories (a hierarchy of names), metadata and permissions, space allocation, and crash consistency.
On-disk layout (Unix-style, e.g. ext4)
Maximum file size with 4 KB blocks and 4-byte pointers (classic ext2/3): 12 direct + 1024 + 1024² + 1024³ blocks ≈ 4 TB (ext4 extents go beyond).
Path lookup
open("/home/ana/notes.txt") → read the root inode → read its directory data to find home → its inode → … → notes.txt → inode 4711 → check permissions → create an open file description (offset, flags) → return the lowest free file descriptor in the process's fd table. The kernel caches lookups in the dentry cache.
Allocation methods
| Method | Idea | Pros | Cons |
|---|---|---|---|
| Contiguous | file = start + length | fast sequential/random | external fragmentation, hard to grow |
| Linked (FAT) | each block points to the next (table in FAT) | no fragmentation | slow random access |
| Indexed (inode) | index block(s) list data blocks | fast random access | index overhead |
| Extents (ext4, XFS, NTFS) | runs of contiguous blocks | compact, fast | — |
Links
- Hard link — another name for the same inode (
ln a b). - Symbolic link — a file containing a path (
ln -s a b).
Crash consistency
| Approach | How | Examples |
|---|---|---|
| fsck | scan and repair after a crash | ext2 |
| Journaling (WAL) | log changes, then apply; replay on recovery | ext4, XFS, NTFS |
| Copy-on-write | never overwrite; atomically switch a root pointer | ZFS, Btrfs, APFS |
| Log-structured | the whole FS is an append-only log | F2FS (flash), LFS |
RAID
| Level | Layout | Usable capacity (N disks) | Fault tolerance | Notes |
|---|---|---|---|---|
| 0 | striping | N | none | speed only |
| 1 | mirroring | 1 disk | N − 1 | fast reads |
| 5 | striping + 1 distributed parity | N − 1 | 1 disk | write penalty; risky rebuilds |
| 6 | striping + 2 parities | N − 2 | 2 disks | bigger write penalty |
| 10 | stripe of mirrors | N / 2 | 1 per mirror pair | databases' favourite |
Parity in RAID 5 is XOR: P = D1 ⊕ D2 ⊕ D3; a lost block is the XOR of the survivors. RAID ≠ backup.
The I/O stack
application → libc buffering → syscall (read/write) → VFS → page cache
→ file system (ext4/XFS) → block layer + I/O scheduler → device driver
→ (DMA) device; completion interrupt
- Programmed I/O vs interrupts vs DMA: DMA lets the device copy data to/from RAM without the CPU, interrupting only on completion.
- Buffering smooths speed mismatches; caching keeps hot data in RAM; spooling queues output (printers).
I/O models (how servers wait)
select, poll, epoll
select | poll | epoll (Linux) / kqueue (BSD) | |
|---|---|---|---|
| fd limit | FD_SETSIZE (1024) | none | none |
| Per-call cost | O(all fds) copy + scan | O(all fds) | O(ready fds) |
| Interest set | passed every call | passed every call | registered once in the kernel |
| Triggering | level | level | level or edge |
int ep = epoll_create1(0);
struct epoll_event ev = { .events = EPOLLIN, .data.fd = listen_fd };
epoll_ctl(ep, EPOLL_CTL_ADD, listen_fd, &ev);
struct epoll_event events[1024];
for (;;) {
int n = epoll_wait(ep, events, 1024, -1); // block until something is ready
for (int i = 0; i < n; i++) {
int fd = events[i].data.fd;
if (fd == listen_fd) {
int c = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
struct epoll_event cev = { .events = EPOLLIN | EPOLLET, .data.fd = c };
epoll_ctl(ep, EPOLL_CTL_ADD, c, &cev);
} else {
handle_readable(fd); // edge-triggered: read until EAGAIN
}
}
}
import selectors, socket
sel = selectors.DefaultSelector() # epoll on Linux, kqueue on macOS
srv = socket.socket(); srv.bind(("0.0.0.0", 8080)); srv.listen(); srv.setblocking(False)
sel.register(srv, selectors.EVENT_READ)
while True:
for key, _ in sel.select():
if key.fileobj is srv:
conn, _ = srv.accept(); conn.setblocking(False)
sel.register(conn, selectors.EVENT_READ)
else:
data = key.fileobj.recv(4096)
if data:
key.fileobj.send(data) # echo
else:
sel.unregister(key.fileobj); key.fileobj.close()
io_uring
Linux's modern async interface: two ring buffers shared with the kernel — a submission queue and a completion queue. Applications batch many operations (reads, writes, accepts, sends, even fsync) with few or zero syscalls (with SQ polling). It supports files and sockets, unlike older Linux AIO. Used by high-performance databases and runtimes (TigerBeetle, ScyllaDB/Seastar, Tokio's tokio-uring). Because of a history of security bugs, some environments (Android, ChromeOS, some container runtimes) restrict it.
Zero-copy
| Technique | Avoids |
|---|---|
sendfile, splice | copying file data through user space when sending to a socket |
mmap | the copy from page cache to a user buffer |
MSG_ZEROCOPY | copying send buffers into kernel memory |
| DMA + scatter-gather | CPU copies to the NIC |
| Kernel bypass (DPDK, RDMA) | the kernel network stack entirely |
Kafka's throughput comes largely from append-only sequential writes + page cache + sendfile to consumers.
Useful tools
iostat -x (device utilisation, await), vmstat (paging, run queue), iotop, lsof (open files, deleted-but-open), strace -e trace=file, df -i (inode exhaustion!), blktrace, perf, and BPF tools (biolatency, ext4slower).
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.