Scrivio Interview library
ScenarioIntermediate8 min read · updated 12 November 2026

A WebFlux service makes three independent WebClient calls per request and returns a combined result. Latency equals the sum of the three. What is wrong in code like a.flatMap(x -> b.flatMap(y -> c.map(z -> combine(x,y,z)))) and how do you fix it?

Nested flatMap calls run one after another: the inner Mono for b is only subscribed once a has produced its value, and c only once b has, so the three calls never overlap and the latency is the sum. Because the calls are independent, we should subscribe to all three at once with Mono.zip(a, b, c) and combine the tuple, which makes the latency roughly the slowest call. Then we add a timeout to each call, decide with onErrorResume what a partial result looks like, and make sure the connection pool is large enough for the extra concurrency.

Why this question is asked

Reactive code looks parallel at first glance, so it surprises many teams when a WebFlux endpoint that fans out to three services is exactly as slow as a blocking one. Interviewers ask this question because the answer shows whether we understand that a Mono is a recipe that does nothing until someone subscribes, and that the operator we pick decides when each subscription happens. In this answer, we will recap how Mono and flatMap behave, see why the nested version is sequential, rewrite it with Mono.zip, and then make it production-ready with per-call timeouts, partial results and a correctly sized connection pool.

1A Quick Recap of Mono and Subscription

A Mono is a publisher of at most one value. The important part is that a Mono returned by WebClient is lazy, or cold: building it with webClient.get().uri(...).retrieve().bodyToMono(Price.class) only describes the request. No socket is opened and no bytes are sent until something subscribes to that Mono. In a WebFlux controller, the framework subscribes to the Mono we return when it is ready to write the response.

flatMap takes the value of one Mono and uses it to build the next Mono. That is its whole purpose: it exists for the case where the second call needs the result of the first, for example loading a customer and then loading the orders for that customer id. So flatMap has to wait for the first value before it can even call our lambda, and only the lambda creates and subscribes to the second Mono.

2Why the Nested flatMap Runs Sequentially

Let us look at the code from the question with three calls that take 120 ms, 80 ms and 150 ms.

Java
Mono<Price> a = pricing.get().uri("/price/{id}", id).retrieve().bodyToMono(Price.class); // 120 ms
Mono<Stock> b = inventory.get().uri("/stock/{id}", id).retrieve().bodyToMono(Stock.class); // 80 ms
Mono<Rating> c = reviews.get().uri("/rating/{id}", id).retrieve().bodyToMono(Rating.class); // 150 ms
Mono<ProductView> view = a.flatMap(x -> b.flatMap(y -> c.map(z -> combine(x, y, z))));
// t = 0 ms subscribe to a, pricing request sent
// t = 120 ms a emits, lambda runs, subscribe to b, inventory request sent
// t = 200 ms b emits, lambda runs, subscribe to c, reviews request sent
// t = 350 ms c emits, combine() runs; total latency 350 ms
Step throughNested flatMap: each call waits for the previous one
HandlerPricingInventoryReviews
subscribe to a: GET /price
price after 120 ms
inner flatMap subscribes to b
stock after 80 ms more
inner flatMap subscribes to c
rating after 150 ms more
combine(x, y, z) at 350 ms

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

The calls never overlap, so the latency is the sum of the three.

Notice that b and c are declared outside the lambdas, so it looks as if they already exist and are running. They are not. They are only descriptions, and the subscription to b happens inside the first lambda, which runs when a emits. The subscription to c happens inside the second lambda, which runs when b emits. Even though the calls do not depend on each other, the shape of the code makes each one wait for the previous one.

So the reactive stack is doing exactly what we asked. We used an operator meant for dependent calls on independent calls, and paid 120 + 80 + 150 = 350 ms instead of about 150 ms.

3The Fix: Subscribe to All Three With Mono.zip

Mono.zip subscribes to every Mono we give it at the same moment, waits until each has emitted, and then emits a tuple holding the three values in order. Because WebClient is non-blocking, three requests in flight need no extra threads: the same event loop sends all three and handles each response when it arrives.

Java
Mono<ProductView> view = Mono.zip(a, b, c)
.map(t -> combine(t.getT1(), t.getT2(), t.getT3()));
// t = 0 ms subscribe to a, b and c; three requests sent
// t = 80 ms stock arrives, zip keeps waiting
// t = 120 ms price arrives, zip keeps waiting
// t = 150 ms rating arrives, combine() runs; total latency 150 ms
// Same thing, with the combinator passed directly:
Mono<ProductView> view2 = Mono.zip(a, b, c).map(TupleUtils.function(this::combine)); // reactor-extra
Step throughMono.zip: the three calls overlap
HandlerPricingInventoryReviews
subscribe to a
subscribe to b
subscribe to c
stock at 80 ms
price at 120 ms
rating at 150 ms
combine(x, y, z) at 150 ms

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

All three requests leave at once; the slowest one decides the latency.

The latency is now the slowest call, 150 ms, not the sum. Two behaviours of zip are worth knowing before we ship it. If any of the three fails, zip fails at once with that error and cancels the other two. And if any of them completes empty, without a value, zip completes empty too and combine() never runs, which can surprise us when a 404 was mapped to Mono.empty(). We handle both cases in the next section.

4Adding Per-Call Timeouts and Partial Results

Running calls in parallel does not protect us from a slow dependency. If the reviews service hangs, the whole response hangs with it. So each call gets its own timeout, sized from that service's measured latency, and we decide for each call what happens when it fails. Prices are essential, so a failure there should fail the request. A rating is nice to have, so we can show the product without it.

Java
Mono<Price> a2 = a.timeout(Duration.ofMillis(300)); // essential: let it fail
Mono<Stock> b2 = b.timeout(Duration.ofMillis(250))
.onErrorResume(e -> Mono.just(Stock.UNKNOWN)); // show "check availability"
Mono<Rating> c2 = c.timeout(Duration.ofMillis(300))
.onErrorResume(e -> Mono.just(Rating.NONE)) // hide the stars
.defaultIfEmpty(Rating.NONE); // empty would cancel the zip
Mono<ProductView> view = Mono.zip(a2, b2, c2)
.map(t -> combine(t.getT1(), t.getT2(), t.getT3()));
// reviews slow (2 s): view after 300 ms, with Rating.NONE
// pricing down: error after at most 300 ms, mapped to 503 by our error handler

Notice that onErrorResume sits on the individual call, not on the zipped result. If we put it after zip, one failing call would cancel the others and we would lose the two good answers. Placing the fallback on each branch turns the failure into a normal value before zip ever sees it. defaultIfEmpty does the same job for the empty case.

We should also log or count each fallback. A page that silently shows no ratings for a week because a timeout is too tight is a quiet failure that nobody notices without a metric.

5Checking the Connection Pool

There is a catch that appears only under load. Before the change, each request held one connection at a time; now it holds up to three, one to each service. WebClient on Reactor Netty keeps a pool of connections per remote host, and when the pool is full, new requests wait in a pending queue until a connection is free. If the pool is too small for the new concurrency, the calls queue inside our own service and the latency comes back, now hidden in connection acquisition rather than in the code.

The defaults are reasonable for many services, but they are not tuned for ours (check your version of Reactor Netty for the current maximum connections and pending-acquire limits). We size the pool from expected concurrency to each host, set a short pending-acquire timeout so waiting fails fast, and watch the pool metrics that Reactor Netty can publish to Micrometer.

Java
ConnectionProvider provider = ConnectionProvider.builder("reviews")
.maxConnections(200) // about requests/s x latency per host, plus headroom
.pendingAcquireTimeout(Duration.ofMillis(100)) // fail fast instead of queuing
.metrics(true) // reactor.netty.connection.provider.* gauges
.build();
WebClient reviews = WebClient.builder()
.baseUrl("http://reviews")
.clientConnector(new ReactorClientHttpConnector(HttpClient.create(provider)))
.build();

With the pool sized, the three calls really do run in parallel under load, and the endpoint's latency follows the slowest dependency instead of the total.

6Follow-up questions

When is nested flatMap the right choice?

When a later call needs the result of an earlier one, such as loading a customer and then their orders by customer id. The waiting is then a real dependency, not an accident of code shape. If some calls are dependent and others are not, we combine both: flatMap for the dependent step, and zip for the independent calls inside it.

What is the difference between Mono.zip and Mono.when?

Mono.zip waits for all sources and gives us their values as a tuple. Mono.when also subscribes to all of them in parallel, but it only signals completion and discards the values, so it fits side effects such as sending three notifications. Both fail as soon as one source fails, unless we handle errors on each source.

Does Mono.zip use more threads?

No. WebClient calls are non-blocking, so the same event loop threads send the three requests and process the three responses. The extra resource is connections, one per call in flight, which is why the pool size matters more than thread count here.

How would you test that the calls really run in parallel?

Start three stub servers, for example with WireMock or MockWebServer, each delaying its response by a known amount such as 200 ms. Call the endpoint and assert that it answers in well under 600 ms. A test that only checks the combined value would pass for both the sequential and the parallel version.

7Conclusion

In this answer, we saw that a WebClient Mono does nothing until it is subscribed, and that flatMap subscribes to the next Mono only after the previous one emits, so nested flatMap turns three independent calls into a chain whose latency is the sum. Mono.zip subscribes to all three at once and brings the latency down to the slowest call. We then gave each call its own timeout, turned failures of optional data into fallback values on each branch so one failure does not cancel the others, and checked the connection pool so the new concurrency does not queue for connections.

What to say in the interviewAbout 80 seconds aloud

Open with

The problem is that flatMap is for dependent calls. A WebClient Mono is lazy, so in a.flatMap(x -> b.flatMap(...)) the subscription to b only happens inside the lambda, after a has emitted, and c only after b.

Then, if they want more

The three requests never overlap, so with 120, 80 and 150 milliseconds we pay 350 instead of 150. Since the calls are independent, I would use Mono.zip(a, b, c) and map the tuple to combine(t.getT1(), t.getT2(), t.getT3()). Zip subscribes to all three at once, and because WebClient is non-blocking it does not need more threads.

Then I would make it robust. Each call gets its own timeout based on that dependency's latency, and for data that is optional I put onErrorResume and defaultIfEmpty on that branch, so a slow reviews service gives me a page without ratings instead of cancelling the whole zip. Essential data like price I let fail.

Last, I would check the Reactor Netty connection pool, because each request now holds three connections at once instead of one, and if the pool is too small the calls just queue for connections and the latency comes back.

I would size it from throughput times latency per host, set a short pending-acquire timeout, and watch the pool metrics after the change.

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 "A WebFlux service makes three independent WebClient calls per request and returns a...": https://scrivio-interview-library.netlify.app/spring-boot/http-clients/8c87720cf9/