What is the circuit breaker pattern and what are its three states?
The circuit breaker pattern is a fault-tolerance mechanism that prevents a service from repeatedly calling a dependency that is failing or slow, which would otherwise waste resources, increase latency, and propagate failures upstream. The name comes from electrical circuit breakers: when current exceeds a safe threshold, the breaker trips to protect the circuit.
A circuit breaker wraps calls to an external dependency (database, third-party API, microservice) and tracks the outcome of recent calls. Based on failure rate or count, it transitions through three states:
**Closed (normal operation)**: All calls pass through to the dependency. The breaker counts failures in a rolling window (e.g., last 10 calls or last 60 seconds). If the failure rate exceeds a threshold (e.g., 50%), the breaker **trips** to Open.
**Open (failing fast)**: All calls are immediately rejected without contacting the dependency — they fail fast with a fallback response (cached data, default value, error to client). A timer starts. This prevents thread-pool exhaustion and gives the dependency time to recover. After the timeout (e.g., 30 seconds), the breaker transitions to Half-Open.
**Half-Open (probing for recovery)**: A limited number of probe requests are allowed through to the dependency. If the probes succeed (failure rate drops below threshold), the breaker resets to Closed. If probes fail, it immediately trips back to Open and restarts the timer. This prevents premature recovery that would immediately overload a struggling service.
Two failure metrics are commonly used: **count-based** (trip after N consecutive failures) and **rate-based** (trip when failure % exceeds threshold within a time window). Rate-based is generally more accurate under variable traffic — high failure counts in absolute terms may represent a low failure rate during peak traffic.
Fallback strategies: return a cached response, return a default/empty response, queue the request for later processing, or return a user-visible error with appropriate messaging ("Service temporarily unavailable").
In practice: libraries like Hystrix (deprecated), Resilience4j (JVM), Polly (.NET), and PyBreaker implement this pattern. Service meshes like Istio implement circuit breaking at the infrastructure layer, removing the need for application-level instrumentation. Key configuration parameters: failure threshold, time window, open-state timeout, half-open probe count.
Correctly describes all three states and their transitions, explains the core problem (cascading failures / thread exhaustion), and names at least one fallback strategy.
Distinguishes count-based vs. rate-based failure metrics, explains the purpose of Half-Open probing (preventing thundering herd on recovery), names specific libraries or service mesh implementations, and discusses fallback design.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment