What is the Circuit Breaker pattern and how does it improve resilience?
Learn how the circuit breaker pattern improves microservice resilience by failing fast, preventing cascading failures, and testing recovery.
Expected Interview Answer
The Circuit Breaker pattern is a resilience mechanism that stops a service from repeatedly calling a failing dependency by 'tripping' after a threshold of errors, failing fast instead, and periodically testing whether the dependency has recovered.
It works as a state machine with three states: Closed (calls flow through, failures are counted), Open (calls are rejected immediately for a cooldown period), and Half-Open (a few trial calls are allowed to check recovery). By failing fast when a downstream is unhealthy, the breaker prevents threads and connections from piling up on doomed calls, contains cascading failures, and gives the struggling dependency room to recover.
- Prevents cascading failures across services
- Fails fast instead of exhausting threads on slow calls
- Gives failing dependencies time to recover
- Enables graceful degradation with fallbacks
- Improves overall system stability under partial outages
AI Mentor Explanation
Think of a bowler who keeps getting hammered for boundaries every over. Rather than letting him bleed runs indefinitely, the captain takes him off for a spell, tries someone else, and only brings him back for a trial over later to see if he has settled. The circuit breaker does the same: after too many failed deliveries to a service it pulls that route out, then tests it again before fully trusting it.
Step-by-Step Explanation
Step 1
Start in Closed state
Requests pass through to the dependency while the breaker counts failures within a rolling window.
Step 2
Trip to Open on threshold
When failures or slow calls exceed the configured threshold, the breaker opens and rejects calls immediately.
Step 3
Fail fast with a fallback
While Open, return a fast fallback (cached data, default value, or queued request) instead of waiting on the dead dependency.
Step 4
Move to Half-Open after cooldown
After a timeout, allow a limited number of trial calls to test whether the dependency has recovered.
Step 5
Close or re-open based on trials
If trial calls succeed, close the breaker and resume normal traffic; if they fail, re-open and wait again.
What Interviewer Expects
- The three states: Closed, Open, Half-Open
- Understanding of failing fast to avoid thread/connection exhaustion
- How it prevents cascading failures
- Awareness of fallbacks and graceful degradation
- Familiarity with tools like Resilience4j, Hystrix, or Polly
Common Mistakes
- Confusing a circuit breaker with a simple retry
- Forgetting the Half-Open recovery-testing state
- Setting thresholds so tight or loose that the breaker never helps
- Not pairing the breaker with a meaningful fallback
- Sharing one breaker across unrelated dependencies
Best Answer (HR Friendly)
“A circuit breaker is like an electrical fuse for software: when a service it depends on keeps failing, it stops calling it for a while so the whole system doesn't get dragged down, and then it quietly checks later to see if things have recovered.”
Code Example
CircuitBreakerConfig config = CircuitBreakerConfig.custom()
.failureRateThreshold(50) // trip at 50% failures
.waitDurationInOpenState(Duration.ofSeconds(10))
.permittedNumberOfCallsInHalfOpenState(3)
.slidingWindowSize(20)
.build();
CircuitBreaker breaker = CircuitBreaker.of("inventory", config);
Supplier<String> call = CircuitBreaker
.decorateSupplier(breaker, inventoryClient::getStock);
String stock = Try.ofSupplier(call)
.recover(ex -> "unavailable") // fallback
.get();Follow-up Questions
- How does a circuit breaker differ from a retry with backoff?
- What is the Half-Open state and why is it needed?
- How would you choose the failure threshold and cooldown period?
- How do circuit breakers and bulkheads complement each other?
- What metrics would you monitor to tune a circuit breaker?
MCQ Practice
1. In which state does a circuit breaker reject calls immediately?
In the Open state the breaker fails fast, rejecting calls without hitting the failing dependency.
2. What is the purpose of the Half-Open state?
Half-Open lets a few trial requests through to check whether the dependency has recovered before fully closing.
3. A key benefit of the circuit breaker pattern is:
By failing fast on unhealthy dependencies, the breaker stops failures from cascading through the system.
Flash Cards
What are the three circuit breaker states? — Closed (normal), Open (fail fast), and Half-Open (trial calls to test recovery).
Why fail fast? — To avoid tying up threads and connections on calls to a dependency that is already failing.
What triggers the Open state? — Failures or slow calls exceeding a configured threshold within a rolling window.
Name a circuit breaker library. — Resilience4j, Netflix Hystrix, or Polly (.NET).
Continue Learning
Related Interview Questions
What is the Bulkhead pattern and how does it isolate failures?
medium
How do you make a microservice endpoint idempotent, and why is exactly-once delivery a myth?
hard
How do you handle a poison message, and what belongs in a dead-letter queue?
medium
How do you keep a shared cache correct across microservices, and how do you stop a cache stampede when a hot key expires?
hard