What it is
A timeout is the longest a caller is willing to wait for an operation before it gives up and treats the operation as failed. Every remote interaction has several waits that can each hang: opening a connection, getting a connection from a pool, sending the request, and waiting for the answer. A timeout puts an upper bound on each of them, or on all of them together.
Why it exists
A caller that waits is not idle. A thread, an async task, a socket and some memory stay reserved while the call is outstanding. If a dependency slows down and nothing limits the wait, those resources stay reserved for as long as the dependency takes, and new requests have to queue for them. Little’s law makes the effect concrete: concurrency = arrival rate × time held. Hold each worker ten times longer and you need ten times the workers for the same traffic. When the workers run out, the caller is effectively down, including for requests that never needed the slow dependency. That is how one slow component turns into a cascading failure.
A dependency that is down is usually less dangerous: the connection is refused at once and the caller learns quickly. Slow is the hard case, and timeouts exist mostly for it.
How it works
Kinds of timeout. A connect timeout limits establishing a TCP/TLS connection (usually short: tens to a few hundred milliseconds inside a data centre). A request or read timeout limits waiting for the response. A pool acquire timeout limits waiting for a free connection in a client-side pool; library defaults here are often long (30 s is common). A statement timeout on the database side cancels a query that runs too long. An overall deadline limits the entire operation, including retries.
What happens on expiry. The caller stops waiting, frees its resources and reports a failure (an error, a fallback, or a retry if the operation allows it). The other side is not informed unless something tells it: the request may still be queued or running downstream.
Choosing the value. Measure the dependency’s latency distribution when it is healthy and pick a value above its high percentiles, for example a bit above P99.9, or about two to three times P99, then check what that means for capacity: with W workers and a timeout T, a fully hung dependency still lets the caller finish about W / T requests per second. A timeout below the healthy P99 fails at least 1% of perfectly good requests, usually the slowest and often the most valuable ones (big carts, large reports).
Deadline propagation. Instead of a fixed timeout per hop, the first service sets an absolute deadline and every call passes the remaining budget downstream. A service that receives a request whose deadline has passed does not start it; a service whose budget runs out mid-way cancels its own downstream calls. gRPC sends a deadline with every call, and a server that passes the incoming call’s context to its own outgoing calls propagates it; with HTTP, services pass the remaining budget in a header and check it themselves.
When to use it
- On every call that crosses a process boundary: HTTP and gRPC calls, database queries and connection acquisition, cache and queue clients, DNS lookups, calls to third-party APIs.
- On the client side of any user-facing request, so users get an answer (even an error) in bounded time.
- Together with retries and circuit breakers: the timeout produces the failure signal the other two act on.
When not to
- Do not use a short timeout for work that is legitimately long (a large export, a batch job). Make such work asynchronous: accept it, return a job id, and let the client poll or be notified.
- Do not rely on a timeout to protect against overload you could prevent: if a dependency is routinely near its limit, fix capacity or shed load.
- Avoid a timeout that cancels a non-idempotent operation and then retries it blindly (see the Retries lesson).
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Short timeout | Workers are freed quickly; slow dependencies cannot drag the caller down | Healthy-but-slow requests fail; more false failures during brief latency spikes |
| Long timeout (or none) | Few false failures | A slow dependency can hold every worker; users wait a long time for an error |
| Fixed per-hop timeouts | Simple to configure | Inner hops can outlive the caller; work continues after the user left |
| Propagated deadline | Every hop works only within the remaining budget | Needs support in every service and client library |
| Adaptive timeouts (from live percentiles) | Follow real latency | Can drift upward during a slow degradation; harder to reason about |
Alternatives
- Circuit breakers fail calls without waiting at all once a dependency is clearly failing; they need timeouts to detect slowness in the first place.
- Bulkheads cap how many workers one dependency may use, so even a long wait cannot exhaust the whole pool.
- Asynchronous processing takes the slow work off the request path entirely.
- Hedged requests send a second copy after a short delay (for example at P95) and use whichever answers first; they cut tail latency at the cost of extra load and only suit idempotent reads.
- Adaptive concurrency limits shrink the number of in-flight calls when latency rises.
Failure modes
- Missing timeouts: library defaults that are infinite or very long (socket reads, pool waits) are the classic root cause of thread-pool exhaustion.
- Inner longer than outer: an API waits 5 s for a dependency while the user gave up after 2 s; the remaining 3 s are wasted work.
- Timeout below the healthy tail: a constant trickle of failures, often hidden by retries that double the load.
- Timeout plus aggressive retries: every timeout produces another call to the dependency that is already slow, a retry storm.
- Timeout only on the request, not on the connection or the pool: the call hangs before the request timeout even starts counting.
In production
Set connect, request and pool timeouts explicitly in every client; do not trust defaults. Derive values from measured latency (dashboards of P99 and P99.9 per dependency) and revisit them when the dependency changes. Give each user-facing request an overall budget and pass it down, with gRPC deadlines or a header the services respect, and make servers check the remaining budget before starting expensive work. Count timeouts per dependency as a metric and alert on them. Test with injected latency, not just injected errors: a dependency that answers in 10 s reveals missing timeouts that a dead dependency never will.
Common misconceptions
- "A timeout stops the work." It stops the waiting. The server keeps working unless cancellation or a deadline reaches it.
- "A long timeout is the safe choice." A long timeout is what lets a slow dependency take down the caller.
- "Set it to the average latency plus a bit." Averages hide the tail; choose from high percentiles.
- "If the dependency is down, timeouts matter most." A dependency that refuses connections fails fast; timeouts matter most when it is slow.
Interview angle
When a design has a synchronous call chain, say where timeouts go and how they relate: the user-facing timeout is the outer budget, each downstream call gets less, ideally through a propagated deadline. Show the capacity argument (workers / timeout = the throughput left when a dependency hangs) and how you choose the value from P99 or P99.9. Then connect it to retries (only idempotent operations, with backoff) and circuit breakers (stop waiting entirely when failures persist).
Model assumptions
The simulation is a simplified model, not a benchmark. Users send 60 requests per second (Poisson) to an API with 12 workers, 15 ms of own work per request (lognormal, cv 0.3) and room for 60 waiting requests. The API checks a product cache first, modelled as a fixed 60% hit ratio; a miss reads the inventory database with 2 connections and 40 ms per query (lognormal, cv 0.4). An API worker is held for the whole request, including waits. "Slow" makes the database 4× slower. The engine models two timeouts: the users’ end-to-end timeout and the API’s wait for a database connection; a query that has started always runs to completion (no statement timeout, no cancellation), and a request whose user timed out is still processed. Deadline propagation is not modelled: that step is a scripted illustration with live traffic paused. Latency figures count successful requests only. Latency percentiles (P50, P95, P99) are computed over the requests that succeeded in the last 5 simulated seconds: failed, rejected and timed-out requests are not in them and are counted in the Errors tile, which is always shown next to them. Rejecting or timing out the slowest requests therefore improves the percentiles while making the system less reliable, so read the two together. Numbers show behaviour and orders of magnitude only.