Video in progress
How do you choose between WebFlux (reactive) and virtual threads for a new high-load service?
On Java 21+, for most IO-bound services, virtual threads with plain blocking code are simpler and enough. The reactive stack pays off for streaming, streams that need backpressure, long-lived connections (SSE, WebSocket), and when the team already has a reactive ecosystem.
📖 Answer
Both solve the same problem: not tying up an OS thread while waiting. The difference is the programming model and what it costs the team.
| Criterion | Virtual threads + MVC | WebFlux / Reactor |
|---|---|---|
| Code style | plain, sequential | operator chains |
| Debugging and stack traces | usual | hard, needs checkpoints |
| Libraries | JDBC, any blocking client | R2DBC, reactive drivers needed |
| Backpressure | manual (Semaphore, queue) | built into the protocol |
| Streaming, SSE | possible, but manual | natural |
| Learning cost for the team | low | high |
The case where I choose virtual threads: a classic REST/CRUD service, a DB plus a few HTTP calls, and a team that knows Spring MVC. Throughput is almost the same as with reactive, but the code is readable and onboarding is easy.
The case where I choose reactive:
- a data stream: reading from Kafka, transforming and writing somewhere else, with backpressure;
- thousands of long-lived connections: WebSocket, SSE, long polling;
- a gateway-type service (Spring Cloud Gateway is already reactive);
- the team knows Reactor well and there's already a reactive codebase.
There's also a mixed approach: inside WebFlux, send a blocking call to Schedulers.boundedElastic(). But that combines the complexity of both worlds, so if you can, pick one model.
Before deciding, I build a small prototype of a realistic endpoint in both variants and compare p99, CPU and memory under the same load. The difference often turns out small, and then the choice stops being technical and becomes a team question: who will maintain this code for the next 3 years.
Sign up to mark studied questions and save your progress.