Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

A Spring WebFlux endpoint calls a blocking JPA repository inside a map operator. Under load, throughput collapses and latency rises for all endpoints. Explain and fix.

WebFlux serves all requests on a small group of event loop threads, roughly one per CPU core, and a blocking JPA call inside map() holds one of those threads for the whole database round trip. Every other request assigned to that thread waits, so throughput collapses everywhere. The fix is to use a reactive driver such as R2DBC, or to wrap the blocking call in Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()); if most dependencies are blocking, Spring MVC on virtual threads is often the simpler choice.

Why this question is asked

This is one of the most common production problems in reactive Spring applications. The code looks reasonable, it passes every functional test, and then under real traffic one slow endpoint drags down endpoints that never touch the database. Interviewers ask it because the answer shows whether we understand the threading model behind WebFlux rather than only its API. In this answer, we will recap how WebFlux uses threads, reproduce the problem, see why one blocking call hurts unrelated requests, go through the fixes, see how BlockHound catches the mistake in tests, and finish with when Spring MVC with virtual threads is the better choice.

1A Quick Recap of the WebFlux Threading Model

Spring MVC traditionally gives each request its own thread from a pool of around 200. If the request waits for the database, its thread simply sleeps, and other requests use other threads.

WebFlux works the other way round. It runs on Reactor Netty, which uses an event loop: a thread that repeatedly asks the operating system which connections have something ready, and runs a short piece of work for each. By default there are about as many event loop threads as CPU cores, so an 8-core machine has roughly 8 of them, named like reactor-http-nio-1. Each connection is pinned to one of those threads, and thousands of connections share them.

This only works if every piece of work is short and never waits. A reactive pipeline describes what to do when data arrives; it should hand control back to the event loop while the data is on its way, not sit and wait for it.

2Reproducing the Problem

Here is the kind of code that causes the collapse. The repository is an ordinary Spring Data JPA repository, which uses JDBC underneath.

Java
@GetMapping("/orders/{id}")
Mono<OrderDto> order(@PathVariable long id) {
return Mono.just(id)
.map(orderRepository::findById) // blocking JDBC call, about 50 ms
.map(o -> toDto(o.orElseThrow()));
}
// log line inside findById:
// thread=reactor-http-nio-3 <- an event loop thread is waiting on the database

Notice the thread name in the log. map() runs its function on whatever thread delivers the value, and here that is the event loop thread that received the HTTP request. JDBC is blocking: the thread sends the query and then waits until the database replies. For those 50 milliseconds, reactor-http-nio-3 can do nothing else.

3Why One Blocking Call Slows Every Endpoint

Suppose the machine has 8 event loop threads and each blocking call takes 50 ms. Each thread can then finish at most 20 such calls per second, so the whole server tops out at about 160 requests per second for this endpoint, no matter how many connections are open. That is far less than the same server handles for non-blocking work.

But the bigger problem is the other endpoints. A health check or a cached lookup that takes 1 ms is served by the same event loop threads. If its connection is pinned to reactor-http-nio-3, it waits behind every database call queued on that thread. So latency rises for all endpoints at once, health checks start failing, and the load balancer may take healthy instances out of rotation. This is why the symptom is a collapse of the whole service rather than one slow URL.

Step throughA blocking call holds the event loop
Client AEvent loop 3DatabaseClient B
GET /orders/1
JDBC query, thread waits
GET /health arrives, must wait
rows after 50 ms
order response
health response, 50 ms late

6 messages in order. Play them, or step through them one at a time.

The health check needed 1 ms of work but waited for the whole database call.

4Fix 1: Use a Reactive Driver

The cleanest fix is to stop blocking at all. R2DBC is a reactive database API with drivers for PostgreSQL, MySQL and others, and Spring Data R2DBC gives us repositories that return Mono and Flux. The driver sends the query and releases the thread; when the rows arrive, Netty wakes the pipeline up again.

Java
interface OrderRepository extends ReactiveCrudRepository<Order, Long> { }
@GetMapping("/orders/{id}")
Mono<OrderDto> order(@PathVariable long id) {
return orderRepository.findById(id) // returns Mono<Order>, no thread waits
.map(this::toDto); // toDto is quick CPU work, fine on the event loop
}

Notice that we use flatMap or a repository that already returns a Mono, not map, when the next step is itself asynchronous. The cost of this fix is that R2DBC is not JPA: there is no lazy loading, no persistence context and no dirty checking, so entity mappings and queries often need rework.

5Fix 2: Move the Blocking Call to boundedElastic

When we must keep JPA, we move the blocking call off the event loop onto a scheduler meant for blocking work. Schedulers.boundedElastic() is a pool that grows up to ten times the number of cores by default and queues extra tasks, so blocking there does not starve the event loop.

Java
@GetMapping("/orders/{id}")
Mono<OrderDto> order(@PathVariable long id) {
return Mono.fromCallable(() -> orderRepository.findById(id)) // nothing runs yet
.subscribeOn(Schedulers.boundedElastic()) // run the callable on a blocking-safe thread
.map(o -> toDto(o.orElseThrow()));
}
// thread=boundedElastic-4 <- the event loop is free while we wait

Mono.fromCallable defers the call until subscription, and subscribeOn decides which thread performs the subscription, so the JDBC call runs on boundedElastic-4. The event loop thread is released at once and can serve other connections. This is a fix for the thread starvation, not for scale: each request still holds a pool thread and a JDBC connection for 50 ms, so the database connection pool size becomes the real limit.

6Catching Blocking Calls With BlockHound

These mistakes are easy to reintroduce, so it helps to catch them automatically. BlockHound is a Java agent from the Reactor team that instruments known blocking methods, such as Thread.sleep, socket reads and file I/O, and throws an error when one of them runs on a thread marked as non-blocking, which includes the Reactor and Netty event loops.

Java
@BeforeAll
static void installBlockHound() {
BlockHound.install(); // on JDK 13+ the test JVM needs -XX:+AllowRedefinitionToAddDeleteMethods
}
@Test
void orderEndpointDoesNotBlock() {
webTestClient.get().uri("/orders/1").exchange().expectStatus().isOk();
// with the broken version: BlockingOperationError: Blocking call! java.net.SocketInputStream#read
}

Running integration tests with BlockHound installed turns a production latency incident into a failed build, which is a much cheaper place to find it.

7When Spring MVC With Virtual Threads Is Simpler

If most of a service's dependencies are blocking, such as JPA, JDBC drivers and older SDKs, wrapping each call in fromCallable gives us the complexity of reactive code without its benefit. Since JDK 21, Spring Boot can run Spring MVC on virtual threads with spring.threads.virtual.enabled=true. A virtual thread is a lightweight thread managed by the JVM; when it blocks on I/O, the JVM parks it and frees the underlying carrier thread, so thousands of blocking requests cost little.

So the choice is about the whole service. WebFlux with reactive drivers fits streaming and services where every dependency is non-blocking. A blocking stack on virtual threads fits services that mostly call blocking code, and it is easier to write, debug and profile.

8Follow-up questions

Why does publishOn not fix it in the same way?

publishOn changes the thread for the operators that come after it, while subscribeOn changes the thread on which the source is subscribed. With Mono.fromCallable the blocking work is the source, so subscribeOn moves it. publishOn(boundedElastic()) placed before the blocking map would also work, but subscribeOn on a fromCallable is the idiomatic pattern and keeps the blocking call clearly marked.

Can we just increase the number of event loop threads?

We can, but it only delays the problem. Each extra event loop thread is still blocked for the full database call, and we lose the efficiency that made WebFlux attractive. The right move is to keep event loops non-blocking and give blocking work its own bounded pool, or to switch to a blocking model on virtual threads.

9Conclusion

In this answer, we saw that WebFlux serves everything on a few event loop threads, about one per core, so a blocking JPA call inside map() holds one of them for the whole database round trip and makes every request on that thread wait. That is why latency rises across all endpoints. We fixed it with a reactive driver such as R2DBC, or by wrapping the call in Mono.fromCallable with subscribeOn(Schedulers.boundedElastic()). We used BlockHound to catch blocking calls in tests, and noted that when most dependencies are blocking, Spring MVC on virtual threads is the simpler choice.

What to say in the interviewAbout 90 seconds aloud

Open with

WebFlux runs on Reactor Netty, which has a small number of event loop threads, about one per core. Every connection is pinned to one of them, and thousands of connections share them, so the model only works if nothing on those threads waits.

Then, if they want more

A JPA repository uses JDBC, which blocks. If I call it inside map, the function runs on the event loop thread that delivered the request, so that thread sits idle for the whole query, say 50 milliseconds.

On an 8-core box that caps us around 160 requests per second, and worse, every other request pinned to those threads, even a health check, waits behind the database calls. That is why all endpoints slow down. The proper fix is a reactive driver, R2DBC with Spring Data R2DBC, so the thread is released while the query runs.

If I have to keep JPA, I wrap the call in Mono.fromCallable and add subscribeOn(Schedulers.boundedElastic()), which moves it to a pool meant for blocking work; then the connection pool becomes the limit. To stop it coming back, I run integration tests with BlockHound, which fails on blocking calls on event loop threads.

And if most of the service's dependencies are blocking, I would honestly move it to Spring MVC with virtual threads, which is simpler to write and debug.

About this answer

Last updated
12 November 2026
Versions it names
Java 13, Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Spring WebFlux endpoint calls a blocking JPA repository inside a map operator. Under...": https://scrivio-interview-library.netlify.app/java/concurrency-patterns/b4f6cdeaa8/