Designing a Circuit Breaker

Difficulty: Intermediate Patterns: State, Proxy, Strategy Asked at: Amazon, Netflix, Stripe, Uber


A dependency gets sick. Not down, because down is the easy case and returns instantly, but sick: it answers one request in three and takes the full timeout to refuse the rest. Your service keeps calling it, because nothing told it to stop, and every caller now blocks for that timeout holding a thread. Within a minute the pool is full of threads waiting on a service that is not going to answer, and your service is down too, for a reason that has nothing to do with your code. A circuit breaker is the object that notices and stops calling.

The three states are not the hard part, and candidates recite them fluently: CLOSED passes calls through, OPEN refuses them without touching the dependency, HALF_OPEN lets a trial through to see whether the patient is breathing. Proxy Pattern already builds that machine over a remote pricing service, with an injected clock and a single-permit gate, and this page agrees with it rather than re-deriving it. The page is about the three decisions underneath those states, and all three have an obvious answer that is wrong.

When do you trip? Nearly everyone says “after five consecutive failures”, and that rule is broken in a way that matters: one lucky success resets the counter to zero. A dependency failing forty percent of requests almost never produces five in a row, so the breaker never trips while every caller keeps paying the timeout on the four calls in ten that fail. The condition that works is a failure rate over a window with a minimum-calls floor – and then you have to implement the window, which is where the data-structure question lives.

Who gets the trial call, and what comes back instead? When the open window expires, every thread in flight reads HALF_OPEN and each concludes it should be the probe, so five hundred threads fire five hundred requests at the service you just decided was sick – the stampede the breaker exists to prevent. Admission has to be a single permit, not a state check. And a refused call still has to hand the caller something, which is where most implementations quietly hand back a lie: a stale cached value is legitimate only if the caller can see how old it is, and a hard-coded default is a business decision wearing an infrastructure costume.

💡 Circuit breaker = an object that watches calls to one dependency and, once the failure rate crosses a threshold, stops forwarding them for a while, so the sick service gets room to recover and your callers stop paying its timeout. Learn more →

💡 Sliding window = the recent slice of history a decision is made over, either the last N calls or the last N seconds. Everything outside it has no vote, which is what makes the breaker respond to how the dependency is behaving now rather than this morning.

💡 Compare-and-set, usually shortened to CAS = one indivisible operation that says “if this value is still exactly what I last read, replace it”. It is how one thread out of twenty claims the trial without a lock. Learn more →

💡 Fallback = what a refused call returns. Fail fast, serve something stale, serve something smaller, or accept the work for later – and say which one happened. Learn more →


Functional Requirements

  1. Three states – CLOSED forwards calls, OPEN refuses them without touching the dependency, HALF_OPEN admits a bounded number of trials.
  2. Rate-based tripping – trip when the failure rate over the window crosses a threshold and the window holds at least a configured minimum number of calls.
  3. Both window kinds – a count-based window over the last N calls and a time-based window over the last N seconds, behind one interface, chosen per dependency.
  4. Slow calls count as failures – a call that returns successfully but takes longer than a slow-call threshold records as a failure, because a dependency answering in thirty seconds is an outage that never throws.
  5. Bounded recovery – when the open window expires, at most a configured number of trial calls reach the dependency, and the breaker closes only after a required number succeed.
  6. Pluggable fallback – a refused call resolves through a fallback strategy: fail fast with a typed exception, serve a stale value carrying its timestamp, serve a reduced feature, or queue the work.
  7. One breaker per dependency, observably – a registry hands out a named breaker per downstream service, and every state change publishes an event with its reason alongside lifetime counters for permitted, refused, failed and slow calls.

Non-Functional Requirements

  1. O(1) per call – recording an outcome and reading the failure rate are constant time, maintained incrementally as running totals in a ring buffer, never by scanning a list of timestamps. The exception is a time-based window whose clock has skipped ahead, costing one bounded pass over its buckets: O(buckets), independent of traffic.
  2. Thread-safe, with no lock held across the dependency call – admission is a compare-and-set and the network call happens outside every critical section. A breaker that serialises its callers behind one round trip has become the outage it was installed to prevent.
  3. At most N trials on recovery – when the open window expires, no more than trialPermits calls reach the dependency however many threads arrive in the same microsecond.
  4. Isolated per dependency – one breaker’s verdict says nothing about another’s, and the registry lookup is a single hash lookup on the dependency name.