What is the Circuit Breaker Pattern?
Learn the circuit breaker pattern: closed, open, and half-open states, why it prevents cascading failure, and how to pair it with fallbacks.
Expected Interview Answer
The circuit breaker pattern wraps calls to a downstream service and stops sending traffic to it once failures cross a threshold, failing fast locally instead of letting slow or broken calls pile up and cascade failure through the whole system.
A circuit breaker has three states: closed, where requests pass through normally while failures are counted; open, entered once the failure rate or count crosses a threshold, where calls are rejected immediately without even attempting the network call; and half-open, entered after a cooldown period, where a limited number of trial requests are allowed through to test whether the downstream service has recovered. If those trial requests succeed the breaker closes again and normal traffic resumes; if they fail it reopens and the cooldown restarts. This protects the calling service from wasting threads and connections on a downstream dependency that is timing out or erroring, which is critical because slow failures are often worse than fast ones β a hung call holding a thread can exhaust a connection pool and take down an otherwise healthy service. Circuit breakers are commonly combined with a fallback (cached data, a default response, or a degraded feature) so the caller can still return something useful to its own callers while the breaker is open.
- Prevents cascading failure from an unhealthy downstream dependency
- Frees up threads/connections instead of blocking on slow calls
- Enables graceful degradation via fallback responses
- Automatically probes and recovers once the dependency is healthy again
AI Mentor Explanation
The circuit breaker pattern is like a captain benching a bowler who has been hit for boundaries repeatedly in an over, stopping the damage instead of letting them keep bowling and leaking more runs. While benched, the captain does not even consider giving that bowler the ball, just like a breaker rejecting calls instantly while open. After a rest, the captain gives the bowler a single trial over to see if their form has returned; if it goes well they are trusted with the full spell again, and if not they are benched once more. This protects the teamβs total score from one struggling bowler doing further damage.
Step-by-Step Explanation
Step 1
Start closed
Requests to the downstream service pass through normally while success/failure counts are tracked.
Step 2
Trip to open
Once the failure rate or count crosses a configured threshold within a window, the breaker opens and rejects calls immediately.
Step 3
Cool down
After a configured timeout, the breaker moves to half-open and allows a small number of trial requests through.
Step 4
Close or reopen
If trial requests succeed the breaker closes and normal traffic resumes; if they fail it reopens and the cooldown restarts.
What Interviewer Expects
- Ability to name and explain closed, open, and half-open states
- Understanding of why fast failure is better than slow failure/thread exhaustion
- Knowledge of pairing circuit breakers with fallbacks for graceful degradation
- Awareness of concrete tools/libraries (e.g. resilience4j, Envoy outlier detection, Hystrix historically)
Common Mistakes
- Confusing a circuit breaker with a simple retry policy
- Not implementing the half-open probing state, causing permanent outages or thrashing
- Setting thresholds too sensitive, tripping on normal transient blips
- Forgetting to pair the breaker with a meaningful fallback response
Best Answer (HR Friendly)
βA circuit breaker stops our service from hammering a downstream dependency that is already struggling, similar to an electrical breaker tripping to prevent further damage. Once it detects too many failures, it stops sending traffic and fails fast instead, then periodically tests whether the dependency has recovered before letting traffic flow normally again. This keeps one failing service from taking down everything connected to it.β
Code Example
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: payments-service
spec:
host: payments-service
trafficPolicy:
connectionPool:
http:
http1MaxPendingRequests: 10
maxRequestsPerConnection: 2
outlierDetection:
consecutive5xxErrors: 5
interval: 30s
baseEjectionTime: 60s
maxEjectionPercent: 50Follow-up Questions
- What is the difference between a circuit breaker and a simple retry with backoff?
- Why is the half-open state necessary rather than just closing after the timeout?
- How would you choose a good fallback response when the breaker is open?
- How does Envoy outlier detection implement circuit-breaking behavior?
MCQ Practice
1. What are the three standard circuit breaker states?
A circuit breaker moves between closed (normal), open (failing fast), and half-open (testing recovery) states.
2. What does an open circuit breaker do to new requests?
While open, the breaker fails fast, rejecting calls immediately instead of letting them hang on a failing dependency.
3. What is the purpose of the half-open state?
Half-open lets a small number of probe requests through; success closes the breaker again, failure reopens it.
Flash Cards
What problem does a circuit breaker solve? β Stops cascading failure by failing fast against an unhealthy downstream dependency.
Name the three circuit breaker states. β Closed, open, half-open.
What happens in the open state? β Calls are rejected immediately without attempting the network call.
What often pairs with a circuit breaker? β A fallback response for graceful degradation while the breaker is open.