Quick revision
The short answer
Each orTimeout(2, SECONDS) starts its own clock when its stage starts, so three sequential stages can take up to 6 seconds in total, plus some overhead. Proper handling starts from a deadline computed at request start: each call gets only the time remaining until that deadline, an overall timeout on the final future acts as a backstop, the HTTP clients get matching connect and read timeouts so threads and connections are released, and the remaining budget is passed downstream so other services can stop early too.
What to remember
In this answer, we saw that per-stage timeouts measure from each stage's own start, so three sequential 2-second timeouts allow nearly 6 seconds in total. A deadline computed at request start lets each call use only the remaining time, a final orTimeout on the whole chain acts as a backstop, client connect and request timeouts make sure threads and connections are released, and passing the remaining budget downstream lets other services stop early instead of working for nobody.
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.
Each orTimeout(2, SECONDS) starts its own clock when its stage starts, so three sequential stages can take up to 6 seconds in total, plus some overhead. Proper handling starts from a deadline computed at request start: each call gets only the time remaining until that deadline, an overall timeout on the final future acts as a backstop, the HTTP clients get matching connect and read timeouts so threads and connections are released, and the remaining budget is passed downstream so other services can stop early too.
Why this question is asked
Timeouts that are each correct on their own can still add up to a broken budget. This scenario is common: someone wraps every call in the same timeout, and the endpoint's service level silently becomes three times weaker than intended. Interviewers like it because the fix requires thinking about time as a budget shared by the whole request. In this answer, we will see why the waits add up, introduce a deadline, give each call the remaining time, add a backstop, set client timeouts, and pass the budget downstream.
1Why the Timeouts Add Up
A timeout on a stage measures time from the moment that stage's future starts, not from the start of the request. With three calls in sequence, the second call only starts after the first finishes, so each one gets a fresh 2 seconds.
Notice that no timeout ever fires in the worst case. Each call is just under its limit, so every individual timeout is respected while the endpoint misses its 2-second budget by almost four seconds.
2Compute a Deadline at Request Start
The fix is to think of the 2 seconds as one budget for the whole request. At the start, we compute a deadline, an absolute point in time by which the response must be ready. Every later call asks how much time is left until the deadline and uses only that.
Notice that we use System.nanoTime(), which measures elapsed time and does not jump when the wall clock is adjusted. We also keep a small reserve, here 100 ms, for the work our endpoint does after the last call.
3Give Each Call the Remaining Time
Now every call takes its timeout from the deadline. If the first call is fast, later calls get more time; if it is slow, they get less. And if the deadline has already passed, we do not start the call at all.
| Call | Starts at | Timeout given | Took |
|---|---|---|---|
| customers | 0 ms | 1900 ms | 300 ms |
| pricing | 300 ms | 1600 ms | 900 ms |
| shipping | 1200 ms | 700 ms | times out at 1900 ms |
| response | 1900 ms | reserve 100 ms | error returned by 2 s |
Notice that the supplier is called inside within, so the remaining time is computed at the moment each call starts, not when the chain is built. The final orTimeout on the whole chain is a backstop: if something slips through, such as time spent between stages, the user still gets an answer by 2 seconds.
4Set Matching Client Timeouts
orTimeout only stops the waiting. The HTTP request behind a timed-out future keeps running, holding a thread or a connection until it ends on its own. To actually release them, the client must know the same limit. So the remaining time also goes into the request's own timeout, and the client has a short connect timeout.
With the client timeout set, the request is aborted at the same moment our future times out, so a slow dependency cannot pile up abandoned requests in our service.
5Pass the Remaining Budget Downstream
The header in the last snippet carries the remaining time to the downstream service. If pricing knows it has only 1,600 ms, it can set its own database query timeout to fit, skip a slow optional step, or refuse immediately when the budget is already too small, instead of doing work whose result will arrive after we stopped waiting. We send the remaining duration rather than an absolute time, because the servers' clocks may disagree by more than our budget.
The header name here is only an example; teams should agree on one name and format. gRPC has this built in as call deadlines, which it propagates for us.
6Follow-up questions
Why not simply divide the budget, giving each call about 600 ms?
Fixed shares waste time: if the first call takes 50 ms, the others still get only 600 ms each, and a call that is slow but within budget fails needlessly. A shared deadline lets later calls use time the earlier ones did not need, while still guaranteeing the total.
Could we call the three services in parallel instead?
Only if they are independent. Here pricing needs the customer and shipping needs the price, so they must be sequential. When calls are independent, running them in parallel is the best latency fix of all, and the deadline approach still applies to each.
What about retries within the budget?
A retry is only worth starting if the remaining time can fit another attempt, for example at least the typical latency of the call. Otherwise it only adds load and still fails. The deadline makes this check easy: compare remaining() to the expected call time before retrying.
7Conclusion
In this answer, we saw that per-stage timeouts measure from each stage's own start, so three sequential 2-second timeouts allow nearly 6 seconds in total. A deadline computed at request start lets each call use only the remaining time, a final orTimeout on the whole chain acts as a backstop, client connect and request timeouts make sure threads and connections are released, and passing the remaining budget downstream lets other services stop early instead of working for nobody.
Open with
The problem is that each orTimeout starts its own clock. The second call only starts when the first finishes, so it gets a fresh two seconds, and so does the third.
Then, if they want more
Three calls each just under two seconds take almost six, and no timeout ever fires. So I would treat the two seconds as one budget. At the start of the request I compute a deadline, say now plus 1.9 seconds, keeping a little reserve for building the response, using nanoTime so clock changes do not matter.
Then every call takes its timeout from the time remaining until that deadline, computed at the moment the call starts, and if the deadline has already passed I do not start the call at all. On top of that I put an overall orTimeout of two seconds on the final future, as a backstop.
Because orTimeout only stops the waiting, I would also give the HTTP client the same remaining time as its request timeout, plus a short connect timeout, so the connection and thread are actually released.
And I would pass the remaining time downstream in a header, as a duration rather than a timestamp because of clock skew, so the next service can size its own timeouts and give up early instead of doing work nobody will wait for.
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
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "An endpoint has a 2-second budget and calls three services in sequence, each wrapped in...": https://scrivio-interview-library.netlify.app/java/completable-future/7420ddf356/