Quick revision
The short answer
The convention is an end-to-end deadline set once at the edge and passed downstream as the remaining time in a header. Each service derives its call timeouts from that remaining time minus its own processing budget, uses orTimeout on futures only as a backstop, and sets client connect and read timeouts so resources are freed. Nobody retries when the remaining time cannot fit another attempt. We deliver it through the shared client libraries and measure timeouts per dependency.
What to remember
In this answer, we saw that fixed timeouts chosen team by team are both too long, when they exceed the user's budget, and too short, when they fail work that still had time. The convention sets one deadline at the edge, passes the remaining time downstream in a header, derives each call's timeout from the remaining time minus the service's own processing budget, uses orTimeout only as a backstop to client connect and read timeouts, and allows a retry only when another attempt fits. Shared libraries make it automatic, and per-dependency timeout metrics show whether it works.
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 convention is an end-to-end deadline set once at the edge and passed downstream as the remaining time in a header. Each service derives its call timeouts from that remaining time minus its own processing budget, uses orTimeout on futures only as a backstop, and sets client connect and read timeouts so resources are freed. Nobody retries when the remaining time cannot fit another attempt. We deliver it through the shared client libraries and measure timeouts per dependency.
Background to this question
Fixed timeouts chosen independently by each team cause two opposite failures. A service far down the chain with a short fixed timeout fails a request that still had plenty of time, which users see as premature errors. A chain of services with generous fixed timeouts adds them up, which users see as long waits. No single team can fix this alone, because the right timeout for a call depends on how much time the whole request has left. In this answer, we will see why fixed timeouts fail in both directions, define the deadline convention step by step, handle retries, and then make it real through shared libraries and measurement.
1Why Fixed Timeouts Fail in Both Directions
Consider a request with a 3-second user-facing budget that passes through the gateway, an order service and a pricing service. Each team picked its own timeout. The gateway waits 3 seconds for the order service, the order service waits 5 seconds for pricing, and pricing waits 1 second for its database.
| Hop | Fixed timeout | Effect |
|---|---|---|
| user -> gateway | 3 s budget | user gives up at 3 s |
| gateway -> order | 3 s | returns error at 3 s |
| order -> pricing | 5 s | keeps waiting 2 s after the user left |
| pricing -> database | 1 s | fails early though 2.5 s remained |
Long waits happen because the order service's 5-second timeout is longer than the user's whole budget: the gateway gives up at 3 seconds while the order service keeps waiting, and pricing keeps working for 2 more seconds on a result nobody will read. Premature failures happen at the bottom: pricing's fixed 1-second database timeout fails a slow query even when the request still had 2.5 seconds left. Each timeout made sense locally; together they are both too long and too short.
2Set the Deadline Once, at the Edge
The first rule is that only the edge, the gateway or the first service a request reaches, decides how long the whole request may take. It sets the deadline, for example 3 seconds for an interactive page, and every downstream call receives the remaining time in a standard header. We pass a duration rather than an absolute timestamp, because servers' clocks can differ by more than our budget; each service converts the received duration into its own local deadline with System.nanoTime() as soon as the request arrives.
Notice the default and the cap. A request without the header still gets a sensible default, and a service never accepts a budget larger than its own maximum, so a bad caller cannot ask it to work for ten minutes. The header name is an example: the teams agree on one. gRPC propagates deadlines natively, so gRPC services get this for free.
3Derive Each Call's Timeout From the Remaining Time
Inside a service, each outgoing call's timeout is the remaining time minus the service's own processing budget: the time it needs after the call returns to build its response. If the order service needs 50 ms after pricing answers, then with 2,400 ms remaining it gives pricing 2,350 ms. If the result is too small to be useful, below the typical latency of the call, the service fails fast instead of starting work it cannot finish.
7 messages in order. Play them, or step through them one at a time.
Every hop works within the time the user still has.
Notice the two layers. The client's own connect and read timeouts, set from t, are what actually free the connection and thread. orTimeout, set slightly above, is a backstop that guarantees the future completes even if the client misbehaves; on its own it would only stop the waiting and leave the request running.
4Retry Only When Another Attempt Fits
Retries are where fixed timeouts do the most damage, because a retry with a fresh timeout can double or triple the wait. Under the convention, a retry is allowed only if the remaining time can fit another full attempt, measured as the typical latency of that dependency, for example its p95. If pricing usually answers in 200 ms and 150 ms remain, we do not retry; we return the error or a fallback now.
This also protects struggling services. When a dependency is slow, deadlines run out quickly and retries stop by themselves, instead of piling extra load onto the service that is already failing.
5Make It Real: Shared Libraries and Measurement
A convention that each team must implement by hand will be implemented ten different ways. So the deadline lives in the shared client and server libraries. The server side reads the header, creates the local deadline and stores it in the request context. The client side reads that context, computes the call timeout, sets the client's connect and read timeouts, adds the orTimeout backstop, applies the retry rule, and writes the header for the next hop. A team that uses the standard clients gets the whole convention without writing any timeout code.
Then we measure, per dependency: how often calls time out, how much budget remained when they started, and how often a service refused work because the deadline had passed. These numbers show where budgets are too tight, such as a dependency that always gets less time than its p99 needs, and they let us check that premature failures and long waits have both gone down after the rollout.
6Follow-up questions
Why pass remaining time instead of an absolute deadline?
Because each service would compare the absolute time with its own clock, and clocks on different machines can be off by hundreds of milliseconds or more. A remaining duration only depends on each service measuring elapsed time locally, which nanoTime does reliably. The cost is that network transit time is not subtracted, which the per-service processing reserve covers.
What happens to background work started by a request?
Work that must finish regardless of the user, such as writing an event to an outbox, should not inherit the request deadline. It gets its own budget, and the library offers a way to start a call without the propagated deadline, used deliberately.
How do you choose the edge deadline?
From the user experience and measured latency: for interactive pages often 2 to 5 seconds, for internal batch APIs longer. It should be above the p99 of a healthy request with some margin, and it should be documented per endpoint so downstream teams know what to expect.
7Conclusion
In this answer, we saw that fixed timeouts chosen team by team are both too long, when they exceed the user's budget, and too short, when they fail work that still had time. The convention sets one deadline at the edge, passes the remaining time downstream in a header, derives each call's timeout from the remaining time minus the service's own processing budget, uses orTimeout only as a backstop to client connect and read timeouts, and allows a retry only when another attempt fits. Shared libraries make it automatic, and per-dependency timeout metrics show whether it works.
Open with
The root problem is that each team picks timeouts without knowing how much time the request has left, so some timeouts are longer than the user's whole budget and others fail work that still had time.
Then, if they want more
My convention is one end-to-end deadline. The edge, the gateway, decides the budget, say three seconds, and every outgoing call carries the remaining time in a standard header, as a duration rather than a timestamp, because clocks differ between machines.
Each service turns that into a local deadline with nanoTime on arrival, capped at its own maximum, with a default when the header is missing. For every outgoing call, the timeout is the remaining time minus the service's own processing budget, and if that is less than the call usually needs, the service fails fast.
The client's connect and read timeouts are set from that value, because those are what free connections and threads, and orTimeout on the future is only a backstop slightly above it. Retries happen only when the remaining time can fit another typical attempt.
None of this should be written by hand in each team, so I would build it into the shared server and client libraries, and measure per dependency how often calls time out and how much budget they had, so we can see that both premature failures and long waits went down.
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 "Requests pass through several services, each composing CompletableFutures with its own...": https://scrivio-interview-library.netlify.app/java/completable-future/db29f6053e/