Skip to content

Timeouts, retries, and circuit breakers

Introduction

Set a wait limit on every external call (timeout); retry failures in moderation (retry); and stop calling a dependency for a while when it keeps failing (circuit breaker).

Why use it

Networks and other people’s services will fail sometimes. Without these three controls, a small glitch becomes a major incident:

  • No timeout: requests hang forever and resources fill up.
  • Blind retries: the dependency is already struggling, and you hammer it harder until it collapses.
  • No circuit breaker: you keep waiting out the full timeout on every request even though you know it is down.

How to do it

1. Timeouts: every external call must have one, including HTTP, databases, caches, and messaging. Most libraries default to a very long timeout or none at all.

2. Retries: in moderation

  • Retry only errors that may self-heal: network drops, timeouts, 502/503, 429. Retrying 400, 401, or 404 is pointless.
  • Retry only idempotent operations, or you may charge twice; see Idempotency.
  • Exponential backoff: double the wait each time (1s, 2s, 4s, …) so the dependency can catch its breath.
  • Add jitter: a little randomness on the interval so thousands of clients do not retry in the same second.
  • Cap the attempts (e.g. at most 3) and set an overall time budget.

3. Circuit breaker: three states.

State Behavior
Closed (normal) Requests go out; failure rate is tracked
Open (tripped) Failure rate too high; reject for a while without sending
Half-open After a while, let a few probe requests through; succeed → recover, fail → stay open

4. Fallback: when the breaker is open, return a “good enough” substitute—e.g. cached data—instead of failing the whole page.

Component comparison

Microsoft.Extensions.Http.Resilience

Built into the frameworkOpsLow

Microsoft's official HttpClient resilience package, built on Polly; a single AddStandardResilienceHandler call adds timeouts, retries, and circuit breaking.

Best forNew .NET projects that call external HTTP services.

Avoid whenWhat needs protection is not an HTTP call, or the project is stuck on a very old .NET version.

Pros

Defaults include a total timeout, jittered retry, a circuit breaker, and a per-try timeout

With the HttpClient factory, almost no extra code is required

Microsoft maintains it, so a new project can prefer it

Cons

It only covers HttpClient, not databases or message queues

The default settings may not fit, so understand them before you tune

Non-idempotent POSTs are retried by default, which can charge the user twice

Official docs ↗

Polly

Self-hosted open sourceOpen sourceOpsLow

The long-standing resilience library for .NET; freely combine timeout, retry, circuit breaker, rate limiting, and other policies on any code, not just HTTP.

Best forC# projects that need to protect non-HTTP calls or need finely customized policies.

Avoid whenYou only want standard protection on a plain HttpClient; the official Resilience package is easier.

Pros

It can protect any code, not only outbound HTTP calls

Timeouts, retries, and circuit breakers can be combined as needed

There is plenty of material, and older projects often already use it

Cons

v7 and v8 are written differently, so online tutorials are easy to mix up

A bad setup, such as retries with no upper bound, can cause an incident

You attach it at every call site yourself, and it is easy to miss one

Official docs ↗

httpx timeout + tenacity retry

Self-hosted open sourceOpen sourceOpsLow

In Python, use httpx to set request timeouts, then use tenacity to make a function "retry by rule after failure".

Best forPython services calling external HTTP APIs that need simple timeouts and retries.

Avoid whenYou need mature circuit breaking and fallback, or the call chain is complex and requires a global unified policy.

Pros

Simple to write, one decorator line adds retries

Supports exponential backoff, random jitter, and attempt limits

tenacity works on any function, not just HTTP

Cons

No built-in circuit breaker, so you need another library (such as pybreaker) or write your own

You must specify which exceptions to retry; too broad a choice retries errors that should not be retried

You must confirm yourself that the retried operation is idempotent

Official docs ↗

Hand-written retry loop

Hand-rolledOpen sourceOpsLow

Write a for loop yourself: wait a bit after a failure and try again, up to a few times.

Best forOne-off scripts and special environments with no ready-made library.

Avoid whenProduction online services, which should use a mature library.

Pros

No library is added, and the retry logic is fully visible

A loop in a throwaway script is enough to get it running

The attempt count and the wait time are changed directly in the loop

Cons

It is easy to skip backoff and turn into a retry storm that overwhelms the other side

There is no circuit breaker, so every request still waits when the other side is down

Each place is written by hand, and inconsistent rules are hard to trace

tower (timeout / retry)

Open-source libraryOpen sourceOpsLow

Wrap outbound HTTP calls in Tower timeout and retry middleware, and retry failures according to a policy.

Best forRust backends that call multiple downstream services.

Avoid whenThere are only one or two stable downstreams and a simple timeout is enough.

Pros

Timeouts and retries are composed as middleware instead of scattered through business code

Shares the Tower ecosystem with Axum and reqwest

Each call can use its own policy

Cons

Circuit breakers and bulkheads need another component

Defaults may not suit your downstream

Enable retries only for idempotent operations

Official docs ↗

cockatiel

Open-source libraryOpen sourceOpsLow

A fault-tolerance library for TypeScript: retry, circuit breaker, timeout, and bulkhead, all composable.

Best forNode backends that call multiple downstream services.

Avoid whenThere are only one or two stable downstreams, where AbortSignal.timeout plus a simple retry is enough.

Pros

Retry, circuit breaker, timeout, and bulkhead are all present and composable

Type-friendly with a clear API

Not tied to any framework or HTTP library

Cons

You must wire the policies into your specific HTTP / database calls yourself

Default parameters may not suit your case

Enabling retries only for idempotent operations relies on discipline

Official docs ↗

context timeout + backoff retry

Open-source libraryOpen sourceOpsLow

Use context.WithTimeout to control the timeout of each call, and a backoff library for exponential backoff with jittered retries.

Best forTimeouts and retries for outbound calls in any Go backend.

Avoid whenRetry logic is already handled uniformly by a service mesh or SDK.

Pros

Timeouts and cancellation are native to Go and propagate through the whole call chain

The backoff library is small and focused, with configurable max attempts and jitter

No framework coupling

Cons

Circuit breakers and bulkheads need a separate library (such as sony/gobreaker)

Avoid retrying non-idempotent operations

Retries must honor cancellation of the outer context

Checklist

  • Every external call has an explicit timeout
  • Retries cover only idempotent operations and self-healing errors
  • Retries use exponential backoff + jitter with a capped attempt count
  • Critical dependencies have a circuit breaker
  • There is a fallback when the breaker trips
  • Do not retry at every layer of the call chain
  • Work that cannot finish in one request goes through a queue; see Message queue