Why this lab exists
Every server you have ever deployed runs one of four I/O models, and the model it chose determines whether it falls over at 1,000 connections or 500,000. nginx serves millions of concurrent connections on a single thread. Redis processes 100,000 commands per second without a single mutex. Java’s Spring WebFlux handles 150,000 requests per second with annotated controller methods. None of this is magic — it is a specific answer to the question: how does my process know which of its 50,000 open sockets has data ready to read?
Until you have implemented all four answers and measured where each one breaks, you are guessing. You have read that “epoll is O(1) while select is O(n)” but you have never seen what that means in allocations, syscall counts, or CPU profiles. You have been told to use async servers but you do not know what the kernel is actually doing differently. This lab makes all of it concrete.
We trace a TCP connection from NIC interrupt to your application code, implement four progressively better servers in C, and end by understanding exactly how Netty, Spring WebFlux, and Java 21 Virtual Threads map to the same four models.
What we are building
Four C servers that all respond with “hello” to any HTTP request, implementing progressively more sophisticated I/O models.
We build:
- v0: blocking single-thread server — one accept, one process, repeat
- v1: thread-per-connection server — one pthread per connection, bounded by a semaphore pool
- v2: epoll event loop — edge-triggered, single-threaded, drain-loop pattern
- v3: io_uring server — submission/completion rings, SQPOLL mode, zero-syscall hot path
- A Java module showing Netty’s boss/worker groups and Java 21 Virtual Threads
We explicitly skip (and why):
- TLS — OpenSSL integration triples the code size; the lessons here are about concurrency, not encryption
- HTTP/2 — requires HPACK and stream multiplexing; the I/O model lessons are orthogonal
- Kernel modules — we stay in userspace; TUN/TAP is covered in the tcp-stack lab
- Multiple listening ports or virtual hosting — one port, one model per binary
Mental model
The two-queue model
Before any server code runs, the kernel has already done significant work on your behalf. Understanding this is the prerequisite for understanding all four I/O models.
When a client initiates a TCP connection to your server, the kernel manages the handshake entirely without waking your process. Here is what happens step by step:
Client Kernel (your machine) Application
│ │ │
│─── SYN ──────────────────────▶│ │
│ │ Add to SYN queue │
│ │ (half-open, SYN_RCVD) │
│◀── SYN-ACK ───────────────────│ │
│ │ │
│─── ACK ──────────────────────▶│ │
│ │ Move to accept queue │
│ │ (fully established) │
│ │ │
│ │◀──── accept() ────────────│
│ │ Dequeue → return fd │
│ │ │
│◀══════════ application data ══════════════════════════════│
SYN queue (also called the incomplete connection queue): holds connections that have sent SYN and received SYN-ACK but have not yet sent the final ACK. Each entry is a lightweight structure; the queue depth is controlled separately from listen(backlog).
Accept queue (also called the complete connection queue): holds fully established connections waiting for your application to call accept(). This is the queue that listen(fd, backlog) sizes. When your application is slow to call accept(), this queue fills up.
What happens when the accept queue is full: When the final ACK arrives from a client and the accept queue is already at capacity, the kernel silently drops the ACK. It does not send a RST. The client sees no error and waits. After its retransmit timeout (typically 1 second on the first retry), the client retransmits the ACK. Eventually the client either gets accepted or times out after multiple retransmits. This silent-drop behavior is why listen(backlog) overflows produce mysterious latency spikes rather than immediate connection errors.
The four I/O models in this lab differ entirely in how fast and how many connections can be dequeued from the accept queue, and what happens after dequeue.
The C10k problem
In 1999, Dan Kegel posed the C10k problem: how do you serve 10,000 simultaneous clients? At the time, the dominant model was thread-per-connection. At 8 MB per thread, 10,000 threads require 80 GB of virtual memory — more than any machine had. The answer was event-driven servers (epoll/kqueue), which is why nginx was written as an event loop from day one (2004) while Apache’s threaded model has been declining ever since.
Today the problem has shifted to C10M (10 million connections), which is where io_uring’s zero-syscall model becomes relevant.
Prerequisites
- Basic C (pointers, structs, file descriptors)
- How TCP connections work at the state-machine level (covered in the tcp-stack lab)
- How HTTP/1.1 request/response works (covered in the http-server lab)
- Linux system calls:
socket(),bind(),listen(),accept(),recv(),send()
Stage-by-stage build
Stage 0 Blocking accept loop + SYN/accept queue anatomy
- Implement socket() → bind() → listen(BACKLOG=128) → accept loop → recv → send → close
- Add print_queue_stats() that reads /proc/net/tcp to show recv-Q on the listening socket
- Observe: start the server, open 200 connections without accept()ing them, watch /proc/net/tcp show recv-Q hitting backlog
The simplest possible server. Every new connection waits in the kernel’s accept queue until we finish processing the previous one.
int main(void) {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = INADDR_ANY,
};
bind(server_fd, (struct sockaddr *)&addr, sizeof(addr));
/*
* listen(fd, BACKLOG) sets the depth of the accept queue.
* The kernel caps this at /proc/sys/net/core/somaxconn (default 4096).
* When accept queue is full: new ACKs are silently dropped.
* The client retransmits after RTO — you see mysterious 1-second latency.
*/
listen(server_fd, BACKLOG);
while (1) {
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &len);
handle_connection(client_fd, &client_addr);
/* Next accept() only called AFTER handle_connection() returns.
During handle_connection(), all incoming connections queue in kernel. */
}
}Reading /proc/net/tcp: The recv-Q column for a socket in state 0x0A (LISTEN) shows how many connections are waiting in the accept queue right now. Run cat /proc/net/tcp and look for your port (in hex). recv-Q > 0 on a listening socket means your application is not calling accept() fast enough.
The key observation: start v0, then run for i in $(seq 200); do curl -s http://localhost:8080/ & done. Watch the second curl invocations stall. They are sitting in the accept queue, fully established at the TCP level, waiting for your single-threaded server to finish the first request.
Benchmark baseline: Our v0 achieves 1,200 req/s on loopback — entirely limited by sequential processing. The accept queue is not the bottleneck; the single-threaded processing is.
Stage 1 Thread-per-connection: the C10k wall
- Spawn a pthread for each accepted connection; pass the fd as argument
- Use pthread_attr_setstacksize to print and control the 8 MB default stack
- Add a semaphore pool capped at MAX_THREADS=1000; observe sem_wait() becoming the new bottleneck
- Benchmark at 100, 500, 1000, 2000 concurrent connections — find the collapse point
Thread-per-connection is the intuitive solution: spawn a thread, give it the connection, let the OS schedule everything.
#define MAX_THREADS 1000
static sem_t thread_slots; /* counts available slots */
/* Accept loop */
while (1) {
int client_fd = accept(server_fd, ...);
/* Block here when MAX_THREADS threads are active.
This is the pool's backpressure — the accept queue becomes the wait list. */
sem_wait(&thread_slots);
conn_arg_t *arg = malloc(sizeof(*arg));
arg->fd = client_fd;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 8 * 1024 * 1024); /* 8 MB — the C10k problem */
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, connection_thread, arg);
}
/* Thread function */
void *connection_thread(void *arg) {
conn_arg_t *conn = arg;
recv(conn->fd, buf, sizeof(buf) - 1, 0);
send(conn->fd, RESPONSE, strlen(RESPONSE), 0);
close(conn->fd);
free(conn);
sem_post(&thread_slots); /* release slot */
return NULL;
}The 8 MB math: pthread_attr_getstacksize() returns 8,388,608 bytes on Linux. At 10,000 threads, that is 83 GB of virtual address space — more than any typical server’s RAM. The OS will start swapping, then OOM-killing, long before you reach 10,000 active threads.
Context switch overhead: Each pthread_create triggers a kernel syscall. Switching between 10,000 threads means the scheduler runs constantly. A thread doing 10μs of I/O work but switching every 100μs spends 90% of its CPU time in the kernel scheduler. Our measurement shows throughput collapses above 2,000 threads — the context switch storm dominates.
Thread pool as mitigation: Capping at MAX_THREADS=1000 bounds memory (8 GB) but moves the bottleneck to sem_wait(). When all slots are busy, new accepted connections wait. The pool queue has replaced the accept queue as the primary backpressure mechanism. Nothing is fundamentally faster — we have just bounded the explosion.
v1 result: 38,000 req/s at 1k threads, dropping sharply above 2k. Memory RSS is 8 GB at the pool cap.
Stage 2 epoll event loop: O(1) readiness notification
- Create a non-blocking server socket; set O_NONBLOCK on every accepted fd
- epoll_create1(0) → add server_fd with EPOLLIN | EPOLLET
- Event loop: epoll_wait → server_fd ready → accept loop until EAGAIN; client_fd ready → drain loop until EAGAIN
- Implement drain loop: recv() in a loop until EAGAIN, accumulate partial requests, send response, close
- Run with 50,000 concurrent idle connections — confirm memory stays flat (no per-thread stack)
epoll solves the O(n) problem. Instead of scanning all file descriptors, the kernel maintains an interest list and notifies us only when a descriptor becomes ready.
The select/poll problem:
/* poll() — O(n): kernel scans every fd in the array on every call */
poll(fds, n_fds, -1); /* n_fds=50,000 → 50,000 comparisons per wake */The epoll solution:
/* epoll: kernel maintains a red-black tree (interest list) and a linked list (ready list).
epoll_wait returns only ready fds — cost is O(ready_events), not O(all_fds). */
int epfd = epoll_create1(0);
/* Add server socket with edge-triggered mode */
struct epoll_event ev = { .events = EPOLLIN | EPOLLET, .data.fd = server_fd };
epoll_ctl(epfd, EPOLL_CTL_ADD, server_fd, &ev);
while (1) {
int n = epoll_wait(epfd, events, MAX_EVENTS, -1);
for (int i = 0; i < n; i++) {
if (events[i].data.fd == server_fd) {
/* New connection(s) — accept in a loop until EAGAIN */
while (1) {
int client_fd = accept(server_fd, ...);
if (client_fd < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) break;
set_nonblocking(client_fd);
epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &ev_client);
}
} else {
/* Data ready on a client fd */
handle_readable(epfd, events[i].data.fd);
}
}
}Edge-triggered vs level-triggered:
Level-triggered (default, no EPOLLET): epoll_wait returns the fd on every call as long as data is available. Simpler to code but can result in repeated wake-ups for the same data.
Edge-triggered (EPOLLET): epoll_wait fires exactly once per state transition — when new data arrives. After that, it will not fire again until you have drained all available data and the fd goes quiescent. This requires the drain loop:
void handle_readable(int epfd, int fd) {
while (1) {
ssize_t n = recv(fd, buf, sizeof(buf), 0);
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) break; /* drained */
if (n <= 0) { close_conn(epfd, fd); return; } /* closed or error */
/* process data... */
}
}If you skip the drain loop with EPOLLET, you read the first chunk of data and then never hear about the rest — the socket silently stops firing.
The single-thread trade-off: One thread, 50,000 connections, no locking anywhere. This is why nginx’s single-worker is so fast: no mutex contention, no context switches, cache-hot data structures. The cost: a slow handler (database query, blocking file read) stalls every other connection on that thread. nginx solves this by keeping handlers non-blocking and offloading slow operations to worker threads via thread pools.
v2 result: 148,000 req/s on a single thread. nginx’s single worker reaches ~180k; our gap is the simplified HTTP parsing.
Stage 3 io_uring: submission/completion rings, zero-syscall I/O
- io_uring_queue_init_params(QUEUE_DEPTH=256, &ring, IORING_SETUP_SQPOLL) — fall back gracefully if SQPOLL needs CAP_SYS_ADMIN
- Submit an initial accept SQE; in the completion loop: OP_ACCEPT → submit_recv; OP_RECV → submit_send; OP_SEND → close(fd)
- Pack op_type + fd into user_data (uint64) to identify completions
- Measure syscall count with strace -c: compare v2 epoll vs v3 io_uring
io_uring, introduced in Linux 5.1 (2019), reimagines the kernel I/O interface. Instead of crossing the kernel boundary for every operation, userspace and the kernel share two ring buffers mapped into both address spaces.
userspace kernel
│ │
│ Submission Queue (SQ) │
│ ┌─────────────────────┐ │
│ │ SQE: accept fd=3 │──┤── kernel thread picks up SQEs
│ │ SQE: recv fd=7 │ │ (in SQPOLL mode, no syscall needed)
│ │ SQE: send fd=7 │ │
│ └─────────────────────┘ │
│ │
│ Completion Queue (CQ) │
│ ┌─────────────────────┐ │
│ │ CQE: accept res=8 │◀─┤── kernel writes completions here
│ │ CQE: recv res=42 │ │
│ └─────────────────────┘ │
│ │SQPOLL mode: With IORING_SETUP_SQPOLL, the kernel spawns a dedicated thread that polls the SQ ring continuously. Your application writes SQEs directly to the ring buffer (a memory write, not a syscall) and the kernel thread picks them up. On the hot path: zero syscalls. The 26% reduction in syscalls vs the epoll path is measurable with strace -c.
State machine via user_data:
/* Pack op_type and fd into the 64-bit user_data field */
enum op_type { OP_ACCEPT = 0, OP_RECV = 1, OP_SEND = 2 };
uint64_t make_user_data(enum op_type op, int fd) {
return ((uint64_t)op << 32) | (uint64_t)fd;
}
/* Completion loop */
while (1) {
io_uring_wait_cqe(&ring, &cqe);
enum op_type op = ud_op(cqe->user_data);
int fd = ud_fd(cqe->user_data);
io_uring_cqe_seen(&ring, cqe);
switch (op) {
case OP_ACCEPT:
submit_recv(&ring, cqe->res); /* cqe->res = new client fd */
submit_accept(&ring, server_fd, slot); /* keep listening */
io_uring_submit(&ring);
break;
case OP_RECV:
submit_send(&ring, fd, cqe->res);
io_uring_submit(&ring);
break;
case OP_SEND:
close(fd);
break;
}
}Why SQPOLL matters for high-frequency servers: Each syscall on a modern CPU costs 100–300ns (kernel entry, security checks, return). At 187,000 req/s, that is 18–56ms of pure syscall overhead per second if every operation requires one. SQPOLL eliminates the submit-side syscalls; the 26% gain versus epoll comes from removing epoll_ctl() + epoll_wait() per-event overhead.
v3 result: 187,000 req/s in SQPOLL mode, 26% fewer syscalls than the epoll path. Gains widen on faster CPUs where syscall overhead is a larger fraction of total cost.
Run it yourself
labs/io-model ↗
GCC 12+Linux kernel 5.1+ (for io_uring)liburing-dev - 1 Clone and build all four servers
git clone https://github.com/pushkarkumar-dev/system-design-labs cd system-design-labs/labs/io-model make - 2 Run v0 (blocking single-thread)
# Terminal 1 ./v0_blocking # Terminal 2 curl http://localhost:8080/ - 3 Stress v0 to see accept queue fill
# Watch recv-Q on the listening socket watch -n0.5 'cat /proc/net/tcp | awk NR==1 || /0050/' # In another terminal — fire 300 concurrent requests for i in $(seq 300); do curl -s http://localhost:8080/ & done; wait - 4 Run v1 (thread-per-connection)
./v1_threaded - 5 Run v2 (epoll event loop)
./v2_epoll - 6 Benchmark v2 vs v0 with wrk
# Install: sudo apt install wrk wrk -t4 -c100 -d10s http://localhost:8080/ - 7 Run v3 (io_uring, requires Linux 5.1+)
sudo ./v3_io_uring # sudo needed for SQPOLL on kernels before 5.11 - 8 Count syscalls to compare v2 vs v3
strace -c ./v2_epoll & wrk -t1 -c50 -d5s http://localhost:8080/ kill %1 strace -c ./v3_io_uring & wrk -t1 -c50 -d5s http://localhost:8080/ kill %1 - 9 Run the smoke test
bash tests/test_server.sh
Benchmarks
Linux 6.8, 4-core VM, 8GB RAM, loopback | Metric | Result | Notes |
|---|---|---|
| Blocking single-thread (v0) | 1,200 req/s | One connection at a time; accept queue backpressure visible in /proc/net/tcp |
| Thread-per-connection (v1, 1k cap) | 38,000 req/s | RSS 8 GB; throughput collapses above 2k threads (context switch storm) |
| epoll event loop (v2, 1 thread) | 148,000 req/s | nginx single-worker ~180k; our gap is simplified HTTP parsing |
| io_uring SQPOLL (v3) | 187,000 req/s | 26% fewer syscalls than epoll; gains widen on faster CPUs |
Stress tests
Fill the accept queue. Start v0 with BACKLOG=2 (recompile with the constant changed). Fire 100 concurrent connections. Watch /proc/net/tcp show recv-Q=2. The 98 other connections sit in the OS’s pre-accept state. After the RTO fires (~1s), you will see retransmission packets in Wireshark. This is silent-drop behavior in action.
Context switch storm. With v1, cap the pool at 2,000 threads and fire 2,000 concurrent keep-alive connections from wrk. Observe CPU usage with htop. At 2,000 threads all doing tiny amounts of work, the kernel scheduler runs constantly. CPU user% drops and kernel% rises — the machine is thrashing between threads rather than doing application work. Reducing the pool cap to 500 restores throughput because fewer context switches are needed.
Edge-triggered trap. In v2, remove the drain loop from handle_readable() — call recv() exactly once per EPOLLET notification. Fire a series of requests where the HTTP headers span two TCP segments (force this with --limit-rate 1k in curl). The second segment arrives silently — no epoll_wait notification — and the connection hangs. The client eventually times out. This is the most common epoll bug in production systems.
io_uring SQ ring exhaustion. In v3, set QUEUE_DEPTH=4 and fire 1,000 concurrent connections. io_uring_get_sqe() returns NULL when the ring is full. Our server logs the error and drops the connection. In production io_uring servers, SQ ring sizing is a tuning parameter as important as listen(backlog).
What the toy misses
- No SO_REUSEPORT multi-queue Linux 3.9+ allows multiple processes to bind the same port, each with a private accept queue. nginx uses this to saturate all CPU cores without a shared accept mutex — each worker has its own kernel queue. Our v2 is single-threaded; adding SO_REUSEPORT would let us run N copies and scale to N cores with zero synchronization.
- No TCP_FASTOPEN TFO piggybacks data on the SYN packet, saving one full RTT for repeat connections. Requires kernel config (net.ipv4.tcp_fastopen=3) and a TFO cookie exchange on first connect; our server pays the full 3-way handshake cost unconditionally.
- No receive-side scaling (RSS) awareness Modern NICs hash 4-tuples (src-IP, src-port, dst-IP, dst-port) to different RX queues, routing packets to different CPU cores. Without RSS pinning, all connections on one port can land on a single CPU's softirq handler regardless of how many epoll threads you run. ethtool --set-rxfh controls this.
- No zero-copy sendfile sendfile(2) transfers data from a file descriptor to a socket entirely in the kernel — no userspace copy. Our server does read()→write() with two copies through userspace buffers, doubling memory bandwidth for large responses. nginx's sendfile directive enables this; our toy never reads from a file at all.
- No NUMA-aware thread placement On multi-socket machines, memory allocated on NUMA node 0 has higher latency when accessed by a thread pinned to node 1. nginx and Redis pin threads to NUMA nodes and allocate from local memory pools to avoid cross-socket traffic. taskset and numactl are the tools; our server has no NUMA awareness.
- No kernel TLS (kTLS) kTLS moves TLS record encryption into the kernel, enabling sendfile on encrypted data. Without it, TLS multiplies per-connection CPU cost by 8–15x; adding OpenSSL to our server would make epoll's advantage over threads disappear under encryption overhead.
Wiring it into a Java/Spring service
Wiring it into a Java/Spring service
io.netty:netty-all:4.1.107.Final org.springframework.boot:spring-boot-starter-webflux:3.3.0 labs/io-model/java-integration Clone and run: cd labs/io-model/java-integration && mvn spring-boot:run Netty’s boss/worker groups = our v0 accept loop + our v2 epoll loop
Netty’s ServerBootstrap exposes the two-group model explicitly. The mapping to our C servers is exact:
// Boss group: 1 thread — this IS our v0_blocking accept loop.
// One thread calls accept() on the server channel and hands each
// new SocketChannel to the worker group.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
// Worker group: 4 threads — each thread is our v2_epoll loop.
// Each worker runs its own event loop (epoll on Linux, kqueue on macOS)
// over the subset of channels assigned to it.
// No locking on the hot path — each channel is pinned to one worker.
EventLoopGroup workerGroup = new NioEventLoopGroup(4);
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new EchoHandler());
}
});
ChannelFuture f = b.bind(8080).sync();
f.channel().closeFuture().sync();The NioEventLoopGroup uses Java’s Selector (which wraps epoll on Linux) under the hood. When you write a ChannelInboundHandlerAdapter, you are writing a handler that runs inside the worker’s event loop — exactly like writing the body of our handle_readable() function in v2. If your handler blocks (calls Thread.sleep, does a synchronous database query, reads a file), it stalls all channels on that worker thread, exactly like a slow handle_readable() stalls all connections in our C event loop.
// Full NettyEchoServer — see labs/io-model/java-integration/
static class EchoHandler extends ChannelInboundHandlerAdapter {
private static final byte[] RESPONSE =
"HTTP/1.1 200 OK\r\nContent-Length: 6\r\nConnection: close\r\n\r\nhello\n"
.getBytes(CharsetUtil.UTF_8);
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
((ByteBuf) msg).release(); // discard the request bytes
ctx.writeAndFlush(Unpooled.copiedBuffer(RESPONSE))
.addListener(ChannelFutureListener.CLOSE);
}
}Spring WebFlux = writing our v2 epoll loop without knowing it
When you add spring-boot-starter-webflux to your pom.xml, Spring Boot starts Reactor Netty as the HTTP server. Every @RestController method is compiled into a Reactor pipeline that runs on Netty’s worker event loop threads.
// This looks like a normal controller method.
// Internally it runs on a Netty NioEventLoop thread — our v2 event loop.
// The Mono return type signals the event loop to continue processing
// other channels while this response is being built.
@RestController
public class HelloController {
@GetMapping("/hello")
public Mono<String> hello() {
return Mono.just("hello"); // synchronous: fine, runs inline
}
// WARNING: this blocks the event loop — stalls ALL channels on this thread
@GetMapping("/bad")
public Mono<String> blocking() {
Thread.sleep(1000); // NEVER do this in WebFlux
return Mono.just("bad");
}
}The rule is the same as in our C v2: if you block the event loop thread, you stall all other connections. WebFlux’s Mono/Flux are the Java mechanism for keeping work non-blocking.
Java 21 Virtual Threads = the hybrid model
Project Loom (Java 21) adds a fourth option: write blocking code, but let the JVM schedule it efficiently. A virtual thread is a coroutine managed by the JVM. When a virtual thread blocks on I/O (recv, connect, file read), the JVM parks it and the carrier thread (a real OS thread) picks up another virtual thread to run.
// VirtualThreadServer — blocking code that scales like async.
try (ServerSocket server = new ServerSocket(8080)) {
while (true) {
Socket conn = server.accept();
// Thread.ofVirtual() creates a virtual thread.
// This code looks exactly like v0_blocking's accept loop.
// The JVM makes it scale like v2 by parking on blocking calls.
Thread.ofVirtual().start(() -> handle(conn));
}
}
private static void handle(Socket conn) {
try (conn) {
// This recv() blocks — but it blocks the VIRTUAL thread, not the carrier.
// The carrier thread is freed to run other virtual threads.
byte[] buf = conn.getInputStream().readNBytes(4096);
conn.getOutputStream().write(RESPONSE);
}
}The stack size difference: A virtual thread’s stack starts at a few hundred bytes and grows on demand. 100,000 virtual threads consume approximately 100 MB. A real OS thread consumes 8 MB unconditionally — 100,000 threads would consume 800 GB. Virtual threads make the C10k problem structurally solved at the JVM level.
What virtual threads do NOT do: They do not eliminate syscalls. Each park/unpark still involves JVM runtime work. For maximum throughput on hot paths, explicit Netty event loops (or our C v2/v3) remain faster. Virtual threads are optimized for throughput under I/O-bound concurrency, not for minimizing per-operation overhead.
The full comparison
| Model | Code style | Scales via | JVM equivalent |
|---|---|---|---|
| v0 blocking | Sequential | Nothing | Classic blocking Java (Servlet 3.0, single thread) |
| v1 threads | Sequential | OS threads | ExecutorService + Callable |
| v2 epoll | Callback/state machine | Event loop | Netty ChannelHandler, Spring WebFlux Mono |
| v3 io_uring | Completion-driven | Ring buffers | Netty’s linux-io-uring transport (experimental) |
| — | Sequential (looks like v0) | JVM scheduler | Java 21 Thread.ofVirtual() |
Run the demo
cd labs/io-model/java-integration
mvn spring-boot:runExpected output on startup:
=== I/O Model Comparison (C vs JVM) ===
Model req/s Notes
-----------------------------------------------------------------------------------------------
v0 Blocking single-thread (C) 1,200 One connection at a time; accept queue backpressure
v1 Thread-per-connection (C, 1k) 38,000 8 GB RSS; collapses above 2k threads
v2 epoll event loop (C, 1 thread) 148,000 O(1) readiness; nginx single-worker ~180k
v3 io_uring SQPOLL (C) 187,000 26% fewer syscalls than epoll
--- --- ---
Netty NioEventLoop (Java, 4 workers) ~160,000 =~ v2 x4; boss=accept thread, workers=epoll loops
Spring WebFlux (Reactor Netty) ~155,000 WebFlux annotations compile to Netty pipeline handlers
Java 21 Virtual Threads (Loom) ~140,000 Blocking code; JVM parks on I/O; no 8MB stack penalty What surprised me
Where to go deeper
- Linux epoll man page — the definitive reference including the level-triggered vs edge-triggered distinction:
man 7 epoll - io_uring documentation by Jens Axboe — the original design document explaining SQ/CQ ring semantics and SQPOLL: kernel.dk/io_uring.pdf
- liburing GitHub — well-documented examples for accept, recv, send, and linked requests: github.com/axboe/liburing
- nginx architecture guide — explains master/worker process model, accept mutex, SO_REUSEPORT, and sendfile: nginx.org/en/docs/dev/development_guide.html
- Redis event loop (ae.c) — 600 lines of C implementing a portable epoll/kqueue/select abstraction: github.com/redis/redis blob ae.c
- C10k problem — Dan Kegel’s original 1999 analysis still holds up: kegel.com/c10k.html
- Project Loom — the official JEP and state of virtual threads in Java 21+: openjdk.org/projects/loom
Related labs and questions
- Prerequisite: TCP Stack — the state machine that produces the connections this lab accepts
- Prerequisite: HTTP Server — the protocol layer sitting on top of the I/O model
- Next in networking: WebSocket Gateway — long-lived connections where the epoll model shines
- System design question: Why does Redis use a single-threaded event loop but nginx uses multiple worker processes? Redis is CPU-bound on in-memory operations with microsecond latency; the event loop avoids locking. nginx is I/O-bound waiting on upstream servers; multiple workers saturate multiple CPU cores via SO_REUSEPORT.
- Interview question: A Java service uses Spring MVC (thread-per-request, Tomcat) and handles 500 req/s fine. At 5,000 req/s the latency explodes. What is likely happening and what is the fix? Thread pool exhaustion — requests queue waiting for a Tomcat thread. Fix: either increase the thread pool (bounded by memory) or migrate to Spring WebFlux (event-loop model, same thread count handles 10x more connections).