Video in progress
For a new high-load gateway service, which model do you choose: Netty (event loop) or plain blocking code with virtual threads?
On Java 21+, virtual threads + blocking code are enough for many services and much simpler for the team. Netty pays off when you need a huge number of long-lived connections, streaming protocols, minimal latency and precise control over memory.
📖 Answer
Here it is not only the technology; the team counts too.
The Netty model: a few event loop threads, each managing thousands of channels. Its strengths are pooled direct buffers, explicit backpressure (isWritable), a ready pipeline for protocols like WebSocket/HTTP2/gRPC, and small, predictable memory. The price: the code is in a callback or reactive style, stack traces are hard to read, and accidentally blocking the event loop stalls a whole group of connections. Finding such bugs takes experience on the team.
The virtual thread model: "one request, one thread" is cheap again. JDBC, a plain HTTP client, try/catch, a debugger, ordinary stack traces: everything is as usual. Tomcat or Jetty switch to this model with spring.threads.virtual.enabled=true.
What I check for the decision:
- Connection profile. Short request/response: virtual threads. More than 100 thousand open WebSockets or long streaming: Netty (or a framework on top of Netty).
- Memory. A virtual thread is cheap, but each one still holds a stack (on the heap) and the objects tied to the request. A 64 KB buffer per connection means 6 GB at 100 thousand connections. In Netty, the buffer for a connection is allocated only while there is data.
- Libraries. Are the drivers you use compatible with virtual threads, is there
synchronized+ IO inside (pinning on Java 21)? Blocking inside native code holds the carrier. - The team. Are there people who can support reactive code? If not, reactive code quickly becomes technical debt.
In practice a hybrid is common: a ready-made Netty-based gateway at the outer edge (Spring Cloud Gateway, Envoy), and internal business services on plain MVC with virtual threads. Before deciding, build a small prototype of both options and test them under real load, comparing p99 and memory: that is far more reliable than guessing.
Sign up to mark studied questions and save your progress.