Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

p99 latency in a WebFlux service spikes from 20 ms to 3 seconds under moderate load while CPU usage stays low. Thread dumps show reactor-http-nio threads in socketRead and in a JDBC driver. Explain the cause and the fix.

A blocking JDBC call is running on Netty's event-loop threads. Each connection belongs to one loop, so while a loop waits in socketRead for the database, every other connection on that loop waits too, which is why latency jumps to seconds while CPU stays low: the threads are waiting, not working. As a stopgap we move the call off the loop with Mono.fromCallable(...).subscribeOn(Schedulers.boundedElastic()), the real fix is a non-blocking driver such as R2DBC, and we add BlockHound to the tests and check the other usual blocking sources: logging appenders, DNS lookups, SecureRandom and file I/O.

Why this question is asked

This is one of the most common production problems in WebFlux services, and it is confusing the first time we meet it. The service is not busy, the database looks healthy, yet requests that normally take 20 ms take 3 seconds. The thread dump holds the answer if we know how to read it. Interviewers ask this debugging question because it tests whether we understand how the event loop works and can connect a thread dump to a latency graph. In this answer, we will recap the event-loop model, read the evidence, explain why latency rises while CPU stays low, apply the stopgap and the real fix, and add the checks that stop it from coming back.

1A Quick Recap of the Event-Loop Model

A WebFlux service on Reactor Netty runs on a few event-loop threads, about one per CPU core, named reactor-http-nio-N. Each open connection is bound to one of them for its whole life, and that loop reads its requests, runs our handler code and writes its responses. The model works only if every piece of code run on a loop returns quickly, because while the loop is busy with one task it cannot serve any other connection it owns.

JDBC, the standard Java database API, is blocking: a call such as executeQuery() holds the calling thread until the database replies. Inside the driver that shows up as the thread sitting in a socket read, waiting for bytes from the database server.

2Reading the Evidence

The thread dump in the question shows exactly this. Several reactor-http-nio threads have stack traces that run from our handler, through a repository, into the JDBC driver and down to a socket read.

Text
"reactor-http-nio-3" #41 daemon RUNNABLE
at sun.nio.ch.NioSocketImpl.read(...)
at java.net.Socket$SocketInputStream.read(...)
at org.postgresql.core.VisibleBufferedInputStream.readMore(...)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(...)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(...)
...
at com.example.orders.OrderRepository.findByCustomer(OrderRepository.java:42)
at com.example.orders.OrderHandler.list(OrderHandler.java:27)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(...)
at reactor.netty.http.server.HttpServerOperations.onInboundNext(...)

Notice the two ends of the trace. At the bottom is Reactor Netty handling an inbound request; at the top is a PostgreSQL socket read. The event-loop thread itself is waiting for the database. Notice also that the thread state is RUNNABLE, not WAITING: a thread blocked in a socket read is reported as runnable by the JVM even though it uses no CPU, which misleads people who scan dumps for blocked threads.

Someone probably called a JPA or JdbcTemplate repository from a reactive handler, perhaps wrapped in Mono.just(repository.findByCustomer(id)), which runs the query immediately on whatever thread assembles the Mono.

3Why Latency Rises While CPU Stays Low

Suppose the machine has 8 cores, so 8 event loops, serving 2,000 open connections, about 250 per loop. A query takes 50 ms under moderate load. While loop 3 waits those 50 ms, none of its other 249 connections is read or written. If the next request on that loop also needs the query, the wait repeats. Requests queue behind each other on each loop, so a request's latency becomes the sum of the blocking calls ahead of it. With dozens of queued queries per loop, 3 seconds is easily reached.

Meanwhile the CPU is idle, because the threads are waiting on the network, not computing. Low CPU with high latency is the signature of threads that are blocked; high CPU with high latency would point instead to expensive computation or garbage collection. The p99 rises first because only requests that land behind a blocking call on the same loop are hurt; as load grows, more of them are.

Step throughRequests queued behind a blocking call
Client 1Client 2reactor-http-nio-3PostgreSQL (JDBC)
GET /orders?customer=7
executeQuery (thread blocks)
GET /health (same loop)
cannot read client 2 while blocked
rows after 50 ms
response 1
response 2, 50 ms late for a 1 ms request

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

Even a trivial request waits if it shares a loop with a blocking call.

4The Stopgap and the Real Fix

The stopgap moves the blocking call off the event loop to a scheduler meant for blocking work. The call becomes lazy with fromCallable, so it runs at subscription, and subscribeOn runs that subscription on a boundedElastic thread.

Java
// Broken: the query runs on the event loop
Mono<ServerResponse> list(ServerRequest r) {
List<Order> orders = repository.findByCustomer(r.pathVariable("id")); // blocks nio-3
return ServerResponse.ok().bodyValue(orders);
}
// Stopgap: same JDBC call, on a thread that may block
Mono<ServerResponse> list(ServerRequest r) {
return Mono.fromCallable(() -> repository.findByCustomer(r.pathVariable("id")))
.subscribeOn(Schedulers.boundedElastic())
.flatMap(orders -> ServerResponse.ok().bodyValue(orders));
}
// p99 back to about 20 ms; event loops idle in the selector again
// Real fix: a non-blocking driver end to end
Flux<Order> findByCustomer(String id); // Spring Data R2DBC repository

The stopgap works immediately, but every request still holds a thread while it waits, and the boundedElastic pool, capped at ten times the cores, becomes the new limit. If the service's main job is database access, switching to R2DBC makes it truly non-blocking; if the team prefers to keep JDBC and JPA, a Spring MVC service on virtual threads is often the better architecture than a reactive one wrapped around blocking calls.

5Preventing It: BlockHound and Other Blocking Sources

BlockHound is a Java agent from the Reactor team that detects blocking calls on threads marked as non-blocking, such as event loops and Schedulers.parallel(), and throws an error when one happens. We install it in tests so a blocking call fails the build instead of surprising us in production. On recent JDKs it needs the JVM flag -XX:+AllowRedefinitionToDifferentClass, and its support for the newest JDK releases should be checked for your version.

Java
@BeforeAll
static void installBlockHound() { BlockHound.install(); }
@Test
void listOrdersDoesNotBlock() {
client.get().uri("/customers/7/orders").exchange().expectStatus().isOk();
}
// with the broken handler:
// reactor.blockhound.BlockingOperationError: Blocking call! java.net.Socket$SocketInputStream#read
// on thread reactor-http-nio-2

JDBC is the obvious culprit, but not the only one. Synchronous logging appenders that write to a file or a slow network target block on every log line; an async appender fixes that. DNS resolution through InetAddress blocks, which is why Reactor Netty ships its own non-blocking resolver, but our own code may still call InetAddress. SecureRandom can block while gathering entropy, and UUID.randomUUID() uses it. File reads, Thread.sleep, synchronized sections held by slow code, and blocking calls in third-party SDKs belong on the same list. A load test with BlockHound enabled and a thread dump taken under load are the quickest ways to find the rest.

6Follow-up questions

Why did the problem only show at moderate load?

At low load, few requests share a loop at the moment a query runs, so most requests never wait behind one. As load rises, more connections per loop need service during each blocking call, and the queue behind each call grows, so the tail latency climbs sharply even though the average looks reasonable.

Would adding more event-loop threads fix it?

It would dilute the problem slightly by spreading connections over more loops, but every blocking call would still stall the connections on its loop. It also departs from the event-loop design, which assumes about one loop per core. The fix is to stop blocking on the loops.

Is boundedElastic enough as a long-term solution?

For a few occasional blocking calls in an otherwise non-blocking service, yes, ideally with a dedicated pool sized for the dependency. If most requests block on the database, the service is effectively thread-per-request with extra overhead, and either R2DBC or MVC on virtual threads is the better long-term design.

7Conclusion

In this answer, we traced a p99 spike from 20 ms to 3 seconds to a JDBC call running on Reactor Netty's event loops. Because each connection is bound to one loop, a loop waiting in socketRead delays every connection it owns, and because the threads are waiting rather than working, CPU stays low. We moved the call to boundedElastic with fromCallable and subscribeOn as a stopgap, chose R2DBC or MVC with virtual threads as the real fix, and added BlockHound in tests along with a check of other blocking sources such as logging appenders, DNS, SecureRandom and file I/O.

What to say in the interviewAbout 100 seconds aloud

Open with

The thread dump tells the story: reactor-http-nio threads, the Netty event loops, are sitting in socketRead inside the PostgreSQL JDBC driver, called from our handler. So a blocking JDBC call is running on the event loop.

Then, if they want more

In WebFlux each connection is bound to one event loop, and there are only about as many loops as cores, so with 2,000 connections on 8 loops each loop owns about 250.

While a loop waits 50 milliseconds for the database, none of its other connections is read or written, and requests queue behind each other, so tail latency climbs to seconds. CPU stays low because the threads are waiting on the network, not computing; low CPU with high latency is the signature of blocked threads.

Also note the threads show as RUNNABLE in the dump even though they're waiting. The immediate fix is to move the call off the loop: wrap it in Mono.fromCallable so it's lazy, and subscribeOn(Schedulers.boundedElastic()), which is the pool meant for blocking. That brings p99 back quickly.

The real fix is to go non-blocking end to end with R2DBC, or, if the service is mostly JDBC and JPA, to consider Spring MVC on virtual threads instead.

To stop it coming back, I'd install BlockHound in the test suite so any blocking call on a non-blocking thread fails the build, and I'd check the other usual blocking sources: synchronous logging appenders, DNS through InetAddress, SecureRandom including UUID.randomUUID, and file I/O.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. 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 "p99 latency in a WebFlux service spikes from 20 ms to 3 seconds under moderate load...": https://scrivio-interview-library.netlify.app/spring-boot/webflux/b433dd1db9/