Sub topic 13
Virtual threads or reactive: choosing an approach
This chapter assumes you know Java 21 basics, have a Spring Boot 3.2+ MVC application, and understand blocking I/O such as JDBC and HTTP clients. You have seen how virtual threads let blocking code scale. Now the question is whether to use them or move to reactive programming (Spring WebFlux, Reactor).
What each approach solves
Both approaches address the same problem: a platform thread that waits on I/O is wasted. They fix it in different ways.
- Virtual threads keep the thread-per-request style. You write sequential, blocking code. When the code blocks, the JVM unmounts the virtual thread from its carrier thread, so the carrier can run other work.
- Reactive programming changes the programming model. You describe a pipeline of asynchronous steps. A small number of event-loop threads run those steps, and nothing blocks.
Virtual threads make blocking cheap. Reactive code avoids blocking entirely.
Comparing the two
| Concern | Virtual threads (Spring MVC) | Reactive (Spring WebFlux) |
|---|---|---|
| Code style | Sequential, imperative | Declarative pipelines of `Mono` and `Flux` |
| Stack traces and debugging | Normal call stacks | Often fragmented across operators |
| Existing blocking libraries (JDBC, JPA) | Work as they are | Need wrappers or reactive drivers |
| Backpressure | Not built in. Limit with semaphores or pools | Built into the model |
| Streaming data | Possible, but manual | Natural fit |
| Learning curve | Low for MVC teams | Higher |
| `ThreadLocal` use | Works, with care | Not reliable, because Reactor uses a context instead |
Backpressure is the real difference
Backpressure means a consumer can tell a producer to slow down. Reactive streams have this in the protocol. Virtual threads do not. A virtual thread per request will happily accept an unlimited number of requests and push them at your database.
With virtual threads you add limits yourself, as the earlier chapter on protecting downstream services showed.
import java.util.concurrent.Semaphore;
class LimitedInventoryClient {
private final Semaphore permits = new Semaphore(20);
private final InventoryClient delegate;
LimitedInventoryClient(InventoryClient delegate) {
this.delegate = delegate;
}
String stockFor(String sku) throws InterruptedException {
permits.acquire();
try {
return delegate.stockFor(sku);
} finally {
permits.release();
}
}
}If your workload is a stream of events where flow control between stages matters, such as server-sent events, long-lived streaming connections, or chained message processing, reactive handles that more directly.
A decision flow
Decision criteria
Choose virtual threads when:
- The application is Spring MVC and mostly makes blocking calls to databases and HTTP services.
- You depend on blocking libraries such as JDBC or JPA.
- Your team wants readable code and ordinary stack traces.
- You can set explicit limits on downstream resources.
Choose or keep reactive when:
- You already have a working WebFlux code base. Rewriting it gains little.
- You stream data with flow control between stages.
- Your whole call chain, including drivers, is already non-blocking.
Choose neither when the work is CPU-bound. Virtual threads do not add CPU capacity, and reactive code does not either.
Common mistakes
- Rewriting a working reactive service. Virtual threads are not a reason to migrate code that already performs well.
- Mixing styles carelessly. Calling blocking code inside a WebFlux handler stalls an event-loop thread. This is a bug in either model.
- Dropping limits. Moving from a bounded pool to virtual threads removes an accidental safety valve. Add a
Semaphoreor connection pool limit on purpose.
Exercise
A team runs a Spring MVC service that calls a JPA database and two REST APIs. Requests are slow because threads wait on I/O, and the pool of 200 threads is exhausted at peak. A colleague proposes rewriting the service in WebFlux. What would you recommend, and what must you add after the change?
Show answer
Enable virtual threads with spring.threads.virtual.enabled=true. The workload is blocking I/O, the code uses JPA (a blocking library), and a rewrite would be costly. After the change, add explicit limits such as a Semaphore or a sized connection pool, because the old 200-thread pool no longer caps concurrency and the database could be overloaded.