Skip to content

Circuit breaker

A circuit breaker trips open after sustained failures to a dependency, failing fast instead of draining caller resources on repeated doomed calls.

Definition

What it is

A circuit breaker is a state machine that sits between a caller and a dependency. It counts successes and failures. When failures exceed a threshold, it "opens": further calls return an error immediately, without ever reaching the dependency. After a cooldown period, it enters a "half-open" state and lets a few test calls through. If those succeed, it closes and resumes normal operation; if they fail, it opens again.

The point is not to fix the dependency. The point is to stop wasting the caller's resources (threads, connections, latency budget) on calls that are almost certain to fail.

Why it matters

In a distributed system, every call is a potential liability. A database that has hit max connections will not answer faster under more load; a third-party API under rate limit will just keep returning 429. Without a breaker, the caller keeps trying, exhausts its own thread pool or connection pool, and eventually fails for a reason that has nothing to do with the actual bug.

A circuit breaker turns that cascade into a bounded failure. The caller responds quickly (even if with a degraded result), keeps serving healthy traffic, and recovers as soon as the dependency recovers.

The three states

State

Behaviour

Exit condition

Closed

Calls pass, counters accumulate

Open on failures > threshold

Open

Calls rejected immediately (fast-fail)

Closed→Half-open after cooldown

Half-open

Limited test calls allowed

Closed if tests succeed, Open if they fail

Resilience patterns side by side

Pattern

What it does

Trigger

Scope

Retry

Re-attempts the same call

Transient failure

Operation

Circuit breaker

Rejects calls to protect caller

Failure threshold

Dependency

Failover

Switches to standby replica

Primary failure

Infrastructure

Failback

Returns to primary after recovery

Primary repaired

Infrastructure

The state machine

stateDiagram-v2
    [*] --> Closed
    Closed --> Closed: call succeeds
    Closed --> Open: failures > threshold
    Open --> Open: calls rejected (fast fail)
    Open --> HalfOpen: cooldown expires
    HalfOpen --> Closed: test calls succeed
    HalfOpen --> Open: test call fails

How to configure one well

A circuit breaker that helps, not hurts

  1. Threshold based on error rate, not absolute count

    50% errors over 20 calls is a better signal than "5 errors in a row".

  2. Cooldown long enough to matter

    30-60 seconds. Too short and the breaker flips repeatedly; too long and it delays recovery.

  3. Always have a fallback

    A cached value, a default response, a queued retry. Failing fast is fine; failing without a message is not.

  4. Log every state change

    Closed→Open is an incident signal. HalfOpen→Closed is a recovery signal. Both belong in your dashboards.

  5. Per-dependency breakers, not global

    A breaker around the payment gateway and a breaker around the recommendation service are independent.

Common failures

Origin

Wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all.

— Martin Fowler, *CircuitBreaker*, 2014
2007 · Nygard populariza el patrón en *Release It!*
2014 · Fowler escribe el bliki canónico
2015 · Netflix Hystrix entra en producción masiva
2018 · Hystrix entra en modo mantenimiento
2019 · Resilience4j lo reemplaza como estándar Java

Evidence

FAQ

Circuit breaker vs retry?

A retry re-attempts the call; a circuit breaker stops it. Use retries for transient failures inside the closed state, and a breaker to stop retries once failures become sustained.

How many errors to open?

Depends on traffic. A common default is 50% error rate over a rolling 20-50 call window. Tune per dependency.

What happens when the breaker is open?

Calls return immediately with an error or a fallback. No request reaches the dependency, so it has room to recover.

Circuit breaker vs failover?

A breaker protects the caller from a dependency; failover moves the workload to a standby replica. They operate at different layers.

Connections

Circuit breareliabilitresilienceRetryFailoverBackpressurate-limitGraceful d

● broader ● narrower ● related

Broader

  • reliability
  • resilience-patterns

Related

Used in the garden

Sources

Etymology

Origin: metaphor

  • circuit (noun): from Latin circuitus, "a going around"
  • breaker (noun): electrical device that interrupts current flow on overload

Semantic network

Circuit breareliabilitresilienceRetryFailoverBackpressurate-limitGraceful d

● broader ● narrower ● related

Broader

  • reliability
  • resilience-patterns

Related

Used in the garden

Sources