Video in progress

For a new real-time gateway, would you choose the plain blocking model with Java 21 virtual threads, or the reactive WebFlux/Netty model?

For a gateway where connections are mostly idle and every message involves blocking I/O, virtual threads simplify the code. When you need a huge number of connections, strict backpressure and stream operators, the Netty/reactive model is still stronger.

📖 Answer

First, one misconception to correct: a WebSocket server doesn't give each connection a thread even without virtual threads. Tomcat NIO and Netty keep idle connections in a selector. Virtual threads aren't for "raising the number of connections"; they change how you write the code that handles a message when it arrives.

The virtual thread model (Spring MVC + spring.threads.virtual.enabled=true):

  • plain JDBC and RestClient inside @MessageMapping: blocking code, but it doesn't occupy a platform thread;
  • stack traces are readable, and debugging and profiling work as usual;
  • a style the team already knows.

One catch: STOMP's clientInboundChannel/clientOutboundChannel have their own executors. Depending on the Boot version they may not switch to virtual threads automatically; check in a thread dump and provide the executor yourself if needed.

Limits: pinning. On Java 21, blocking inside a synchronized block holds the carrier thread (mostly fixed in JDK 24). It's still found in old libraries and JDBC drivers. And backpressure: in plain code the "client is slow" case is still handled with a queue that you write.

The reactive (WebFlux, Netty) model:

  • working with streams is natural: buffer, sample, onBackpressureLatest, with conflation and batching in one line;
  • very low memory per connection;
  • but the whole chain has to be non-blocking. One blocking call stops the event loop and hundreds of connections freeze.

I usually choose like this:

  • the gateway is "thick" (a DB and other services on every message) and the team knows MVC well: virtual threads;
  • the gateway is "thin" (routing only, high throughput, transforming the stream) and there are hundreds of thousands of connections: Netty or WebFlux;
  • a reactive codebase already exists: it stays, and migrating just for virtual threads isn't worth it.

In both cases I measure with one prototype and the same load test. Memory per connection and p99 latency numbers close the argument quickly.