Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

What is BlockHound, and how would you use it in a WebFlux project?

BlockHound is a Java agent from the Reactor team that watches threads marked as non-blocking, such as Netty event loops and Reactor's parallel scheduler, and throws a BlockingOperationError the moment code on one of them makes a blocking call like Thread.sleep or a socket read. In a WebFlux project we install it in tests with BlockHound.install(), whitelist the few known safe calls with allowBlockingCallsInside, and run it in integration tests rather than in production.

Why this question is asked

A WebFlux service runs on a handful of event-loop threads, usually one per CPU core. If one request blocks one of those threads for 200 ms, every other request assigned to that thread waits too, and the service slows down for everyone. The trouble is that blocking calls hide well: a JDBC call deep inside a library, a synchronized logger, a file read in a template engine. Code review rarely catches them and unit tests pass happily. Interviewers ask about BlockHound because it is the standard tool for turning these hidden calls into loud test failures. In this answer, we will recap why blocking is a problem, see how BlockHound detects it, install it in a test, allow the calls that are safe, and talk about where it should and should not run.

1A Quick Recap: Why Blocking Hurts an Event Loop

In Spring MVC, each request gets its own thread from a pool of about 200. If the request waits on a database, its thread sleeps, and the other 199 threads keep working. In WebFlux the model is different. Reactor Netty runs a small number of event-loop threads, and each one handles thousands of connections by never waiting: it starts an I/O operation, registers a callback, and moves on to the next piece of work.

So a blocking call, one that parks the current thread until something finishes, is far more expensive on an event loop than on a request thread. On an 8-core machine there may be only 8 event loops. Block one of them for 200 ms and one eighth of the server's capacity is gone for that time. Block all of them and the service stops responding, even though CPU use looks low. The rule in reactive code is simple: never block a non-blocking thread. Applying that rule is the hard part, because we cannot see which library calls block.

2How BlockHound Detects a Blocking Call

BlockHound is a Java agent, a piece of code that the JVM lets modify classes as they are loaded. When we install it, it instruments a list of known blocking methods in the JDK, such as Thread.sleep, Object.wait, socket and file reads and writes, and the parking methods used by locks. Each instrumented method gets a small check added at its start.

The check asks one question: is the current thread marked as non-blocking? Reactor's parallel and single schedulers create threads that implement the marker interface reactor.core.scheduler.NonBlocking, and Reactor Netty's event loops are covered by an integration that ships with Reactor. If the thread is non-blocking and the call is not on the allow list, BlockHound throws a BlockingOperationError right there, with a stack trace that points at the exact line of our code that called the blocking method. If the thread is an ordinary thread, such as one from boundedElastic, nothing happens, because blocking there is allowed.

Step throughWhat happens when code calls Thread.sleep()
  1. Code calls a blocking JDK methodfor example Thread.sleep(100)
  2. BlockHound check runs firstadded by the agent at class load
  3. Is the thread non-blocking?
    YesCheck the allow listNoCall proceeds normally
  4. Inside an allowed method?
    YesCall proceeds normallyNoThrow BlockingOperationError

4 steps in order. Play them, or step through them one at a time.

Only threads marked non-blocking are checked, so boundedElastic threads may block freely.

3Installing BlockHound in a Test

We add the io.projectreactor.tools:blockhound dependency with test scope, and call BlockHound.install() once before the tests run. A static initializer in a test base class, or a JUnit extension, is the usual place. There is also a blockhound-junit-platform module that installs it automatically for every test run on the JUnit Platform.

Java
class BlockingDetectionTest {
static { BlockHound.install(); }
@Test
void sleepOnParallelThreadIsReported() {
Mono<String> m = Mono.fromCallable(() -> { Thread.sleep(10); return "done"; })
.subscribeOn(Schedulers.parallel()); // parallel threads are NonBlocking
StepVerifier.create(m)
.expectErrorMatches(e -> e instanceof BlockingOperationError)
.verify(); // passes: "Blocking call! java.lang.Thread.sleep"
}
@Test
void sleepOnBoundedElasticIsFine() {
Mono<String> m = Mono.fromCallable(() -> { Thread.sleep(10); return "done"; })
.subscribeOn(Schedulers.boundedElastic()); // ordinary threads, not checked
StepVerifier.create(m).expectNext("done").verifyComplete(); // passes
}
}

Notice that the same code fails or passes depending only on the thread it runs on. That is exactly the rule we want enforced. In a WebFlux integration test using WebTestClient against the running server, any controller, filter or library that blocks on the Netty event loop will now fail the test with a stack trace naming the offending line, instead of slowing production down months later.

On JDK 13 and later, BlockHound needs the JVM flag -XX:+AllowRedefinitionToAddDeleteMethods for the way it instruments classes, so we add it to the Surefire or Gradle test JVM arguments. Support for new JDK releases can lag behind the JDK itself, so check BlockHound's release notes for your Java version before upgrading.

4Allowing Calls That Are Known to Be Safe

Some blocking calls are harmless in practice. A classic example is UUID.randomUUID(), which reads from SecureRandom and can in theory block, or a logging framework that takes a short lock. If BlockHound reports them, the tests fail for calls we have decided to accept. We tell BlockHound about them with the builder.

Java
BlockHound.builder()
.allowBlockingCallsInside("java.util.UUID", "randomUUID") // allowed, even on event loops
.allowBlockingCallsInside("ch.qos.logback.core.OutputStreamAppender", "writeBytes")
.install();

allowBlockingCallsInside takes a class name and a method name, and allows any blocking call made while that method is on the stack. The reason it works on the caller rather than on the JDK method is precision: we want to allow Thread.sleep inside one known method, not everywhere. Libraries can also ship a BlockHoundIntegration, loaded through Java's ServiceLoader, which registers their own allowances; Reactor and several other libraries do this. Keep our own allow list short and commented, because every entry is a decision that a blocking call is acceptable, and that decision should be reviewed like code.

5Where BlockHound Should Run

BlockHound adds a check to every call of every instrumented method, and its instrumentation changes JDK classes at load time. That overhead is acceptable in a test run but not something we want on a production hot path, and an unexpected BlockingOperationError in production would turn a slow request into a failed one. So the usual practice is to run it in integration tests and in a staging profile, not in production.

The best results come from tests that exercise real paths: WebTestClient tests against the application context, tests that use the real WebClient against a mock server, and repository tests that hit a real database through Testcontainers. A unit test with mocks rarely reaches the library code that actually blocks. When BlockHound reports a call, the fix is almost always one of two things: replace the blocking library with a non-blocking one, or move the call onto Schedulers.boundedElastic() with subscribeOn so it runs on a thread made for blocking work.

6Follow-up questions

Does BlockHound find blocking calls that our tests do not execute?

No. It is a runtime check, so it only sees code that actually runs on a non-blocking thread during the test. That is why it belongs in integration tests that exercise realistic paths, including error paths and fallbacks. A blocking call in a branch no test reaches will still slip through.

Why does BlockHound not complain about blocking on boundedElastic?

Because boundedElastic exists to run blocking work. Its threads are ordinary threads and do not carry the NonBlocking marker, so BlockHound's check lets the call proceed. Moving a blocking call there with subscribeOn is the standard fix for what BlockHound reports.

Can we mark our own threads as non-blocking?

Yes. The builder has nonBlockingThreadPredicate, which lets us add a rule such as a thread name prefix, so threads from our own executor are checked too. This is useful when a service runs custom event loops or schedulers that must never block.

7Conclusion

In this answer, we saw that BlockHound is a Java agent that instruments the JDK's blocking methods and throws BlockingOperationError when they are called on a thread marked non-blocking, such as a Netty event loop or a parallel scheduler thread. We installed it with BlockHound.install() in tests, allowed known safe calls with allowBlockingCallsInside, and kept it to integration tests and staging because of its overhead and its fail-fast behaviour. We also noted that it needs a JVM flag on newer JDKs and that its support for new Java versions can lag.

What to say in the interviewAbout 90 seconds aloud

Open with

BlockHound is a Java agent from the Reactor team that detects blocking calls on threads that are supposed to be non-blocking. When it is installed, it instruments JDK methods like Thread.sleep, socket and file I/O and lock parking, and on each call it checks whether the current thread is marked non-blocking, which covers Reactor's parallel scheduler and the Netty event loops.

Then, if they want more

If it is, it throws a BlockingOperationError with a stack trace pointing at our code.

In a WebFlux project I add it as a test dependency and call BlockHound.install() once, usually in a base test class or through the JUnit platform module, and I make sure our WebTestClient integration tests run with it, because those exercise the real controllers, filters and libraries.

When it reports something we have decided is safe, like UUID.randomUUID, I use the builder's allowBlockingCallsInside with the class and method, and keep that list short and reviewed. When it reports a real problem, the fix is either a non-blocking library or moving the call onto boundedElastic with subscribeOn.

I do not run it in production, because it adds overhead to every instrumented call and would turn a slow request into a failed one. On JDK 13 and later it needs the AllowRedefinitionToAddDeleteMethods flag, and support for new JDKs can lag, so I check compatibility before a Java upgrade.

About this answer

Last updated
12 November 2026
Versions it names
Java 13
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 "What is BlockHound, and how would you use it in a WebFlux project?": https://scrivio-interview-library.netlify.app/spring-boot/webflux/576f05ffbf/