TIMEOUTS
A call without a timeout is a promise to wait forever
4 workers, no timeout. The db hangs, and from then on a request that needs it arrives every 400 ms. Health checks need no dependency. When does the first health check start to wait?
A service handles requests on a fixed pool of worker threads. A worker that calls a dependency without a timeout waits as long as the dependency takes, and a dependency that has stopped responding takes forever. Each new request that touches it parks one more worker. After workers such requests, the pool is empty and every request, including the ones that never needed the dependency, queues behind calls that will never return.
A timeout turns a hang into an error after a bounded wait and gives the worker back. A deadline budget goes one step further: each request carries one end-to-end deadline, and each hop gets only what is left of it, so two sequential hops cannot each spend the full amount.
// Every worker thread runs:
const token = await auth.verify(req); // no timeout
const row = await db.query(sql, params); // no timeout: hangs forever if db does
return render(row);// One deadline per request, shrinking per hop.
// The timeout must cancel the call (abort signal, driver statement timeout),
// not just stop waiting for it, or the connection stays held.
const deadline = Date.now() + 1000;
const left = () => deadline - Date.now();
const token = await auth.verify(req, { signal: AbortSignal.timeout(left()) });
if (left() <= 0) throw new DeadlineExceeded();
const row = await db.query(sql, params, { signal: AbortSignal.timeout(left()) });
return render(row); Watch the invariant strip. NO TIMEOUT turns both lines red once the pool is full. PER-CALL TIMEOUT keeps health checks alive but holds a worker 1200 ms against a 1000 ms deadline, because hop 2 got a fresh timeout after hop 1 spent 200 ms. DEADLINE BUDGET keeps both green under the same arrivals.
Per-call timeouts, no budget. A request that needs the db arrives every 400 ms once it hangs; a healthy db call takes 400 ms. With 4 workers the answer is 1000 ms: three stuck calls may overlap, but not four. Shrink WORKERS to 3 and find the largest timeout for which the healthy request #1 still succeeds and the pool never fully drains during the hang.
GOAL: WORKERS below 4, request #1 served, pool never at 0 free workers, and one notch higher on TIMEOUT drains it
A request has one end-to-end deadline and must make its hops in order. Rewrite callWithBudget so the total wall time never exceeds deadlineMs, no hop is called once the budget is spent, and the result is \{ ok, ms \} with the total time spent.
HTTP clients with no default timeout (most of them), database drivers with connectTimeout set but no query timeout, DNS lookups inside a request path, thread pools sized for the happy path. The symptom is a service that looks idle while every request hangs, and a health check that fails for a dependency it never uses.
“One slow downstream took the whole service down. Why?” Explain pool exhaustion, then say where the timeout value comes from (dependency p99, caller's deadline) and how a deadline budget propagates through hops. Bonus: point out that a timeout without a retry policy is an error, and a retry without a budget is the next lesson's incident.
USED IN
Chapters that build on this lesson.
- Technical decisions as a managerEngineering Manager
- Idempotency and moving money exactly oncePayments
- Inside a switch: routing, timeouts, PIN translation and stand-inPayments
- USSD: the telco, the bank and who pays for the sessionPayments
- When the money is wrong: payments incidentsPayments
- API, contract and integration testingQA and Quality Engineering
- Mobile and accessibility testingQA and Quality Engineering
- Technical debt and the trade-offs you defendSoftware Engineer
- Performance and load tests as part of the suiteSoftware Testing and Quality
- Chaos and resilience testingSRE, Platform and DevOps
- Production readiness reviewsSRE, Platform and DevOps
- How technical a TPM must beTechnical Program Manager