Skip to content

Lesson 7 of 9 · 28m

Timeouts, Retries, Jitter, and Circuit Breakers Without Footguns

Set dependency budgets deliberately and avoid the retry amplification and synchronization failures that turn small slowdowns into outages.

Most teams add retries because it feels resilient. Then a dependency slows down, every client retries at the same time, connection pools fill, queues back up, and the dependency dies under the additional load. The correct mental model is a budget: every dependency call gets bounded time, bounded attempts, bounded concurrency, and a deliberate failure behavior.

Timeouts are the foundation

A request without a timeout is an unbounded promise. Set connect, read, and end-to-end deadlines explicitly:

import httpx

client = httpx.Client(
    timeout=httpx.Timeout(1.0, connect=0.2, read=0.8),
    limits=httpx.Limits(max_connections=50, max_keepalive_connections=10),
)

The end-to-end budget matters because nested calls multiply. If checkout has 900ms total and payments gets 800ms, inventory cannot also receive 800ms unless those calls are concurrent. Draw the budget across the request path.

Retries need backoff, jitter, and idempotency

Retries are appropriate for transient failures, but only when the operation is safe to repeat or protected by an idempotency key:

delay = min(2 ** attempt * base_delay, max_delay)
delay = random.uniform(0, delay)  # full jitter

Without jitter, clients synchronize. You can see this in metrics as evenly spaced waves of traffic after a dependency hiccup. Cap total attempts and count time spent retrying against the caller's overall deadline.

Circuit breakers protect scarce capacity

A circuit breaker stops sending traffic to a dependency that is failing so both sides can recover:

closed   normal traffic
open     fail fast or use fallback after error threshold
half-open allow a small number of trial requests

The fallback decision is domain-specific. Reading a stale product recommendation may be acceptable. Reusing a stale payment authorization absolutely is not. Circuit breakers are not magic; they choose a controlled failure over an uncontrolled one.

Bulkheads prevent one dependency from consuming everything

Limit concurrency per dependency and per workload. If product recommendations become slow, they must not consume every worker thread needed by checkout. Separate pools, queues, workers, and sometimes process boundaries. Measure saturation by pool, not only globally.

Test the combinations

A timeout of two seconds, three retries, and 200 concurrent requests can produce far more load than intuition suggests. Chaos experiments should inject latency just above and just below timeout thresholds, verify jitter dispersion, inspect circuit state transitions, and confirm that fallback behavior is honest rather than silently wrong.

Field notes

  • Retrying non-idempotent writes can duplicate charges, orders, emails, and account actions.
  • 'Fail fast' still has a user experience cost; decide what the product says instead of pretending the feature works.
  • Circuit breakers keyed globally can punish all tenants because one tenant or region is unhealthy.
  • Client-side timeouts do not cancel server-side work automatically; cancellation propagation matters.

Was this lesson helpful?

Sign in to rate