Reactive programming with WebFlux, and when it's worth it
Non-blocking I/O, Reactor Mono and Flux, backpressure, WebClient, R2DBC, the costs (debugging, ecosystem), and why virtual threads changed the calculus.
The idea
Spring MVC (all your projects) traditionally uses one thread per request. That thread blocks while waiting on the DB or HTTP calls. Reactive (Spring WebFlux + Project Reactor) uses a few event-loop threads that never block: work is expressed as asynchronous pipelines that resume when data arrives.
@GetMapping("/dashboard/{id}")
Mono<Dashboard> dashboard(@PathVariable UUID id) {
Mono<Stats> stats = webClient.get().uri("/stats/{id}", id).retrieve().bodyToMono(Stats.class);
Flux<Booking> recent = bookingRepo.findRecent(id); // R2DBC: non-blocking SQL
return Mono.zip(stats, recent.collectList())
.map(t -> new Dashboard(t.getT1(), t.getT2()))
.timeout(Duration.ofSeconds(2))
.onErrorResume(e -> Mono.just(Dashboard.degraded()));
}
Mono<T>holds 0 or 1 values, andFlux<T>holds 0 to N values (a stream).- Nothing runs until you subscribe; WebFlux subscribes for you.
- Backpressure. The consumer signals how much it can handle, so a fast producer can’t overwhelm a slow consumer.
Strengths
- Huge numbers of concurrent connections with few threads (streaming, gateways, SSE/WebSocket fan-out).
- Native streaming of data (
Fluxto SSE). - Composition of many async calls with timeouts and fallbacks.
Costs
- Everything must be non-blocking. One blocking call (JDBC, a blocking SDK like some payment libraries) on an event loop stalls the whole server.
- R2DBC instead of JPA. No lazy loading or dirty checking, and a different ecosystem.
- Harder debugging (stack traces through operators), harder
ThreadLocal/MDC/security context propagation, and a steeper learning curve for the team.
Virtual threads changed the decision
Since Java 21, virtual threads give MVC-style blocking code most of the scalability of reactive code for I/O-bound apps (concurrency lesson). The modern guidance:
| Situation | Choose |
|---|---|
| Typical CRUD/business app with JPA (all your projects) | Spring MVC + virtual threads |
| API gateway, streaming proxy, very high connection counts, end-to-end non-blocking stack | WebFlux |
| Kotlin | Coroutines (with MVC or WebFlux) |
Check yourself
1. Mono represents…
2. Worst mistake in WebFlux?
3. For a JPA-heavy business app on Java 25, best default?