Operating systems for backend engineers series: 1. Kernel, processes and threads · 2. Virtual memory and the page cache · 3. I/O models and epoll · 4. File systems and the shell
Most of a backend request’s life is spent waiting for the database, Redis, another API or the disk. How your program waits decides whether a server handles 200 connections or 50,000. After this post you can explain epoll, Tomcat, Netty, virtual threads, fsync and “Too many open files”. Read part 1 (system calls, context switches) and part 2 (the page cache) first.
1. The layers an I/O operation goes through
Key idea: I/O means exchanging data with the outside world (disks, the network, peripherals) through the kernel. The expensive part is not the system call but waiting for the device.
Reading or writing RAM is not I/O, because the CPU accesses memory directly. A read goes through six steps:
- The application needs data, say
in.read(buf). - The library issues a system call (
read,recv) and the CPU switches to kernel mode. - The kernel tells the device what to do through its driver.
- The data lands in RAM via DMA (Direct Memory Access): the device controller copies it itself, so the CPU doesn’t move bytes one by one.
- When it’s done, the device raises an interrupt (a hardware signal); the kernel wakes up the waiting thread.
- The kernel copies the data from its own buffer into the application’s buffer and the system call returns.
sequenceDiagram
participant App as Application thread
participant K as Kernel
participant D as Device
App->>K: read() - enters kernel mode
K->>D: command via the driver
Note over App,K: thread waits, CPU runs other work
D-->>K: DMA into RAM, then an interrupt
K-->>App: copy into app buffer, read() returns
A system call costs well under a microsecond to a few microseconds; waiting for the device takes thousands of times longer: a random HDD read takes 5–10 ms, a round trip inside a datacenter 0.1–0.5 ms. The question for the rest of the post is: what is your thread doing while it waits?
2. Four groups of bottlenecks and how to measure them
Key idea: bottlenecks fall into CPU, memory, disk or network; measure with each group’s tools before guessing.
| Group | Typical causes | Measure with (Linux) |
|---|---|---|
| CPU | heavy computation, GC, too many context switches | top, vmstat 1 (columns r, cs), pidstat -w 1 |
| Memory | not enough RAM, swapping | free -m, vmstat 1 (si, so), dmesg for the OOM killer |
| Disk | IOPS, random I/O, fragmentation | iostat -x 1 (r_await, w_await, aqu-sz) |
| Network | bandwidth, I/O model, connections | ss -s, ss -tan, sar -n DEV 1, nstat (retransmits) |
Reading iowait, %util and thread dumps correctly
On the JVM, add a thread dump (jcmd <pid> Thread.print): 180 of 200 threads parked in NioSocketImpl.read means you are waiting on a dependency.
3. The five Unix I/O models
Key idea: a network read has two phases: waiting for data to reach the kernel, then copying it into the application. The I/O models differ in which phase blocks the thread.
| Model | Phase 1: waiting for data | Phase 2: copy | Example |
|---|---|---|---|
| Blocking | blocks | blocks | InputStream.read(), JDBC |
| Non-blocking | returns EAGAIN at once, you poll again |
blocks (briefly) | socket with O_NONBLOCK |
| Multiplexing | blocks in select/poll/epoll_wait for many fds |
blocks (briefly) | Java NIO, Netty, Nginx, Redis |
| Signal-driven | kernel sends SIGIO |
blocks (briefly) | rarely used |
| Asynchronous | doesn’t block | doesn’t block, kernel notifies after copying | io_uring, Windows IOCP |
The first four are all synchronous in POSIX terms: during the copy the thread still waits. The slides draw “synchronous non-blocking” and “asynchronous”; keep the two words apart:
- Non-blocking: the call returns immediately, possibly with “nothing yet”.
- Asynchronous: the result arrives later through a callback or a completion queue (a queue of “done” notices).
4. The two Java snippets in the slides
Key idea: both slide examples are wrong: the “blocking” one costs a system call per byte, the “non-blocking” one is an async API stuck in a busy-wait.
The slides illustrate blocking I/O with a FileInputStream loop:
while ((content = fis.read()) != -1) {
System.out.print((char) content);
}
The bigger problem than blocking: there’s no buffer, so every byte is a system call. On top of that, casting a byte to char is wrong for UTF-8, and there’s no try-with-resources. Exercise 1 measures the damage.
The slides’ AsynchronousFileChannel snippet and why files have no non-blocking mode
The slides illustrate non-blocking I/O with AsynchronousFileChannel:
Future<Integer> result = fileChannel.read(buffer, 0);
while (!result.isDone()) {
System.out.println("Doing something else while reading...");
}
Three corrections:
- This is an async API returning a
Future, not non-blocking I/O. - The
whileloop is a busy-wait (a loop that keeps asking): it burns a whole core and prints thousands of lines, each one another system call. If you have other work, do it and then callresult.get(), or pass aCompletionHandler. - On Linux and macOS,
AsynchronousFileChannelis emulated with a thread pool running blockingpreadcalls; only Windows uses real overlapped I/O.
5. How many connections can one server handle?
Key idea: the real limits are file descriptors, memory and the threading model, not the number of ports. For traditional Java, “one thread per connection” hits the ceiling first.
- File descriptors (fds): every socket is an fd. The soft
ulimit -nis often 1024 in a shell; systemd services takeLimitNOFILE. - Memory: an idle TCP connection costs a few to a few dozen KB. C10K (ten thousand connections) is an old, solved problem.
- Threading model: with a pool of 200 threads, request 201 queues even while the CPU sits idle.
A system that lives on long-lived connections: the WebSocket gateway holding 10 million concurrent connections in Designing a Discord-style chat system.
Details on fd limits, TCP buffers and ephemeral ports
File descriptors. Containers inherit the runtime’s settings, usually very high; system-wide there are fs.file-max and fs.nr_open. HotSpot raises the soft limit to the hard limit at startup by default (-XX:+MaxFDLimit), so for a JVM the number that matters is usually the hard limit.
Memory. Each TCP connection has kernel send/receive buffers (tcp_rmem/tcp_wmem, auto-tuned) plus application buffers.
Threading. “One connection, one thread” makes a thread wait on I/O, parse, and run business logic. Creating and destroying threads is expensive, so we pool them, and the pool becomes the ceiling.
6. select, poll and epoll
Key idea: multiplexing means one thread asks the kernel “which of these N sockets has data?”. epoll beats select/poll because it registers once and returns only the ready fds.
select: a bitmap of at most 1024 fds (FD_SETSIZE); every call copies the whole set into the kernel and scans all N fds: O(N).poll: drops the 1024 limit but is still O(N) per call.epollseparates registration from waiting:epoll_create1creates an epoll instance (itself an fd).epoll_ctladds or removes a socket from the interest set (stored in a red-black tree), once per connection.- When data arrives, a kernel callback puts the socket on the ready list.
epoll_waitjust copies the ready list out: the cost scales with the number of ready fds.
flowchart LR
B[epoll_ctl ADD socket] --> T[Interest set - red-black tree]
N[NIC receives a packet] -->|callback| R[Ready list]
T -.-> R
R -->|epoll_wait| L[Event loop handles ready fds]
macOS/BSD use kqueue, Windows uses IOCP (a completion model).
Java’s Selector on each OS, and level- vs edge-triggered
7. From epoll to Tomcat, Netty and virtual threads
Key idea: all three sit on epoll and differ in which thread runs your code. Tomcat uses a worker pool, Netty runs handlers on the event loop, and virtual threads let the runtime multiplex for you.
Tomcat’s NIO connector (Spring Boot’s default):
- An Acceptor thread accepts connections.
- A Poller thread keeps a
Selectorover every open connection, including idle keep-alive ones. - When a request arrives, the worker pool (
server.tomcat.threads.max, default 200) runs the servlet in blocking style.
So Tomcat can hold 8,192 connections (max-connections) but only processes 200 requests concurrently.
Netty (under WebFlux and gRPC Java): a few event loops, each a single thread with its own Selector, serving thousands of channels. Handlers run right on the event loop, so you must never block it. This is the Reactor pattern: an event loop waits for fds to become ready and dispatches events to handlers. Don’t confuse it with the Actor model, where each actor owns its state and a mailbox and communicates only by messages (Akka, Erlang). gRPC Java runs HTTP/2 on Netty; choosing REST or gRPC between services is covered in Communication in system design: REST, gRPC, HTTP/2.
Virtual threads (Java 21) keep blocking-style code. When a virtual thread reads from a socket with no data, the JDK:
- puts the socket in non-blocking mode and registers the fd with an internal poller (epoll on Linux);
- parks the virtual thread (sets it aside) and gives the carrier thread back to the scheduler;
- when epoll reports the socket ready, the poller unparks it and it resumes on any carrier.
Two exceptions: file I/O has no non-blocking mode, so it occupies the carrier (the scheduler compensates with a temporary extra carrier), and on Java 21 I/O inside synchronized pins the carrier (fixed in Java 24, JEP 491). Pinning details are in ExecutorService and virtual threads.
# Spring Boot 3.2+: run Tomcat's workers on virtual threads
spring.threads.virtual.enabled=true
The 200-worker ceiling disappears, but it moves to the connection pool (HikariCP defaults to 10): back-pressure (slowing intake when overloaded) in the right place, as long as acquiring a connection has a timeout. How to size the pool and set connectionTimeout is in Connection pooling and HikariCP.
8. Disks: sequential, random, the page cache and fsync
Key idea: sequential reads are far faster than random ones, which is why write-heavy systems append. write() only reaches the page cache; to survive a power loss you need fsync.
The slides say sequential is 3–4× faster than random; in reality the gap is hundreds of times on an HDD and still tens of times for one-at-a-time reads on NVMe. That’s why Kafka, PostgreSQL’s WAL, InnoDB’s redo log and LSM trees are all append-only (see Why Kafka is fast).
- Reads go through the page cache: a hit is just a memory copy.
- Writes:
write()copies into the page cache, marks the pages dirty (not yet on disk) and returns. The kernel flushes after about 30 seconds (or when dirty pages exceedvm.dirty_background_ratio); lose power before that and data that was “successfully written” is gone. fsync(in JavaFileChannel.force(true), or open withStandardOpenOption.DSYNC) forces data to disk. It costs from under 1 ms (NVMe with power-loss protection) to tens of ms (HDD), so databases batch many transactions into one fsync (group commit).
IOPS for HDD, SSD and NVMe, and the fsyncgate story
The slides also call “disks are slow” a misconception: that’s only half true.
| Device | Random 4 KB reads | Sequential reads |
|---|---|---|
| 7,200 rpm HDD | ~100–200 IOPS (~0.5–1 MB/s) | ~150–250 MB/s |
| SATA SSD | ~50–100k IOPS (deep queue) | ~500 MB/s |
| NVMe SSD | hundreds of thousands of IOPS (deep queue); ~10–20k at queue depth 1 | 3–7 GB/s |
Sequential still wins on SSDs because there are fewer requests and readahead (the kernel fetches the next chunk early).
9. File descriptors and “Too many open files”
Key idea: a file descriptor is the integer the kernel hands out for everything a process opens. “Too many open files” (EMFILE) has two very different causes: a limit too low for the real load, or an fd leak.
On Linux everything is a file: regular files, sockets, pipes, devices, even epoll. An fd indexes the process’s fd table (0, 1, 2 are stdin, stdout, stderr). Open fds live under /proc/<pid>/fd or show up in lsof -p <pid>; inodes and links are in part 4.
Look at the fd count’s history: rising and falling with load means a low limit, a steady climb under constant load means a leak.
Exercises
Run with JDK 21 on macOS, in a scratch directory outside the repo.
Exercise 1: Buffered vs unbuffered reads
Task: create a ~20 MB file, read it three ways (byte by byte unbuffered, byte by byte through BufferedInputStream, in 64 KB chunks), compare the timings and explain them.
Result: unbuffered ~7.4 seconds, BufferedInputStream ~100 ms, chunked ~9 ms; the gap comes from the number of system calls, not the disk.
Exercise 1 solution: code and timings
static long unbuffered(Path file) throws IOException {
long sum = 0;
try (InputStream in = new FileInputStream(file.toFile())) {
int b;
while ((b = in.read()) != -1) sum += b; // one system call per byte
}
return sum;
}
static long chunked(Path file) throws IOException {
long sum = 0;
byte[] chunk = new byte[64 * 1024];
try (InputStream in = Files.newInputStream(file)) {
for (int n; (n = in.read(chunk)) != -1; )
for (int i = 0; i < n; i++) sum += chunk[i] & 0xff;
}
return sum;
}
The second variant is unbuffered wrapped in new BufferedInputStream(..., 64 * 1024). main creates the file with new Random(42).nextBytes(...), runs each variant twice and times it with System.nanoTime().
Results (identical checksums): unbuffered 7,244 and 7,523 ms; BufferedInputStream 104 and 102 ms; chunked 9 and 8 ms. That’s 20 million system calls versus ~320. BufferedInputStream is still slower than chunked reads because every byte is still a method call that takes an internal lock. A serious benchmark would use JMH; here the gap is large enough not to need it.
Exercise 2: A single-threaded echo server with an NIO Selector
Task: an echo server that serves many connections with one thread, tested with 200 concurrent clients.
Exercise 2 solution: EchoServer and test run
public class EchoServer {
public static void main(String[] args) throws IOException {
try (Selector selector = Selector.open();
ServerSocketChannel server = ServerSocketChannel.open()) {
server.bind(new InetSocketAddress("127.0.0.1", 9090));
server.configureBlocking(false); // required before register
server.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("selector = " + selector.getClass().getSimpleName());
while (true) {
selector.select(); // epoll_wait or kevent
Iterator<SelectionKey> it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
it.remove(); // forget this and old keys are handled again
if (key.isAcceptable()) {
SocketChannel c = server.accept();
c.configureBlocking(false);
c.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(1024));
} else if (key.isReadable()) {
echo(key); // full version: catch IOException, cancel key, close channel
}
}
}
}
}
static void echo(SelectionKey key) throws IOException {
SocketChannel c = (SocketChannel) key.channel();
ByteBuffer buf = (ByteBuffer) key.attachment();
if (c.read(buf) == -1) { key.cancel(); c.close(); return; } // client closed
buf.flip();
c.write(buf); // demo: assume it all fits in one write
buf.compact();
}
}
A client opened 200 sockets at once, sent hello-i on each and read it back: 200/200 echoed correctly by one server thread; printf 'ping\n' | nc -w 1 127.0.0.1 9090 printed ping. On macOS the program prints KQueueSelectorImpl; on Linux it would be EPollSelectorImpl.
What’s simplified: write may not write everything when the send buffer is full (keep the remainder and register OP_WRITE), and heavy work must leave the selector thread. Netty does all of this for you.
Exercise 3: Create and diagnose a file descriptor leak
Task: write code that leaks fds, run it with a low limit, and diagnose it with OS tools.
Exercise 3 solution: leaking code, lsof and the fix
static final List<Object> keepAlive = new ArrayList<>(); // simulates readers that are never closed
static String readFirstLineLeaky(Path p) throws IOException {
BufferedReader r = Files.newBufferedReader(p); // BUG: no try-with-resources
keepAlive.add(r);
return r.readLine();
}
// main: call readFirstLineLeaky in an endless loop, catch IOException, print the count, sleep 10 s
Run with ulimit -n 1024 and java -XX:-MaxFDLimit (so the JVM doesn’t raise the soft limit itself):
failed after 1020 reads: java.nio.file.FileSystemException: config.txt: Too many open files
$ lsof -p <pid> | awk 'NR>1 {print $5, $NF}' | sort | uniq -c | sort -rn | head -2
1020 REG /.../config.txt
2 REG /.../leak.log
The first run, without -XX:-MaxFDLimit, opened over 61,000 readers before failing, because the JVM had raised the soft limit to the hard limit, as section 5 says. On Linux, also use ls -l /proc/<pid>/fd and cat /proc/<pid>/limits.
The fix: try (BufferedReader r = Files.newBufferedReader(p)) { return r.readLine(); } plus an alert on the open-fd metric. In real life an unclosed reader usually waits for GC (the JDK closes the fd through a Cleaner when it’s collected), so the fd count rises and falls with GC cycles; never rely on GC to close resources.
Remember
- Explain the two phases of a network read and which phase each I/O model blocks.
- Explain why
epollscales better thanselect/poll, and why regular files can’t use epoll. - Know what happens to Tomcat’s request 201 and connection 9,000, and where virtual threads move that ceiling.
- Know that a successful
write()isn’t durable yet, and the three ways to trade fsync against throughput. - Tell a low fd limit apart from an fd leak, and know what to measure.
Interview questions
Junior
Is reading RAM I/O? What is DMA?
No: the CPU accesses RAM directly, while I/O is exchanging data with the outside through the kernel and drivers.
I/O means talking to disks, the network or peripherals. DMA lets a device copy data into RAM without the CPU, then signal completion with an interrupt.
What is a file descriptor? What does “Too many open files” mean?
An fd is an integer the kernel returns when a process opens a file, socket, pipe or device; the error means the process hit its fd limit.
The limit is set by ulimit -n or LimitNOFILE. You hit it because the limit is low or because fds are leaking. An fd count that only ever grows is a leak.
Why wrap a FileInputStream in a BufferedInputStream?
Because FileInputStream.read() is unbuffered, so every call is a system call.
Reading 20 MB byte by byte takes seconds, with a 64 KB buffer about 100 ms. Add try-with-resources so the stream is always closed.
Mid
Explain blocking, non-blocking, multiplexed and asynchronous I/O.
They differ in which phase blocks the thread: waiting for data, or copying it from kernel to user space.
Blocking blocks in both. Non-blocking returns EAGAIN if there’s no data yet. Multiplexing lets one thread wait on many fds. Asynchronous (io_uring, IOCP) blocks in neither. The first three are still synchronous because the thread waits during the copy.
Why does epoll scale better than select and poll?
Because epoll registers each fd once and epoll_wait costs only as much as the ready fds, while select/poll scan all N fds on every call.
select/poll take the whole fd set on every call and both the kernel and the application scan all N fds: O(N), and select is capped at 1,024. epoll registers with epoll_ctl (a red-black tree), and the kernel moves fds onto a ready list via callbacks when data arrives.
Is a server listening on port 8080 limited to 65,535 connections?
No. A connection is identified by the source and destination IP/port four-tuple, so the real limits are fds, memory and the threading model.
The port limit only affects outbound connections to the same destination (~28,000 source ports, 60-second TIME_WAIT), which is why you need connection pooling and keep-alive.
Senior
Tomcat has 200 threads by default. What happens to request 201 and connection 9,000?
Request 201 is accepted but waits for a free worker; connection 9,000 exceeds max-connections (8,192), so it waits in the backlog and is refused once that fills.
The NIO connector separates holding connections from processing them: the Poller holds up to 8,192 connections, the worker pool (200) runs servlets. Beyond 8,192 connections, new ones wait in the accept-count backlog (100); once that fills, clients time out or are refused. Virtual threads remove the 200 ceiling, but it moves to the connection pool, which needs a timeout.
How do virtual threads run blocking code on top of epoll?
The JDK turns a blocking read into: register the fd with epoll, park the virtual thread, unpark it when the socket is ready.
When a virtual thread reads a socket with no data, the JDK switches the socket to non-blocking, registers the fd with an internal poller (epoll on Linux), parks the virtual thread and unmounts it from its carrier. When epoll reports readiness, the poller unparks it and it resumes on any carrier, with its stack kept on the heap. Exceptions: file I/O occupies the carrier (the scheduler compensates with a temporary carrier), and on Java 21 I/O inside synchronized pins (fixed in Java 24).
write() returned successfully, then the machine lost power. Is the data there?
Maybe not: write() only copies into the page cache and the kernel flushes later.
Durability needs fsync (FileChannel.force(true)). Databases fsync every commit with group commit, or relax to once a second and accept losing up to a second; Kafka relies on replication.
A service throws “Too many open files” at 3 a.m. How do you investigate?
Check the limit and the current fd count, use the metric’s history to tell a low limit from a leak, then find the source with lsof.
- Check the limit (
cat /proc/<pid>/limits) and the open fd count (ls /proc/<pid>/fd | wc -l). - The fd metric’s history: rising with load and falling back means the limit is too low, a steady climb means a leak.
- Group fds with
lsof -p <pid>andss -tanp: many handles to the same path mean unclosed streams; many CLOSE_WAIT sockets to one host mean unclosed response bodies. - Fix the root cause in code and alert when fds exceed 80% of the limit.
Useful phrases for interviews
- “The thread isn’t burning CPU, it’s parked waiting on I/O, so adding cores won’t help.”
- “An unbuffered read is one system call per byte, which is why buffering gives a hundredfold speed-up.”
- “epoll’s cost scales with the number of ready sockets, not the number of open ones.”
- “Tomcat holds thousands of connections with one poller, but only processes as many requests as it has worker threads.”
- “Never block the event loop; push blocking work onto a separate scheduler.”
- “With virtual threads the ceiling moves from the thread pool to the database connection pool.”
- “A successful
write()only means the data reached the page cache; durability needs an fsync or replication.” - “If the fd count keeps climbing under steady load, it’s a leak, and raising the ulimit just buys time.”
Notes based on the Advanced Backend course (Ronin Engineer), rewritten in my own words.