Java Interview Questions

Netty Reactor Model and NIO Fundamentals

Explain Netty's reactor model. Why is one thread per connection wrong at scale?

Why Interviewers Ask This

Any high-concurrency Java role (middleware, gateway, RPC) asks this. Shows whether you know why Netty exists.

How to Answer

  1. 1

    One thread per connection: 10K connections = 10K threads × ~1MB stack = 10GB. Context switching dominates.

  2. 2

    NIO Selector: one thread watches N sockets via kernel multiplexing (epoll on Linux). Non-blocking.

  3. 3

    Netty reactor: boss EventLoop accepts, worker EventLoop group handles I/O. Each EventLoop is single-threaded and serially processes its channels — no locking needed inside a channel.

  4. 4

    EventLoop guarantees: a channel is always processed by the same EventLoop, so channel state is thread-confined.

  5. 5

    Blocking operations (DB, file I/O) must go off the EventLoop — use an EventExecutorGroup or offload to a business thread pool, otherwise you stall all channels on that EventLoop.

Sample Answer

One thread per connection is what made servers expensive in the 2000s — 10K connections × 1MB stack = 10GB just for thread stacks, and the OS scheduler thrashes through context switches. NIO's Selector fixes this by having one thread watch many sockets via epoll/kqueue. Netty builds a reactor pattern on top: a boss EventLoop handles accept(), a worker pool of EventLoops each takes some channels and does their I/O. Each EventLoop is single-threaded, so all events for a given channel run serially on the same thread — no locks needed inside handlers. This is also the footgun: any blocking call (synchronous DB query, disk I/O, Thread.sleep) inside a handler halts every channel on that EventLoop. The rule is: handlers stay non-blocking; anything blocking goes to a dedicated EventExecutorGroup. Netty is why a single JVM can hold tens of thousands of live connections; done right, it scales further than raw thread-per-request ever could.

Pro Tips

  • Never do JDBC inside a channel handler. I've seen this blow up prod more times than I can count.

  • EventLoop count ≈ 2 × CPU cores by default — good for CPU-light I/O work.

  • HireMe AI (即答侠) ties Netty to specific failure modes (blocked EventLoop, backpressure) so answers sound like lessons from incidents.

Frequently Asked Questions

Is Netty still relevant with virtual threads (Loom)?

Loom simplifies "one thread per request" without the memory cost, reducing one case for Netty. But Netty's non-blocking pipeline and backpressure model still win for protocol work and very high connection counts.

What is a ChannelPipeline?

An ordered list of ChannelHandlers an event passes through — like a middleware chain. Inbound and outbound events flow in opposite directions.

Worried About Going Blank in Your Interview?

HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.

Try HireMe AI Free