HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1362 min read

Deadline Propagation (Timeout Budgets)

Timeouts Don't Compose, Deadlines Do

System designsystem design / distributed systems

Two things that look identical on a config page

TIMEOUT

  • "wait 3s for THIS call"
  • Relative, per hop
  • Resets to full at every hop
  • Each service decides alone

DEADLINE

  • "all work for this request stops at T"
  • One budget for the whole chain
  • Shrinks as it travels
  • The caller decides, everyone obeys

The arithmetic that bites

Chain: gateway → orders → inventory → pricing. Every team set a sensible "timeout: 3s, retries: 2".

Worst case from the gateway's view:

pricing 3s × 3 = 9s

inventory 9s × 3 = 27s

orders 27s × 3 = 81s

The gateway gave up at 3 seconds. The other 78 are orphan work: DB connections, goroutines and CPU burning for a client that already got a 504 and probably already retried.

That's how one slow dependency becomes a full outage — not through errors, but through work nobody is waiting for. Your queues fill with requests whose answer can no longer be delivered.

With a deadline, pricing receives "180ms left", compares it to its own p99 of 400ms, and fails instantly instead of trying. Load sheds itself.

How it looks in code

gRPC does it for free — a deadline becomes the grpc-timeout header and every hop re-derives it.

Go:

ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
defer cancel()
resp, err := inventory.Get(ctx, id)   // ctx goes everywhere

Plain HTTP has no standard header, so propagate your own:

X-Deadline-Ms-Remaining: 180

and in middleware: ctx, _ = context.WithTimeout(ctx, remaining - 20ms) — subtract a small buffer per hop for network + serialization.

Five things practitioners get wrong

1️⃣ Deadline without cancellation. ctx expires, your handler returns 504 — and the Postgres query keeps running for 40s. Pass ctx into the driver (lib/pq sends a real cancel request), and set statement_timeout as a backstop. Returning early ≠ stopping work.

2️⃣ Retries get a fresh budget. A retry inside a deadline must be checked against what's left: retry only if remaining > p99 of that call + buffer. Otherwise you spend the whole budget on attempt 1 and 2.

3️⃣ Sending an absolute timestamp. Two hosts with 200ms clock skew = a request that's "already expired" on arrival, or one that outlives its budget. gRPC deliberately sends a duration, not a wall-clock time. Copy that.

4️⃣ Background work inheriting the request ctx. You return 200, then your fire-and-forget audit/Kafka publish dies because the HTTP handler's ctx was cancelled on response. Detach it: context.WithoutCancel(ctx) (Go 1.21+), with its own fresh timeout.

5️⃣ No check on arrival. The cheapest win available: if remaining <= 0 at the top of your handler, return immediately. One if that saves your database during a traffic spike.

The mental shift

A timeout protects the caller. A deadline protects the system. Same number, different blast radius.

Your exercise

Take your busiest endpoint. Write down the call chain, and at every level multiply timeout × (1 + retries). If the product is bigger than your client's timeout — and it almost always is — you have orphan work in production right now.

Then add one log line per hop: remaining_ms on entry. Whoever eats the biggest slice of the budget is your real p99 problem, and it's rarely the service you'd guess.