Quick revision
The short answer
We start each downstream call with CompletableFuture.supplyAsync on a bounded executor or a virtual-thread executor, cap each one with completeOnTimeout (or orTimeout plus exceptionally) so it can never exceed the 500 ms budget, wait with allOf, and read each result, using a default for any part that failed or timed out. We also set HTTP client connect and read timeouts so threads are not held after we stop waiting; with WebClient, Mono.zip with timeout and onErrorReturn operators is the reactive alternative.
What to remember
In this answer, we called three downstream services in parallel with CompletableFuture.supplyAsync on a virtual-thread or bounded executor, capped each call with completeOnTimeout or orTimeout and exceptionally, combined them with allOf and used a default for every failed or late part. We saw that these timeouts stop waiting but not the work, so the HTTP client needs its own read timeout, and we looked at the reactive version with WebClient, Mono.zip and timeout operators.
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 start each downstream call with CompletableFuture.supplyAsync on a bounded executor or a virtual-thread executor, cap each one with completeOnTimeout (or orTimeout plus exceptionally) so it can never exceed the 500 ms budget, wait with allOf, and read each result, using a default for any part that failed or timed out. We also set HTTP client connect and read timeouts so threads are not held after we stop waiting; with WebClient, Mono.zip with timeout and onErrorReturn operators is the reactive alternative.
Why this question is asked
A composer service, one that builds a single response from several downstream services, lives or dies by its latency budget. If it waits for the slowest dependency every time, one bad service makes every page slow. Interviewers ask for this approach in code because it tests whether we know the Java concurrency tools well, and whether we remember the details that break in production, such as timeouts that stop waiting but do not stop the work. In this answer, we will write the composer with CompletableFuture, see why each piece is there, handle partial results, configure the HTTP client, and then show the reactive version with WebClient.
1A Quick Recap of the Problem
Suppose a product page needs details from a catalogue service, a price from a pricing service and reviews from a reviews service. Called one after another, with typical times of 120, 80 and 150 ms, the page takes 350 ms; on a bad day, when reviews takes two seconds, the page takes over two seconds. We want the three calls to run at the same time, so the total is roughly the slowest of them, and we want a hard ceiling of 500 ms after which we return whatever we have.
Return what is available means deciding, for each part, whether the page can live without it. Reviews are nice to have, so an empty list is a fine default. A missing price may be acceptable if we show the page with a Price unavailable label, while missing product details might mean the request should fail. Those decisions are part of the design, not an afterthought.
2The CompletableFuture Version
Here is the composer with plain Java and Spring's RestClient. Each call is started on an executor, given a deadline and a fallback, and then the results are combined.
Let us go through the pieces. supplyAsync runs the call on our executor instead of the common ForkJoinPool. That matters because the common pool is small (about one thread per CPU core) and shared with the whole JVM, so blocking HTTP calls on it would starve other work. A virtual-thread executor creates a cheap virtual thread per task, which suits blocking I/O well; a bounded platform-thread pool, for example 50 threads, is the alternative on older Java versions.
completeOnTimeout, available since Java 9, completes the future with a default value if it has not finished in time. orTimeout is its sibling: it completes the future with a TimeoutException instead, which we can then turn into a default with exceptionally. Either way, no single call can push the composer past its budget. We use 450 ms rather than 500 to leave room for building and serialising the response.
exceptionally catches a failed call, such as a 503 from the service or a connection refused, and replaces it with the same default. Then allOf gives us a future that completes when all three are complete. Because every future already has a deadline and a fallback, allOf can never wait longer than about 450 ms, and join() on each part returns immediately without throwing.
3The Catch: Timeouts That Stop Waiting but Not the Work
completeOnTimeout makes our future finish, but it does not interrupt the HTTP call running underneath. If the reviews service takes 10 seconds, the thread making that call stays busy for 10 seconds, holding a connection from the pool. Under load, these abandoned calls can exhaust connections and threads, and the composer slows down even though every request respects its budget.
So the HTTP client must have its own timeouts, set to the same budget. With RestClient we configure them on the request factory; recent Spring Boot versions also offer properties for HTTP client timeouts, though the property names have changed between versions, so check yours.
7 messages in order. Play them, or step through them one at a time.
With virtual threads the cost of a blocked thread is small, but the connection and the load on the downstream service are not, so the client timeout is still needed. If we want to protect a struggling downstream service further, a bulkhead, such as a Semaphore that limits concurrent calls to reviews to 100, or Resilience4j's bulkhead, keeps one service from consuming all the composer's capacity.
4The Reactive Alternative With WebClient
In a reactive application built on Spring WebFlux, the same idea uses WebClient, which returns Mono values that do not block threads at all. Mono.zip subscribes to all three at once and combines them when they are all done.
Here timeout does cancel the underlying request, because cancelling a reactive subscription closes the call, so the abandoned-work problem is smaller. The trade-off is that the whole call path must be reactive, and mixing it with blocking code brings back the thread problems. For a team already using Spring MVC, CompletableFuture with virtual threads is usually the simpler choice.
5Follow-up questions
Why not use parallel streams for the three calls?
Parallel streams run on the common ForkJoinPool, which is sized for CPU work and shared by the whole JVM. Blocking HTTP calls there can starve unrelated tasks, and parallel streams give no way to set a timeout per call. CompletableFuture with our own executor gives control over both.
How would you pass a shrinking budget to calls made later in the request?
We compute a deadline at the start of the request, for example now plus 500 ms, and give each later call the remaining time instead of a fixed timeout. Some teams pass the deadline downstream in a header so the called service can also stop early when the caller has given up.
How do you tell the client that the response is partial?
We can include a field such as missing listing the parts that were not available, or let each part carry its own status. That lets the mobile or web client show a placeholder for reviews instead of an empty section without explanation, and it helps monitoring count how often each fallback is used.
6Conclusion
In this answer, we called three downstream services in parallel with CompletableFuture.supplyAsync on a virtual-thread or bounded executor, capped each call with completeOnTimeout or orTimeout and exceptionally, combined them with allOf and used a default for every failed or late part. We saw that these timeouts stop waiting but not the work, so the HTTP client needs its own read timeout, and we looked at the reactive version with WebClient, Mono.zip and timeout operators.
Open with
I would start each of the three calls with CompletableFuture.supplyAsync on a dedicated executor, never the common pool, because the calls block on I/O. On Java 21 and later I would use a virtual-thread-per-task executor; otherwise a bounded thread pool.
Then, if they want more
On each future I would add completeOnTimeout with a default value, or orTimeout followed by exceptionally, with something like 450 milliseconds so there is room left in the 500 millisecond budget to build the response. exceptionally also turns errors like a 503 into the same default.
Then I wait with CompletableFuture.allOf and read each part with join, which can no longer throw or wait. For each part I decide whether the page can live without it: reviews can be an empty list, but if the product details are missing I might fail the request.
An important detail is that completeOnTimeout only stops us waiting; the HTTP call keeps running and holds a connection. So I also set the HTTP client's read and connect timeouts to the same budget, for example on the RestClient request factory, and maybe add a bulkhead per downstream.
In a WebFlux service I would use WebClient with Mono.zip, timeout and onErrorReturn instead, where the timeout also cancels the request.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 9
- 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 Cloud documentation, Resilience4j documentation
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 "In a Spring Boot composer service, write the approach to call three downstream services...": https://scrivio-interview-library.netlify.app/spring-boot/microservice-design/bbbe4776b8/