Quick revision
The short answer
We wrap the call in Mono.fromCallable(() -> sdk.lookup(id)), move it off the event loop with subscribeOn onto a dedicated bounded elastic scheduler, and add a timeout. Little's law sizes it: 2,000 requests per second times 0.2 seconds means about 400 calls in flight at once, which is more than the default boundedElastic cap of ten threads per core, so we create our own scheduler with room for that load, bound concurrency with a semaphore that sheds load when full, and plan to replace the SDK.
What to remember
In this answer, we saw that calling a blocking SDK in map freezes the event loop, and that the correct wrapper is Mono.fromCallable with subscribeOn and a timeout. Little's law told us that 2,000 requests per second at 200 ms means about 400 concurrent blocking calls, well above the default boundedElastic cap on a typical machine, so we created a dedicated scheduler with a higher thread cap and a small queue, added a semaphore that sheds load when full, and kept the scheduler's cap above the permit count because timeouts do not free threads. Finally, we planned to replace the SDK or move it behind its own service.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We wrap the call in Mono.fromCallable(() -> sdk.lookup(id)), move it off the event loop with subscribeOn onto a dedicated bounded elastic scheduler, and add a timeout. Little's law sizes it: 2,000 requests per second times 0.2 seconds means about 400 calls in flight at once, which is more than the default boundedElastic cap of ten threads per core, so we create our own scheduler with room for that load, bound concurrency with a semaphore that sheds load when full, and plan to replace the SDK.
Why this question is asked
Almost every reactive service eventually has to call something that blocks: an old vendor SDK, a JDBC driver, a library that does file I/O. The question is not whether this is allowed, it is whether we know how to do it without freezing the event loop and without running out of threads. Interviewers like this question because it combines code with arithmetic: a candidate who writes the right operator chain but cannot say how many threads it needs has only solved half the problem. In this answer, we will see why the naive call breaks WebFlux, write the correct wrapper, size it with Little's law, isolate it on its own scheduler, add a concurrency limit with load shedding, and finish with the long-term plan.
1Reproducing the Problem: Calling the SDK Directly
The simplest code calls the SDK inside a map. It compiles, it works in a demo, and it is wrong.
map runs on whatever thread delivers the value, which here is a Netty event-loop thread. Each lookup parks that thread for 200 ms, so one event loop can serve at most 5 requests per second. With 8 event loops the whole server tops out at about 40 requests per second, against a target of 2,000. Worse, every other request sharing those event loops, even ones that never touch the SDK, waits behind the blocked calls.
2The Correct Wrapper: fromCallable, subscribeOn and timeout
The fix is to make the call lazy and move it to a thread that is allowed to block.
5 messages in order. Play them, or step through them one at a time.
The event loop never waits; only the scheduler's thread is parked during the call.
Each part has a reason. Mono.fromCallable defers the call until subscription, so the SDK is not invoked while we are still building the pipeline, and an exception thrown by the SDK becomes an error signal instead of escaping. subscribeOn tells Reactor to perform the subscription, and therefore the callable, on a worker of the given scheduler. The event loop only hands over the work and is free again immediately. When the SDK returns, the result travels back down the pipeline and Netty writes the response.
The timeout protects the caller: if the SDK hangs, the request fails after 500 ms with a TimeoutException that we can map to a 503 or a fallback. But there is a catch: timeout only stops waiting. The worker thread is still inside sdk.lookup, and whether it is freed early depends on whether the SDK responds to interruption. So a hanging SDK still holds threads, which is why sizing matters.
3Sizing With Little's Law
Little's law says that the average number of items in a system equals the arrival rate times the time each item spends inside. For us that is 2,000 requests per second times 0.2 seconds, which is 400 calls in flight at any moment on average. Every one of them holds a thread for its whole duration, so we need about 400 threads just for the average case.
Now compare that with the default. Schedulers.boundedElastic() caps itself at ten threads per CPU core, so 80 threads on an 8-core machine, and queues extra tasks (up to 100,000 per thread) instead of creating more. With 80 threads and 400 tasks arriving, the queue grows without limit: requests wait in line, latency climbs, and the 500 ms timeout starts failing requests that never even reached the SDK. The average is not enough either. If the SDK's latency rises to 400 ms during a bad minute, Little's law gives 800 in flight. So we size for a realistic peak, say 500 to 600 threads, and accept that each platform thread costs some memory for its stack, often reserved at 1 MB and committed as used, which is affordable at this scale.
| SDK latency | Calls in flight | Default cap on 8 cores |
|---|---|---|
| 100 ms | 200 | 80 threads: queueing |
| 200 ms (normal) | 400 | 80 threads: queueing |
| 400 ms (degraded) | 800 | 80 threads: heavy queueing |
4Isolating the SDK on Its Own Scheduler
Instead of the shared boundedElastic, we give this dependency its own scheduler. The reason is isolation, the bulkhead idea from ship design: if the SDK slows down and fills its threads, only calls to the SDK suffer, while other blocking work in the service, such as file uploads, keeps its own threads.
A small queue cap is deliberate. A large queue only hides overload: tasks wait so long that they time out anyway. A rejected task fails fast with an error we can turn into a 503, which tells the client to back off. The thread name prefix makes the threads easy to spot in a thread dump, and enabling Reactor's scheduler metrics shows how busy the pool is.
5Bounding Concurrency and Shedding Load
A thread cap limits threads, but we also want an explicit rule for how many SDK calls we let start. A semaphore with tryAcquire does this without blocking: if no permit is free, we reject the request at once.
Mono.defer matters here: it makes the permit check happen per subscription, not once when the method is called. doFinally releases the permit on every outcome, including cancellation. Notice one subtlety: after a timeout, the permit is released while the thread may still be stuck in the SDK, so the scheduler's thread cap must be a little higher than the permit count. That is why we chose 500 permits and 600 threads. For batch work inside a Flux, flatMap(id -> lookup(id), 400) gives the same kind of bound through its concurrency argument. Resilience4j's bulkhead and rate limiter offer the same ideas as reusable operators.
6The Long-Term Plan
All of this makes the SDK survivable, not good. We are holding 400 to 600 threads to wait on a vendor, which is exactly the cost reactive programming was meant to avoid. The lasting fixes are to replace the SDK with its HTTP API called through WebClient, ask the vendor for an asynchronous client, or move the SDK into a small separate service built on Spring MVC with virtual threads, where blocking is cheap and the reactive service talks to it over HTTP. Until then, the dedicated scheduler, the limits and the metrics keep one slow dependency from taking the whole service down.
7Follow-up questions
Could we run the SDK calls on virtual threads instead?
Yes, on Java 21 and later. We can build a scheduler with Schedulers.fromExecutorService(Executors.newVirtualThreadPerTaskExecutor()), and recent Reactor versions can make boundedElastic use virtual threads through a system property; check your version. Virtual threads make 400 or 800 waiting calls cheap in memory, but they do not remove the need for a concurrency limit, because the SDK and whatever sits behind it still have a capacity.
Why not use publishOn instead of subscribeOn?
publishOn moves the signals that come after it to another thread, while subscribeOn moves the subscription, which is where fromCallable executes its work. For a blocking source created with fromCallable, subscribeOn is the right tool. publishOn would be the choice if the blocking work were in a map further down a pipeline.
What do we return to the client when the semaphore is full?
A 503 Service Unavailable, ideally with a Retry-After header, produced by mapping SdkOverloadedException in a controller advice. Failing fast is kinder than queueing: the client gets an answer in microseconds and can retry or degrade, instead of waiting 500 ms for a timeout.
8Conclusion
In this answer, we saw that calling a blocking SDK in map freezes the event loop, and that the correct wrapper is Mono.fromCallable with subscribeOn and a timeout. Little's law told us that 2,000 requests per second at 200 ms means about 400 concurrent blocking calls, well above the default boundedElastic cap on a typical machine, so we created a dedicated scheduler with a higher thread cap and a small queue, added a semaphore that sheds load when full, and kept the scheduler's cap above the permit count because timeouts do not free threads. Finally, we planned to replace the SDK or move it behind its own service.
Open with
I would wrap the call as Mono.fromCallable of sdk.lookup, then subscribeOn a scheduler meant for blocking work, then a timeout of about 500 milliseconds. fromCallable makes it lazy and turns exceptions into error signals, and subscribeOn moves the actual call off the Netty event loop, so the event loop stays free.
Then, if they want more
Then I size it with Little's law: 2,000 requests per second times 0.2 seconds is about 400 calls in flight on average, and more when the SDK slows down. The default boundedElastic caps at ten threads per core, so 80 on an 8-core box, which would just queue up and time out.
So I create a dedicated scheduler with Schedulers.newBoundedElastic, around 600 threads and a small queue cap, named after the dependency, so it is isolated from other blocking work and easy to see in metrics.
I also add a semaphore with tryAcquire, maybe 500 permits, inside Mono.defer, released in doFinally, and when it is full I fail fast with a 503 rather than queue. The thread cap stays above the permit count because a timeout does not free a thread stuck in the SDK.
Finally, I would treat this as a stopgap and plan to replace the SDK with an HTTP client or move it into a separate service running on virtual threads.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- 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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring WebFlux, Project Reactor reference guide
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 "You must call a legacy blocking SDK (sdk.lookup(id) takes 200 ms) from a WebFlux service...": https://scrivio-interview-library.netlify.app/spring-boot/webflux/583c21e57d/