Sub topic 1
Why reactive? Blocking vs non-blocking request handling
Prerequisites
This chapter assumes you can read Java with lambdas and generics, and that you know basic Spring Boot (beans, dependency injection, @RestController). You should also understand HTTP and REST, and know that a classic servlet server gives each request its own thread.
The thread-per-request model
In Spring MVC on a servlet container, one thread handles one request from start to finish. When the handler calls a database, a remote API, or Thread.sleep, the thread stops and waits. It does no work, but it still holds its memory (often about 1 MB of stack) and its slot in the thread pool.
A typical pool has around 200 threads. If each request waits 1 second on a downstream service, the pool serves at most about 200 requests per second. A slow dependency fills the pool, and new requests queue up even though the CPU is nearly idle.
The non-blocking model
A non-blocking handler does not wait. It starts the I/O, registers what should happen when the result arrives, and returns the thread to the pool. A small number of threads, often one per CPU core, can then serve thousands of in-flight requests. WebFlux runs on this model.
The diagram compares the two.
The same endpoint both ways
Both controllers below simulate a one-second downstream call.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BlockingController {
@GetMapping("/slow")
public String slow() throws InterruptedException {
Thread.sleep(1000); // the request thread is stuck here
return "done";
}
}import java.time.Duration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class NonBlockingController {
@GetMapping("/slow")
public Mono<String> slow() {
return Mono.delay(Duration.ofSeconds(1)) // timer, no thread waits
.map(tick -> "done");
}
}The second method returns a Mono<String>, a description of a value that will exist later. The method returns immediately. The framework subscribes to the Mono and writes the response when the timer fires. No thread sleeps in between.
When WebFlux is a good fit
WebFlux pays off when requests spend most of their time waiting on I/O:
- Gateways and aggregators that call many remote services.
- Services that hold many long-lived connections, such as streaming or Server-Sent Events.
- Workloads that need high concurrency on limited memory.
| Situation | Better choice |
|---|---|
| Many concurrent I/O-bound calls | WebFlux |
| Streaming data to many clients | WebFlux |
| CPU-heavy work per request | Either; reactive adds no speedup |
| Blocking-only dependencies such as JDBC | Spring MVC, or isolate the calls |
| Team new to reactive, modest traffic | Spring MVC |
Common errors
- Expecting faster single requests. Non-blocking code improves how many requests you can hold at once. One request is not faster, and it can be slower.
- Blocking inside a reactive handler. A
Thread.sleepor JDBC call on an event-loop thread stalls every request on that thread. A later chapter covers detecting and isolating such calls. - Going half reactive. A
Monoreturn type on top of a blocking repository gains nothing. The whole call chain needs non-blocking I/O.
Exercise
A service handles each request with a 200-thread pool, and every request waits 500 ms on a database. Roughly what is the maximum throughput, and why does a non-blocking design change the limit?
Show answer
About 400 requests per second (200 threads divided by 0.5 s). Each thread is held while it waits. A non-blocking design releases the thread during the wait, so the limit depends on CPU and connection capacity, not on the thread count.