Circuit Breaker Pattern
A three-state switch in front of a failing dependency: build it, trace it, count failures over a window, and know how it differs from a retry.
Overview
A circuit breaker sits between your code and a dependency it calls: another service, a database, a payment provider. While the dependency is healthy, calls pass through. When enough of them fail, the breaker opens and refuses new calls at once, without trying the dependency. After a pause it lets a few trial calls through; if they succeed it closes again. The name is borrowed from the electrical part that cuts the current before the wiring burns. Martin Fowler credits Michael Nygard's book *Release It!* with making the pattern well known.
In the interview you are expected to write the state machine, choose how failures are counted, and explain what the breaker protects against that a timeout or a retry does not. This topic does each of those with a breaker you can trace by hand.
The problem: slow failure
A dependency that is down and answers with an error is cheap. A dependency that is down and answers with nothing is expensive: each call holds a thread, a socket and memory until the timeout fires. With enough traffic, the calling service runs out of threads and fails too, even though nothing is wrong with it. The breaker turns a slow failure into a fast one.
Picture service A with 200 worker threads. It calls dependency B, which is hanging, with a 30-second timeout. It also reads its own database, which is healthy. The failure spreads in three steps.
- Users keep sending requests to A, and each one that needs B holds a worker thread while B does not answer.
- The threads wait out the 30-second timeout, and the pool empties.
- Now every request to A fails, including requests that only needed A's healthy database.
Now compare the two cases with numbers. Without a breaker, one call to a dead dependency holds one thread until the timeout, so 200 concurrent calls at a 30-second timeout hold 200 threads for 30 seconds each. Requests that do not need the dependency queue behind the stuck ones, and the dependency gets full traffic plus retries while it tries to recover. With the breaker open, one call costs a state check and then an immediate error. It holds no threads, requests that do not need the dependency are unaffected, and the recovering dependency sees only a few trial calls.
The breaker does not replace the timeout. The timeout is what turns a hang into a failure the breaker can count.
Interview questions
- Implement a circuit breaker with the three-state state machine.
- What is the difference between a circuit breaker and a retry with exponential backoff?
- How do you count failures over a sliding window instead of consecutively?
- How does the breaker behave under concurrency, especially in the half-open state?
- How do you tune the thresholds and monitor a breaker in production?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio