Quick revision
The short answer
The reactor pattern serves many connections with few threads: one demultiplexer, such as epoll or a Java NIO Selector, waits until any connection is ready for I/O, and a dispatcher hands each ready event to its handler. Netty builds this as event loop groups, where each event loop is one thread with its own selector serving many channels, and every handler in a channel's pipeline runs on that channel's event loop. That is why code must never block there: a blocked event loop stalls every connection assigned to it, so blocking work must move to a separate executor.
What to remember
In this answer, we saw that the reactor pattern lets few threads serve many connections: a demultiplexer such as epoll or a NIO Selector reports which connections are ready, and a dispatcher runs short handlers for those events. Netty implements it with event loop groups, where each event loop is one thread with its own selector, each channel is bound to one event loop, and every handler in the channel's pipeline runs there. Blocking that thread stalls every connection assigned to it, so blocking or long work must move to a separate executor and write its result back to the channel.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The reactor pattern serves many connections with few threads: one demultiplexer, such as epoll or a Java NIO Selector, waits until any connection is ready for I/O, and a dispatcher hands each ready event to its handler. Netty builds this as event loop groups, where each event loop is one thread with its own selector serving many channels, and every handler in a channel's pipeline runs on that channel's event loop. That is why code must never block there: a blocked event loop stalls every connection assigned to it, so blocking work must move to a separate executor.
Why this question is asked
Netty sits underneath a large part of the Java server world: Spring WebFlux through Reactor Netty, gRPC Java, Kafka clients in places, Elasticsearch, Cassandra drivers and many API gateways. When these systems misbehave under load, the cause is often a handler that blocks an event loop. Interviewers ask this question to see whether we understand the mechanism and not only the rule. In this answer, we will recap the problem the reactor pattern solves, walk through the pattern itself, see how Netty maps it onto event loops, channels and pipelines, measure what one blocking call does, and look at how to move blocking work safely off the event loop.
1A Quick Recap: Why Thread-per-Connection Runs Out
The simplest server gives each connection its own platform thread, which reads a request, waits while it reads, does the work, and writes the reply. Waiting is cheap for the CPU, but each platform thread reserves stack memory, often 512 KB to 1 MB, and the operating system has to schedule all of them. Ten thousand mostly idle connections would mean ten thousand mostly idle threads.
Most of the time a network connection is idle: no bytes have arrived yet, or the socket buffer is full and we cannot write. What we really need is a way to ask the operating system, for all connections at once, which ones are ready right now. That is exactly what epoll on Linux, kqueue on BSD and macOS, and Java's Selector on top of them provide.
2How the Reactor Pattern Works
The reactor pattern has three parts. The demultiplexer is the system call that waits on many connections and returns the ones that are ready, for example epoll_wait or Selector.select(). The reactor, or dispatcher, is the loop that calls the demultiplexer and, for each ready event, calls the handler registered for that connection. The handlers are small pieces of code that do the work for one event: accept a new connection, read available bytes, decode a message, or write a reply.
The key promise is that a handler returns quickly. Because one thread runs the loop and all the handlers, the loop only gets back to select() when the current handler is done. Here is the bare pattern in plain Java NIO.
- Wait on the demultiplexer
select()or epoll_wait over all channels - Take the next ready keyaccept, read or write readiness
- Dispatch to its handlerthe handler reads or writes what is available
- Handler returns quickly?YesTake the next keyNoEvery other channel waits
- Run queued tasksthen go back to waiting
5 steps in order. Play them, or step through them one at a time.
Notice that nothing in this loop waits on a single connection. If one client sends half a request, the read handler stores what arrived and returns; the rest will come in a later readiness event. So one thread can serve thousands of connections, as long as every handler is quick.
3How Netty Implements It
Netty takes the single loop above and makes it scale across cores. An EventLoop is one thread that owns one selector and runs exactly that loop, plus a queue of tasks submitted to it. An EventLoopGroup is a set of such loops. A typical server uses two groups: a boss group, often one thread, that accepts connections, and a worker group, by default twice the number of cores, that serves them.
When a connection is accepted, Netty creates a Channel for it and assigns the channel to one worker event loop for its whole life. All I/O and all handler calls for that channel happen on that thread. This is a deliberate design: because only one thread ever touches a channel's state, handlers do not need locks.
Each channel has a ChannelPipeline, an ordered chain of handlers. Inbound events, such as bytes read, flow through decoders and into our business handler; outbound writes flow back through encoders. Every handler in the pipeline is called on the channel's event loop by default.
- EventLoopGroupa set of event loops, usually 2 x cores for workers
- EventLoopone thread with its own selector and task queue
- Channelone connection, bound to one event loop for life
- ChannelPipelineordered handlers: decoders, business handler, encoders
- ChannelHandlerour code, called on the channel's event loop
5 layers, from the top down. Step through them one at a time.
Newer Netty versions also offer native transports such as epoll and io_uring, and Netty 4.2 reworks the event loop classes around a common IO handler, so check your version for the preferred constructor. The model is the same: one thread, one selector, many channels.
4What One Blocking Call Does
Now suppose OrderHandler calls a JDBC query that takes 100 ms. The event loop thread that runs it is stuck in that call. It cannot return to select(), so for 100 ms it reads nothing, writes nothing and runs no tasks for any of the channels it owns. If it owns 2,000 channels, all 2,000 connections freeze, including ones whose requests needed only a microsecond of work.
So the damage is not proportional to the blocking request; it is proportional to how many connections share the thread. Throughput falls, latency rises across unrelated clients, timeouts fire, and idle-state handlers may even close connections that were perfectly healthy. With 16 worker event loops, sixteen slow calls in flight are enough to stop the whole server.
5Moving Blocking Work to a Separate Executor
The rule follows directly: anything that may wait, such as JDBC, file I/O, a blocking HTTP client or a lock held by another thread, runs on another executor, and the result is written back to the channel when it is ready.
Notice that writeAndFlush can be called from any thread: Netty sees that the caller is not the channel's event loop and queues the write as a task on it, so the channel is still only touched by its own thread. Netty also lets us add a handler with its own EventExecutorGroup in the pipeline, which runs that handler off the I/O thread. Either way, the event loop goes straight back to serving other channels.
6Follow-up questions
Why does Netty bind a channel to one event loop for its lifetime?
Because it removes the need for locks inside handlers. If only one thread ever runs a channel's handlers and I/O, state such as a partially decoded message or a per-connection counter is safe without synchronisation, and events stay in order. The cost is that a slow handler on that thread hurts every channel bound to it.
Does running Netty on virtual threads remove the rule?
No. Netty's event loops are still a few platform threads that multiplex many channels, so blocking one still stalls its channels. Virtual threads are a good executor for the blocking work we move off the loop, as in the example above, but the event loop itself must stay non-blocking.
7Conclusion
In this answer, we saw that the reactor pattern lets few threads serve many connections: a demultiplexer such as epoll or a NIO Selector reports which connections are ready, and a dispatcher runs short handlers for those events. Netty implements it with event loop groups, where each event loop is one thread with its own selector, each channel is bound to one event loop, and every handler in the channel's pipeline runs there. Blocking that thread stalls every connection assigned to it, so blocking or long work must move to a separate executor and write its result back to the channel.
Open with
The reactor pattern is a way to serve many connections with very few threads. Instead of a thread per connection waiting on its socket, one thread calls a demultiplexer, epoll on Linux or a Java NIO Selector, which waits on all the connections and returns the ones that are ready.
Then, if they want more
The reactor loop then dispatches each ready event to its handler, which reads or writes whatever is available and returns quickly, and the loop goes back to waiting. Netty builds this out as event loop groups.
Each event loop is a single thread with its own selector and task queue, and a server typically has a boss group that accepts connections and a worker group, by default twice the core count.
Each accepted channel is bound to one event loop for life, and every handler in its pipeline, decoders, my business handler, encoders, runs on that thread. That is why it needs no locks, and also why I must never block there.
If a handler makes a 100 millisecond JDBC call, that thread cannot get back to select, so every connection on that event loop freezes, not just the one request.
So anything that can wait goes to a separate executor, for example a virtual thread executor, and the result is written back with writeAndFlush, which Netty queues onto the channel's event loop.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the reactor pattern and how Netty implements it. Why must code never block on an...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/4e0126d160/