TExcelTAD

Timeouts & Deadlines

Every call across a network can hang: a server stuck in garbage collection, a packet lost without a reset, a dependency at 100% CPU. Without a timeout, the caller waits forever, holding a thread, a connection and memory, and the hang spreads upwards until the whole service stops. A timeout is the most basic piece of failure behaviour, and most outages that "cascade" have a missing or wrong one somewhere.

The failure-behaviour lessons in this section are simulations: deterministic Python models of a service under stress, so you can change a number and see the effect. Idempotency and Retries covers the design side (which operations are safe to retry).

Choosing a timeout from data

A timeout should be long enough that healthy requests almost never hit it and short enough that a hung call frees its resources quickly. Start from the latency distribution of the dependency when it's healthy. The model draws 200,000 seeded latencies with a realistic long tail and shows what share of healthy requests each timeout would kill:

python · try it yourself
import random

rng = random.Random(1)
lat = sorted(rng.lognormvariate(-2.5, 0.7) * (8 if rng.random() < 0.003 else 1) for _ in range(200_000))

def pct(p):
    return lat[int(p * len(lat)) - 1]

print(f"healthy latency: p50 {pct(0.5) * 1000:.0f} ms, p99 {pct(0.99) * 1000:.0f} ms, "
      f"p99.9 {pct(0.999) * 1000:.0f} ms, max {lat[-1] * 1000:.0f} ms")
for timeout_ms in (100, 250, 500, 1000, 2000, 30000):
    killed = sum(1 for x in lat if x * 1000 > timeout_ms) / len(lat)
    print(f"timeout {timeout_ms:>6} ms: kills {killed:.3%} of healthy requests")
healthy latency: p50 82 ms, p99 447 ms, p99.9 989 ms, max 7462 ms
timeout    100 ms: kills 39.114% of healthy requests
timeout    250 ms: kills 5.840% of healthy requests
timeout    500 ms: kills 0.690% of healthy requests
timeout   1000 ms: kills 0.100% of healthy requests
timeout   2000 ms: kills 0.020% of healthy requests
timeout  30000 ms: kills 0.000% of healthy requests

A common rule is "a bit above the healthy p99.9", then check that the total time still fits the caller's budget. A 30-second default (common in HTTP clients and some load balancers) kills nothing, but during an incident each hung call holds its resources for 30 seconds.

Timeouts must shrink down the call chain

A request crosses several hops: browser → gateway → checkout → payments → database. If each hop sets its own, independent timeout, a deep hop can keep working long after the user's request at the top has given up: wasted work on a system that is already in trouble.

Deadline propagation fixes this: the edge sets an absolute deadline ("this request is worthless after 10:00:03.200"), passes it down (gRPC does this natively with deadlines; HTTP services use a header), and each hop uses min(own timeout, remaining time), giving up immediately when no time is left.

The model sends 1,000 requests through three hops while the database is slow (each query takes 1.8 s). The user's request times out after 1 s:

python · try it yourself
HOPS = [("gateway", 1.0), ("checkout", 2.0), ("payments", 3.0)]   # (name, its own timeout in s)
DB_LATENCY = 1.8
HOP_OVERHEAD = 0.05
REQUESTS = 1000

def simulate(propagate):
    busy = {name: 0.0 for name, _ in HOPS}                # seconds of work spent per hop
    busy["database"] = 0.0
    for _ in range(REQUESTS):
        remaining = HOPS[0][1]                           # the edge deadline: 1 s from now
        for name, own in HOPS:
            budget = min(own, remaining) if propagate else own
            if budget <= HOP_OVERHEAD:
                break                                    # no time left: don't even call the next hop
            remaining = budget - HOP_OVERHEAD
            busy[name] += min(budget, DB_LATENCY + HOP_OVERHEAD)
        else:
            busy["database"] += min(remaining, DB_LATENCY)   # the DB query is cancelled when the caller gives up
    return busy

for propagate in (False, True):
    busy = simulate(propagate)
    total = sum(busy.values())
    parts = "  ".join(f"{k} {v:6.0f}s" for k, v in busy.items())
    print(f"{'with' if propagate else 'without'} deadline propagation: {parts}  total {total:5.0f}s")
print(f"useful work: 0s (every user request timed out after 1 s)")
without deadline propagation: gateway   1000s  checkout   1850s  payments   1850s  database   1800s  total  6500s
with deadline propagation: gateway   1000s  checkout    950s  payments    900s  database    850s  total  3700s
useful work: 0s (every user request timed out after 1 s)

Without propagation, the inner hops keep waiting for the database up to their own, longer timeouts, so each failed user request costs several seconds of work across the chain. With the deadline passed down, every hop stops when the user's second is over, and the database queries are cancelled too. During an overload this difference decides whether the system recovers or keeps drowning in work nobody will use.

Rules of thumb

Rule Why
every network call has an explicit timeout library defaults are often infinite or 30 s+
inner timeouts are shorter than outer ones, or derived from a propagated deadline otherwise inner work outlives its purpose
separate connect and request timeouts connecting to a dead host should fail in about a second
a timeout plus retries must fit the caller's budget 3 tries × 1 s timeout can't fit in a 2 s budget
measure timeouts as errors in the SLI a request that timed out failed, even if no 5xx was logged
cancel work when the caller goes away the server should notice a closed connection or an expired deadline

Test yourself

Why should inner services use shorter timeouts than outer ones (or a propagated deadline)?

  1. Inner services are faster
  2. Otherwise inner hops keep working after the outer caller has given up, wasting capacity during an incident
  3. To reduce logging
  4. HTTP requires it

A client has a 2-second budget and retries up to 3 times. What per-try timeout can work?

  1. 2 s
  2. About 0.6 s
  3. 5 s
  4. No timeout

What is a reasonable starting point for a timeout?

  1. Always 30 s
  2. A little above the healthy p99.9 latency, checked against the caller's budget
  3. The average latency
  4. 1 ms