TIMEOUTS

A call without a timeout is a promise to wait forever

L114 MINRELIABILITY
PREDICT

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.

▶ SIMULATION
DEPENDENCIES
auth: up · 200 ms
db: up · 400 ms
timeout: none
WORKER 1
idle
free
WORKER 2
idle
free
WORKER 3
idle
free
WORKER 4
idle
free
clockt = 0 ms
free workers4 / 4 (lowest so far 4)
dependency requests0 served · 0 timed out · 0 queued
/health0 served · 0 waiting for a worker
longest worker hold0 ms (deadline 1000 ms)
last event—
✓ healthy requests (no dependency) are still being served✓ no request holds a worker past its 1000 ms deadline
SCHEDULE
01[SVC]service: no timeout on dependency calls
02[REQ]request #1 arrives (auth → db)
03[CLOCK]clock: +200 ms
04[CLOCK]clock: +200 ms
05[CLOCK]clock: +200 ms
06[DB]db stops responding (every call hangs)
07[REQ]request #2 arrives (auth → db)
08[CLOCK]clock: +200 ms
09[CLOCK]clock: +200 ms
10[REQ]request #3 arrives (auth → db)
11[CLOCK]clock: +200 ms
12[CLOCK]clock: +200 ms
13[REQ]request #4 arrives (auth → db)
14[REQ]GET /health arrives (needs no dependency)
15[CLOCK]clock: +200 ms
16[CLOCK]clock: +200 ms
17[REQ]request #5 arrives (auth → db)
18[REQ]GET /health arrives (needs no dependency)
19[CLOCK]clock: +200 ms
20[CLOCK]clock: +200 ms
21[REQ]request #6 arrives (auth → db)
22[REQ]GET /health arrives (needs no dependency)
23[CLOCK]clock: +200 ms
24[CLOCK]clock: +200 ms
25[REQ]GET /health arrives (needs no dependency)
26[CLOCK]end of window: report
Start of no timeout scenario. 26 steps.
// 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);

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.

TUNE IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

every outbound call holds a finite resource (a thread, a connection, a slot) until it returns, and a call with no timeout can hold it forever. The timeout is not about the slow call; it is about giving the resource back so unrelated work can continue. Carry one deadline per request and hand each hop the remainder, so that the sum of waits can never exceed what the caller agreed to wait.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: RETRIES, BACKOFF & JITTER →