Design a Circuit Breaker
🔌 What is a circuit breaker? It is a small guard in front of a call to another service. While the service works, calls go straight through. When the service keeps failing, the breaker "trips" and rejects calls immediately for a while, instead of letting every caller wait for a timeout. After a pause it lets one trial call through, and if that works, traffic flows again.
The name comes from the breaker in a fuse box: when something goes wrong, it cuts the circuit so the damage does not spread. In software the damage is a cascading failure. A slow payment provider makes our request threads wait 10 seconds each, the thread pool fills up, and now our whole service is down too, including the pages that never needed payments. A breaker turns that slow failure into a fast, cheap one. It is a short problem with a clear state machine, a few precise rules and one real concurrency trap, which makes it a popular medium-level interview question.
Requirements
"Our service calls a payment provider that sometimes goes down. When it does, our threads pile up waiting on timeouts. Design a circuit breaker to protect us."
The breaker's behavior is a handful of rules, and every one of them is a number or a condition you have to ask for.
Clarifying Questions
You: When should the breaker trip?
Interviewer: After a number of failures in a row. Make the number configurable.
You: What happens to a call while the breaker is tripped?
Interviewer: It fails immediately, without touching the provider. The caller gets an error it can recognize and decides what to do, for example show "try again later".
You: How does it recover?
Interviewer: After a cool-down period, let a trial call through. If enough trial calls succeed in a row, go back to normal. If a trial fails, trip again and wait another full cool-down.
You: During that trial phase, how many calls may go through at once?
Interviewer: One at a time. A service that just came back should not get our whole backlog at once.
You: What counts as a failure? Any exception?
Interviewer: Any exception from the call, for now.
You: Should the breaker read the clock?
Interviewer: Pass the time in. We want to test it without waiting.
You: Is one breaker shared by many threads?
Interviewer: Yes. All request threads that call the provider share its breaker.
The fourth question is the one candidates forget. Without it, the moment the cool-down ends every waiting thread becomes a "trial", and the recovering provider gets hit by a stampede that knocks it over again.
💡 Interview tip: Draw the three states and the arrows between them before writing any class. A circuit breaker is a state machine with counters, and once the interviewer agrees on the picture, the code is almost a transcription of it.
Final Requirements
- The breaker has three states: CLOSED (normal), OPEN (tripped) and HALF_OPEN (testing recovery). It starts CLOSED.
- CLOSED: every call goes through. A success resets the failure count. After
failure_thresholdfailures in a row, the breaker becomes OPEN. - OPEN: every call is rejected immediately, without calling the downstream. After
open_timeouttime units, the breaker becomes HALF_OPEN. - HALF_OPEN: one trial call at a time goes through; other calls are rejected. A failure makes the breaker OPEN again for a full
open_timeout. Aftersuccess_thresholdsuccesses in a row, it becomes CLOSED. - A rejected call is not counted as a failure or a success.
- Callers can read the current state (for dashboards and tests).
- Time is passed in with every call.
- One breaker is shared by many threads, and the downstream call must not run while holding a lock.
Out of scope
- Retrying failed calls (a separate concern; see /low-level-design/error-handling).
- Fallback values, failure rates and which errors count (we come back to them in Extensibility).
- Sharing breaker state across servers.