Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A service calls a pricing API twice per request. The code builds Mono<Price> price = client.getPrice(id); and then uses price in two places in Mono.zip(price.map(...), price.map(...)). Why are there two calls and how do you fix it?

client.getPrice(id) returns a cold Mono: it describes the HTTP call but does not make it, and every subscription makes it again. Mono.zip subscribes to each of its sources, and both sources are built on the same price Mono, so the call runs twice. We fix it by making the Mono shared with price.cache() or price.share(), or better by restructuring so there is only one subscription, as in price.map(p -> combine(f(p), g(p))).

Why this question is asked

Duplicate calls are a classic reactive bug because the code reads as if the value were computed once and used twice. The pricing team sees double the traffic, latency budgets are spent twice, and if the API is not idempotent, such as one that reserves stock, the duplicate can do real damage. Interviewers ask this to check that we think of a Mono as a recipe rather than a value. In this answer, we will reproduce the duplicate call, explain why each zip source causes its own call, and go through three fixes, including the trap with cache() on a stored Mono.

1Reproducing the Two Calls

Here is the code from the question with a log line that fires each time the HTTP request is actually sent.

Java
Mono<Price> price = client.getPrice(id) // a WebClient call
.doOnSubscribe(s -> log.info("calling pricing API"));
Mono<Quote> quote = Mono.zip(
price.map(p -> p.net()),
price.map(p -> p.taxFor(region)))
.map(t -> new Quote(t.getT1(), t.getT2()));
quote.subscribe();
// calling pricing API
// calling pricing API

Notice that the line is printed twice for one request. The variable price is used in two places, and each place leads to its own subscription.

2Why Each zip Source Calls the API

A Mono built by WebClient is cold. Calling client.getPrice(id) only assembles a description of the request; nothing goes over the network until something subscribes. And each subscription performs the whole description again: open or borrow a connection, send the request, read the response.

price.map(...) does not subscribe either. It creates a new Mono that, when subscribed, subscribes to price and transforms its value. So the two arguments to zip are two independent Monos that each wrap price. When quote is subscribed, zip subscribes to both arguments so it can wait for both values, each argument subscribes to price, and price, being cold, runs the HTTP call once per subscription. The variable holds a recipe, and we followed it twice.

Step throughOne quote, two subscriptions, two calls
Mono.zipprice.map(net)price.map(tax)Pricing API
subscribe
subscribe
GET /price/42 (call 1)
GET /price/42 (call 2)
price
price
net
tax

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

zip subscribes to every source, and both sources subscribe to the same cold Mono.

3Fix 1: Share the Result With cache() or share()

The quickest fix is to make price remember its result. cache() runs the source on the first subscription and replays the value to every later subscriber. share() keeps one subscription running for all concurrent subscribers.

Java
Mono<Price> price = client.getPrice(id).cache();
Mono<Quote> quote = Mono.zip(price.map(Price::net), price.map(p -> p.taxFor(region)))
.map(t -> new Quote(t.getT1(), t.getT2()));
quote.subscribe();
// calling pricing API (once)

Here cache() is perfectly safe because price is a local variable, created fresh for each incoming request and garbage collected when the request ends. share() would also work in this zip, since both subscriptions happen at once, but cache() is more forgiving: a subscriber that arrives after the value was emitted still gets it rather than triggering a new call.

4Fix 2: Restructure to One Subscription

Often the cleanest fix is to avoid two subscriptions in the first place. We get the price once and compute both parts from the same value.

Java
Mono<Quote> quote = client.getPrice(id)
.map(p -> new Quote(p.net(), p.taxFor(region)));
// calling pricing API (once)
// when one part needs another async call, flatMap from the single value
Mono<Quote> withDiscount = client.getPrice(id)
.flatMap(p -> discounts.forCustomer(customerId)
.map(d -> Quote.of(p, d)));

This version has no hidden state and says exactly what happens: one call, two derived values. zip is the right tool when we combine independent sources, such as a price and a stock level from two services, which should run in parallel. It is the wrong tool for splitting one value into two.

5The Trap: cache() on a Stored Mono

There is a catch with the first fix if the Mono outlives the request. If someone moves price into a field, say to avoid the call for repeated requests, cache() keeps the first value forever, because a cached Mono has no expiry. Prices change, the field does not, and the service quotes yesterday's price until it restarts. It also caches errors: if the first call failed, every later subscriber gets the same error.

Java
// Dangerous: one value for the lifetime of the bean
private final Mono<Price> cachedPrice = client.getPrice(id).cache();
// Bounded: refresh after 30 seconds, do not cache errors for long
private final Mono<Price> cachedPrice = client.getPrice(id)
.cache(p -> Duration.ofSeconds(30), e -> Duration.ZERO, () -> Duration.ZERO);

So cache() without a duration is right for per-request sharing and wrong for long-lived caching, where we need a time-to-live or a real cache.

6Follow-up questions

Would block() on the Mono avoid the duplicate call?

It would compute the value once, but block() on a WebFlux event-loop thread throws an IllegalStateException, and even where it works it ties up a thread while waiting. It also breaks the non-blocking model the service is built on. Composing with map or flatMap gives the same single call without blocking.

Is Mono.zip itself a bad choice?

No. zip is the right choice for combining results of independent sources that should run concurrently, such as a price and a stock check from two different services. The bug is not zip; it is using one cold Mono as two zip sources and expecting it to run once.

How would you catch this in tests?

Count calls to the stubbed API, for example with WireMock's verify(1, getRequestedFor(...)) or a MockWebServer request count, and assert exactly one per quote. A test that checks only the returned quote passes even with duplicate calls.

7Conclusion

In this answer, we saw that the pricing API was called twice because client.getPrice(id) returns a cold Mono and Mono.zip subscribed to two sources that both wrap it, so the call ran once per subscription. We fixed it either by sharing the result with cache() or share(), which is safe for a per-request local variable, or by restructuring to a single subscription with price.map(p -> combine(f(p), g(p))). Finally, we saw that cache() without a duration on a Mono stored in a field keeps the first value, or the first error, forever.

What to say in the interviewAbout 90 seconds aloud

Open with

The Mono from client.getPrice is cold. It's a description of the HTTP request, and every subscription executes it again. price.map doesn't subscribe either; it builds a new Mono that wraps price.

Then, if they want more

So zip gets two independent Monos, and when the quote is subscribed, zip subscribes to both so it can wait for both values. Each one subscribes to price, and price makes its own HTTP call, so we get two requests for one quote. I'd confirm it with a doOnSubscribe log or by counting requests on the stub.

There are two fixes. The quick one is price.cache(), or share(), so the call happens once and both zip branches receive the same value. Because price is a local variable created per request, caching forever is fine here, it's garbage collected after the request.

The cleaner fix is to restructure: there's only one value, so I do one call and derive both parts from it, client.getPrice(id).map(p -> new Quote(p.net(), p.taxFor(region))). Zip is for combining independent sources in parallel, not for splitting one value.

The thing I'd warn about is cache() on a Mono stored in a field: without a duration it keeps the first value, and even the first error, forever, so if we ever want cross-request caching we need cache with a TTL or a real cache like Caffeine.

I'd add a test that asserts exactly one call to the pricing stub per quote.

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 service calls a pricing API twice per request. The code builds Mono<Price> price =...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/220be87d57/