Timeouts, Retries and Circuit Breakers

Keep a failing dependency from taking your whole service down with timeouts, safe retries, circuit breakers and bulkheads.

Advanced · about 4 hours · difficulty 4/5

Learn first: Vertical and Horizontal Scaling, Microservices and Service-Oriented Architecture

Every network call can be slow or fail. In a system with many dependencies, something is always degraded. Resilience patterns stop one bad dependency from cascading into a full outage.

Timeouts

The most important and most often missing pattern. Without a timeout, a slow dependency ties up your threads or connections until you have none left. Set explicit connect and request timeouts on every HTTP client, database query and queue call, and keep them shorter than the caller's own timeout.

Retries, carefully

  • Retry only transient failures (timeouts, 503, connection resets), never 400-level validation errors.
  • Use exponential backoff with jitter so thousands of clients do not retry in lockstep.
  • Cap attempts, and only retry idempotent operations or ones protected by idempotency keys.
  • Beware retry amplification: three layers each retrying three times turns one failure into 27 calls.

Circuit breakers

A circuit breaker watches failures to a dependency. After too many, it opens and fails fast for a while instead of waiting on timeouts, then goes half-open to test with a few requests before closing again. This gives the dependency room to recover and keeps your latency predictable. Libraries: opossum (Node), resilience4j (Java), Polly (.NET), or a service mesh.

Bulkheads and fallbacks

  • Bulkheads isolate resources (separate pools or concurrency limits per dependency) so a slow recommendation service cannot exhaust the pool that checkout needs.
  • Fallbacks return something useful when a call fails: cached data, a default value, or a reduced feature.

Test it

Inject failures on purpose (toxiproxy, chaos experiments) to verify behavior before a real outage does.

You are ready to move on when a dependency in your system can be made slow or dead and your service still responds quickly with a fallback, and you can show the breaker opening in your metrics.

Key concepts

  • Timeouts everywhere
  • Retries with backoff and jitter
  • Retry amplification
  • Circuit breakers
  • Bulkheads
  • Fallbacks
  • Fault injection and chaos testing

Mini project

Put toxiproxy between your API and a dependency, add timeouts, jittered retries and a circuit breaker with a cached fallback, and chart latency with the dependency healthy, slow and down.

Check yourself

Why can adding retries at every layer of a call chain make an outage worse?

Free resources