When would you choose Spring WebFlux over Spring MVC with virtual threads?
BackendSenior
In shortChoose WebFlux for streaming and connection-heavy workloads (server-sent events, WebSockets, gateways) and end-to-end backpressure with a fully non-blocking stack. For typical request-response services, Spring MVC on virtual threads gives similar concurrency with simpler blocking code, JPA and readable stack traces.
Model answer
How each scales: WebFlux uses a few event-loop threads and non-blocking I/O; handlers return Mono/Flux pipelines. Virtual threads make thread-per-request cheap again: a blocked virtual thread unmounts from its carrier, so blocking code scales to many concurrent requests.
WebFlux fits when:
- There are many long-lived connections or streams: SSE, WebSockets, API gateways and proxies.
- You need backpressure from the client socket to the data source.
- Every dependency has a non-blocking client (WebClient, R2DBC, reactive Kafka, Redis, Mongo).
MVC on virtual threads fits when:
- The service is request-response (most CRUD and business services).
- It uses JDBC/JPA or libraries that block or rely on ThreadLocals.
- The team values straightforward code and stack traces.
Costs of reactive: a learning curve, harder debugging (checkpoint(), debug agent), context propagation for MDC, tracing and security, and no JPA. Limits that remain with virtual threads: connection pools and downstream capacity (use bulkheads), pinning in native code, and memory for many ThreadLocals.
Kotlin: coroutines on WebFlux combine the non-blocking runtime with sequential code and structured cancellation. Mixed estates are normal: a reactive edge, virtual-thread services behind it.
What they're looking for
How the event loop and virtual threads each achieve concurrency, the non-blocking-everywhere requirement, backpressure, the costs of reactive (debugging, context propagation, R2DBC), and a nuanced per-service choice including Kotlin coroutines.
Common mistakes
- Saying WebFlux is always faster.
- Using WebFlux with blocking JDBC calls.
- Believing virtual threads remove all concurrency limits.
More backend questions
- What is the N+1 query problem, and how do you find and fix it?Mid-level
- What does Spring Boot's auto-configuration do, and how do you change what it set up?Mid-level
- How does a Spring backend authenticate requests from a mobile app, and how do you make sure users only see their own data?Mid-level
- Your Spring Boot service in Kubernetes keeps restarting with exit code 137. How do you investigate and fix it?Senior