# Circuit Breakers in Production: Timeouts, Retry Storms, and Failure Recovery

> How circuit breakers limit calls to a failing API, how half-open probes work, and how timeouts and retries can prevent a retry storm.

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2025-11-15
- Updated: 2026-11-16
- Source: https://www.internalswithzee.com/blog/circuit-breakers

## The short version

- A breaker fails fast once a dependency looks unhealthy, so callers stop waiting on calls that are likely to fail.
- In half-open, limit probes to avoid hammering a recovering dependency. A per-process breaker permits one probe per worker, and an expired lease can permit another.
- A breaker only counts failures that callers give up on, so it needs a timeout on every call underneath it.
- Retries multiply load during an outage. Cap attempts, use backoff with jitter, add a retry budget, and never retry an open circuit.

A dependency doesn't have to go completely offline to cause trouble. Sometimes it just gets slow enough that requests pile up, workers stay busy, and retries add even more traffic to a service already struggling. A circuit breaker gives that dependency breathing room, provided the timeouts and retries around it are configured with the same care.

## The Problem

In a pipeline that calls several LLM providers, each provider has its own failure modes, rate limits, and latency. If one slows down, calls waiting on it can hold resources that the rest of the pipeline needs.

Timeouts and retries help, but they can also create problems when their limits don't fit the failure:

- Retries during an outage amplify the problem (retry storms)
- Long timeouts block resources and cascade failures
- Users wait for failures we already know will happen

## What is a Circuit Breaker?

The circuit breaker pattern, popularized by Michael Nygard in "Release It!", works like an electrical circuit breaker. When failures reach a threshold, the circuit "opens" and immediately fails requests without attempting them.

Three states:

1. **Closed** - Normal operation. Requests pass through.
2. **Open** - Failure threshold exceeded. Requests fail immediately.
3. **Half-Open** - Testing if the service recovered. Let a single probe request through.

The half-open state deserves care. If the dependency is just coming back, sending it the full load again can overwhelm it. A limited probe gives you a chance to check whether it has recovered.

![In the closed state requests pass. When failures reach the threshold, the circuit opens and fails fast. After a timeout it becomes half open and permits limited probes. A failed probe reopens it; successful probes can close it.](https://www.internalswithzee.com/images/diagrams/breaker-relationships-wide.svg)

## Implementation

Here's a simplified circuit breaker. It is **per-process**: the lock protects threads in one process, and nothing here coordinates between processes (more on that below).

```python
import time
from datetime import timedelta
from enum import Enum
from threading import Lock

class CircuitState(Enum):
    CLOSED = "closed"
    OPEN = "open"
    HALF_OPEN = "half_open"

class CircuitBreaker:
    def __init__(
        self,
        failure_threshold: int = 5,
        recovery_timeout: timedelta = timedelta(seconds=30),
        probe_lease: timedelta = timedelta(seconds=10),
    ):
        self.failure_threshold = failure_threshold
        self.recovery_timeout = recovery_timeout
        self.probe_lease = probe_lease

        self.state = CircuitState.CLOSED
        self.failure_count = 0
        self.opened_at = 0.0
        self.probe_deadline = 0.0   # when the current probe's claim expires
        self.lock = Lock()

    def can_execute(self) -> bool:
        with self.lock:
            now = time.monotonic()

            if self.state == CircuitState.CLOSED:
                return True

            if self.state == CircuitState.OPEN:
                if now - self.opened_at < self.recovery_timeout.total_seconds():
                    return False
                # Timeout over: this caller claims the probe.
                self.state = CircuitState.HALF_OPEN
                self.probe_deadline = now + self.probe_lease.total_seconds()
                return True

            # Half-open: one probe is already out. Everyone else fails fast,
            # unless the probe's lease expired (the prober never reported back).
            if now >= self.probe_deadline:
                self.probe_deadline = now + self.probe_lease.total_seconds()
                return True
            return False

    def record_success(self):
        with self.lock:
            if self.state == CircuitState.HALF_OPEN:
                self.state = CircuitState.CLOSED
            self.failure_count = 0

    def record_failure(self):
        with self.lock:
            if self.state == CircuitState.OPEN:
                return   # a late result from a call that started before we opened

            if self.state == CircuitState.HALF_OPEN:
                self._open()   # the probe failed: back to open, restart the timer
                return

            self.failure_count += 1
            if self.failure_count >= self.failure_threshold:
                self._open()

    def _open(self):
        self.state = CircuitState.OPEN
        self.opened_at = time.monotonic()
```

Two things changed from the naive version, and both matter:

- **One caller claims the probe.** The caller that moves the circuit from open to half-open claims the probe; other callers are refused until it reports or its lease expires. Letting several callers through immediately can send too much traffic to a service that may still be fragile. (An earlier version of this example allowed the call that flipped the state plus up to three more.)
- **A probe has a lease.** If the probing caller crashes or forgets to call `record_success` or `record_failure`, the circuit would otherwise sit in half-open forever. The lease lets another caller try after a bounded time.

The lease limits claims while it is valid; it does not guarantee that only one probe is still running after it expires. This simplified example also doesn't identify which probe reported a result, so a late result can affect a newer state. In production, bound probe execution with a timeout and associate results with a probe generation or token so stale results can be rejected.

### Concurrency across processes

The lock above is a thread lock. If you run twenty workers, you have twenty independent breakers, and each one can send its own probe: up to twenty probes, not one. For many services that is acceptable, and per-process breakers are common because they are simple and add no dependency. But say it plainly: this example gives you one probe per process.

If you need one probe for the whole fleet, the claim must be atomic in shared state. A short-lived lease in Redis is one way:

```python
# Returns True for exactly one caller until the lease expires.
claimed = redis.set(f"breaker:{service}:probe", worker_id, nx=True, ex=10)
```

`SET ... NX EX` allows one caller to hold that key until it expires. The expiry is the lease: if the prober dies, the key vanishes and someone else can claim it. As with the local example, a slow probe can outlive its lease; the claim alone does not prevent overlapping calls or stale results. The costs are real, though. Every call now depends on Redis being reachable, so decide what the breaker does when Redis is down (usually: fall back to the local breaker). A database row with `UPDATE ... WHERE probe_claimed_until < now()` works the same way.

## Timeouts and Retries

A breaker can only count failures that the caller actually gives up on. If a call hangs for two minutes, it is neither a success nor a failure for that whole time, and the threshold is never reached while workers pile up behind it. So:

- **Put a timeout on every call under the breaker**, and make it shorter than whatever deadline the user or upstream caller is waiting on. Count a timeout as a failure.
- **Decide where retries sit.** If retries wrap the breaker, each attempt is counted and an open circuit stops further attempts. If retries are inside the breaker call, the breaker sees one slow failure instead of several quick ones, and an attempt can carry on while the breaker is trying to open. Retry outside, and don't retry when the circuit is open.
- **Remember that layers multiply.** If a service, its client library and the HTTP client each allow three total attempts, one request can become 27 calls to a dependency that is already struggling. If each instead allows three retries *in addition* to the first attempt, the number can reach 64.

Retries are what turn a partial outage into a retry storm. Three habits keep them safe:

1. **Cap attempts** at a small number.
2. **Back off with jitter.** Wait exponentially longer between attempts, and randomize the wait, so a thousand clients don't all come back on the same second.
3. **Use a retry budget.** Allow retries only up to a fraction of normal traffic (say, 10 percent, as an illustrative number) so that during an outage the extra load stays bounded instead of doubling or tripling.

Retrying also requires the operation to be safe to repeat. For a write, an idempotency key can help if the receiving service supports and enforces it: the same logical operation must use the same key after an ambiguous timeout.

## Key Decisions

**Failure threshold**: The example counts consecutive failures, and a success resets the count. A rolling window of recent calls is better when traffic is mixed, because it can tell "5 failures in a row" from "5 failures out of 10,000". Too low a threshold and you'll open on transient errors. Too high and you'll let too many requests fail first.

**Recovery timeout**: 30 seconds is a reasonable starting point for many APIs, not a rule. For rate-limited services, we use longer timeouts based on the rate limit reset time.

**What counts as failure**: Not all errors should trip the breaker. The sample leaves classification to its caller; a typical integration might count:
- Timeouts, including connection timeouts
- Connection failures
- 5xx errors

Client errors (4xx) are the caller's problem, not a service failure. A rate-limit response (429) is a signal to slow down, so handle it with backoff and the provider's retry-after hint instead of counting it toward opening the circuit.

## Fallback Strategies

Failing fast can be useful on its own: it frees resources and gives the caller a clear failure. When the product can tolerate an alternative, we also use fallbacks such as:

1. **Provider failover**: If OpenAI's circuit opens, route to Anthropic
2. **Cached responses**: Return stale data if appropriate
3. **Graceful degradation**: Offer reduced functionality
4. **Queue for retry**: Persist the request and process later

## Monitoring

Circuit state changes are important signals. We track:

- Circuit open events (alerts)
- Time spent in each state
- Fallback success rates
- False positive rate (opened when service was healthy)
- Retry rate, as a share of total calls. A rising retry rate often shows up before the breaker opens.

## Lessons Learned

**Start conservative**: It's easier to relax thresholds than to explain cascading failures.

**Test the open state**: Your fallback logic needs as much testing as the happy path. Test half-open with concurrent callers, lease expiry, and late results too. Verify the guarantee your implementation actually provides.

**Different breakers for different operations**: Read and write operations to the same service might need different thresholds.

**Document the behavior**: Teams need to know what happens when a circuit opens.

Circuit breakers are one piece of a resilience strategy. Combined with timeouts, bulkheads, and proper retry policies, they help systems degrade gracefully instead of failing all at once.

## Sources and further reading

The primary material behind the pattern, and what to read once the basics land.

- [Circuit breaker](https://martinfowler.com/bliki/CircuitBreaker.html) by Martin Fowler. The canonical description of closed, open and half-open, and what each state is for. The idea comes from Michael Nygard's *Release It!*, which is worth reading whole.
- [Addressing cascading failures](https://sre.google/sre-book/addressing-cascading-failures/) from the Google SRE book. What actually happens when a dependency degrades and every caller retries into it. This is the failure a breaker exists to stop.
- [Handling overload](https://sre.google/sre-book/handling-overload/), same book, for load shedding and graceful degradation, which are the tools a breaker hands off to once it opens.
- [Timeouts, retries and backoff with jitter](https://aws.amazon.com/builders-library/timeouts-retries-and-backoff-with-jitter/) in the Amazon Builders' Library. Timeouts bound how long a stalled call can hold resources and when the breaker can count it as a failure.
- [Exponential backoff and jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/) from the AWS Architecture Blog, for what retries should look like when many clients recover at once.

## Continue learning

For an example of a breaker working alongside retries and durable jobs, see [When RabbitMQ Isn't Enough](/blog/resilient-jobs-unreliable-apis). Then explore bulkheads, which stop one failing dependency from consuming resources everything else needs, and load shedding, which helps when your own service is overloaded.

## FAQ

**Where should breaker state live?**
Per-process is the simplest and is often enough: no extra dependency, and each worker protects itself. The cost is that every worker discovers the outage on its own, and sends its own half-open probe. Shared state in Redis or a database can coordinate the fleet, but adds a dependency you must handle when it fails. Make the probe claim atomic and account for expiry and stale results; a lease on its own does not guarantee that only one request is still running.

**What failure rate should open the breaker?**
Start from what a healthy dependency looks like, not from a round number. If it normally errors on 1% of calls, a sustained increase may signal trouble, but the right threshold depends on traffic volume, the window, and the cost of false positives. Test it against your service's actual behavior.

**Should a breaker cover all errors?**
No. Count the ones that mean the dependency is unhealthy: timeouts, connection failures, 5xx. A 400 means your request was wrong and will fail identically on every retry, and a 429 is a rate limit that backoff handles better.

**Should I retry inside the breaker?**
Prefer retrying outside it, with a capped number of attempts, jittered backoff and a retry budget, and stop retrying as soon as the circuit is open.
