Quick revision
The short answer
The pipeline is userIds.flatMap(this::fetch, 10).collectList(). The second argument of flatMap caps the number of inner subscriptions, so at most 10 fetch calls are in flight at once, and collectList gathers the results into a Mono<List<User>> when the source completes. If the list must follow the order of the ids, use flatMapSequential(this::fetch, 10), and add a timeout and onErrorResume to each call so that one slow or failing user does not fail the whole list.
What to remember
In this answer, we wrote userIds.flatMap(this::fetch, 10).collectList(), where the second argument of flatMap limits concurrent inner subscriptions to 10 and collectList gathers the users into one List. We switched to flatMapSequential(this::fetch, 10) when the order must match the input, and added a timeout and onErrorResume to each call inside the lambda, so one slow or failing user does not fail the whole list. Finally we tested that the concurrency limit really holds.
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.
The pipeline is userIds.flatMap(this::fetch, 10).collectList(). The second argument of flatMap caps the number of inner subscriptions, so at most 10 fetch calls are in flight at once, and collectList gathers the results into a Mono<List<User>> when the source completes. If the list must follow the order of the ids, use flatMapSequential(this::fetch, 10), and add a timeout and onErrorResume to each call so that one slow or failing user does not fail the whole list.
Background to this question
Fetching a batch of things from a remote service with bounded concurrency is one of the most common tasks in reactive code: enriching search results, loading details for a page of ids, or calling a partner API that allows only so many parallel requests. It is also a small question with several traps, such as unbounded concurrency, lost order and one error killing the batch. In this answer, we will write the basic pipeline, see how the concurrency limit works, keep the order when needed, make each call resilient, and test it.
1The Basic Pipeline
We have a Flux<Long> of ids and a method fetch(Long id) returning Mono<User> that calls a remote service, typically with WebClient. For each id we want to start an asynchronous call, which means a flattening operator, and we want at most 10 calls running at the same time.
map would not work here, because map(this::fetch) would give a Flux<Mono<User>> and nobody would subscribe to the inner Monos. flatMap subscribes to each Mono returned by fetch and merges the users into one Flux, and collectList turns that Flux into a single Mono holding a List.
2How the Concurrency Limit Works
flatMap's second argument is its maximum concurrency. Internally, flatMap first requests 10 ids from the source and subscribes to 10 fetch calls. Each time one of them completes, it requests one more id and starts one more call. So there are never more than 10 calls in flight, and the source is pulled only as fast as calls finish, which is backpressure working for us. Without the argument, flatMap would allow up to 256 concurrent calls, which could overload the user service or trip its rate limit.
- Request 10 ids from the sourcedemand equals the concurrency
- Subscribe to 10 fetch calls10 requests in flight
- A call completesits user goes downstream to
collectList - More ids left in the source?YesRequest 1 id and start 1 callNoWait for the remaining calls
- Source and all calls complete
collectListemits the List of 250 users
5 steps in order. Play them, or step through them one at a time.
3Keeping the Input Order
flatMap emits users in the order the calls complete, so a fast response for id 7 can come before a slow one for id 3. If the caller needs the list in the same order as the ids, for example because the ids came from a sorted search, we switch to flatMapSequential with the same concurrency. It still runs 10 calls at a time but buffers early results until the earlier ones arrive.
concatMap would also preserve the order, but it runs one call at a time, so 250 calls of 100 ms each would take 25 seconds instead of about 2.5.
4Making Each Call Resilient
By default, an error in any inner Mono fails the whole flatMap: the remaining calls are cancelled and collectList never emits a list. If one user service instance hangs, the whole batch waits forever. Usually partial success is better, so we protect each call on its own: a timeout so that no call waits forever, and onErrorResume so that a failed call becomes an empty result, or a placeholder, instead of an error for the whole stream.
Notice that the timeout and onErrorResume are attached inside the lambda, to each fetch call, not after flatMap. Attached after flatMap, they would apply to the whole stream, and the first error would still cancel everything. If a short retry makes sense, retryWhen(Retry.backoff(2, Duration.ofMillis(100))) goes inside as well, before the timeout or with an overall timeout around it. And if the caller must know which users failed, we can map failures to a result type that records the id and the error instead of dropping them.
5Testing the Pipeline
StepVerifier from reactor-test lets us check the result, and a stub fetch can record the maximum number of concurrent calls to prove the limit holds.
6Follow-up questions
What happens if userIds never completes?
collectList only emits when its source completes, so with an infinite Flux it never emits and the list grows in memory. For endless streams we either take a bounded number of ids, group them into batches with buffer or window, or process users one by one instead of collecting.
Why is fetch called with a method reference and not wrapped in defer?
Because flatMap calls the function only when an id arrives, and a WebClient call returns a lazy Mono that does nothing until subscribed. If fetch did blocking work or side effects in its body, we would make it lazy with Mono.defer or fromCallable.
How do you choose the value 10?
From the limits of the downstream service and our own resources: its rate limit or recommended parallelism, our HTTP connection pool size, and measured latency. If the service allows 20 requests per second and each call takes 200 ms, 4 concurrent calls already reach that rate, so 10 would be too many.
7Conclusion
In this answer, we wrote userIds.flatMap(this::fetch, 10).collectList(), where the second argument of flatMap limits concurrent inner subscriptions to 10 and collectList gathers the users into one List. We switched to flatMapSequential(this::fetch, 10) when the order must match the input, and added a timeout and onErrorResume to each call inside the lambda, so one slow or failing user does not fail the whole list. Finally we tested that the concurrency limit really holds.
Open with
I would write userIds.flatMap(this::fetch, 10).collectList(). flatMap is needed because fetch returns a Mono; map would give me a Flux of unsubscribed Monos. The second argument is the concurrency: flatMap requests 10 ids, subscribes to 10 fetch calls, and each time one completes it pulls one more id, so there are never more than 10 calls in flight and backpressure keeps the source in step.
Then, if they want more
Without it, the default would allow 256 concurrent calls, which could easily overload the user service. collectList then gives me a Mono of List of User once all ids are processed. flatMap emits in completion order, so if the list must match the order of the ids I'd use flatMapSequential(this::fetch, 10), which still runs 10 at a time but buffers early results. concatMap would keep order too, but one call at a time.
Then I'd make each call resilient inside the lambda: fetch(id).timeout of a couple of seconds, and onErrorResume that logs and returns Mono.empty or a placeholder. That has to go on each inner call, because an error from any inner Mono otherwise cancels the whole flatMap and the list never arrives.
In a test I'd use StepVerifier and a stub that tracks the peak number of concurrent calls to prove it never exceeds 10.
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.
- 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 have Flux<Long> userIds and a method Mono<User> fetch(Long id) that calls a remote...": https://scrivio-interview-library.netlify.app/spring-boot/reactive-fundamentals/eaf3e4f4b2/