Circuit Breaker Pattern
The circuit breaker pattern prevents an application from repeatedly calling a failing downstream service, giving the service time to recover while returning fast failures to callers — stopping cascading failures across a distributed system.
What It Really Means
In a microservices architecture, Service A calls Service B, which calls Service C. If Service C becomes slow or unresponsive, requests back up in Service B. Service B's thread pool fills up. Now Service B cannot serve requests either. Service A's requests to B start timing out. The failure cascades through the entire system from a single point of failure.
The circuit breaker pattern, popularized by Michael Nygard in "Release It!" (2007), works exactly like an electrical circuit breaker. When a downstream service fails too many times, the circuit "opens" and all subsequent calls fail immediately without even attempting the request. After a cooldown period, the circuit enters a "half-open" state and allows a few test requests through. If those succeed, the circuit closes and normal operation resumes. If they fail, the circuit opens again.
This pattern is implemented in Netflix Hystrix (now in maintenance mode), Resilience4j (the modern Java alternative), Polly (.NET), and is built into service meshes like Istio and Envoy. If you are building microservices, you will use circuit breakers.
How It Works in Practice
The Three States
Closed (normal operation): Requests pass through to the downstream service. The circuit breaker tracks the error rate. If the error rate exceeds a threshold (e.g., 50% of the last 100 requests), the circuit transitions to Open.
Open (failing fast): All requests are immediately rejected without calling the downstream service. The caller receives a fallback response or an error. After a configurable timeout (e.g., 30 seconds), the circuit transitions to Half-Open.
Half-Open (testing recovery): A limited number of requests (e.g., 5) are allowed through to the downstream service. If they succeed, the circuit closes. If any fail, the circuit opens again.
Real-World: Netflix Hystrix
Netflix built Hystrix because a single slow dependency could bring down their entire streaming platform. With hundreds of microservices, each calling multiple downstream services, cascading failures were existential.
Hystrix wrapped every inter-service call in a circuit breaker. When the recommendation service became slow, Hystrix opened the circuit and returned cached recommendations or a generic "trending" list. The streaming service continued working — users saw slightly degraded recommendations instead of a blank screen.
Hystrix also introduced the bulkhead pattern: each downstream dependency gets its own thread pool. If the payment service is slow, it fills its own 20-thread pool but does not affect the 20-thread pool allocated for the inventory service.
Real-World: Envoy Service Mesh
Envoy proxy implements circuit breaking at the infrastructure layer. Instead of adding circuit breaker code to every service, you configure Envoy to monitor error rates and open circuits automatically. This is particularly powerful in Kubernetes environments where Envoy runs as a sidecar proxy for every pod.
Envoy's circuit breaker tracks: max connections, max pending requests, max requests, and max retries — opening the circuit when any threshold is exceeded.
Implementation