Circuit Breaker
The Circuit Breaker is a stability pattern used in distributed systems to prevent a single service failure from causing a cascading collapse of the entire application. Inspired by electrical circuit breakers, this software implementation monitors for failures in calls to an external service or resource. If the failure rate crosses a predefined threshold, the circuit “trips” (opens), and all subsequent calls to the service are immediately failed or routed to a fallback response without attempting the actual request. This gives the failing service time to recover and protects the calling system’s resources from being exhausted by waiting for timed-out requests.
Key Concepts
- Closed State: The normal state where requests are allowed to flow to the external service. The circuit breaker monitors the success and failure rates of these calls.
- Open State: The state triggered when the failure threshold is reached. Requests are blocked and a fallback or error is returned immediately.
- Half-Open State: A temporary state after an open period where the circuit breaker allows a limited number of test requests to pass through. If these succeed, the circuit closes; if they fail, it returns to the open state.
- Thresholds: Parameters that define when the circuit should trip, such as the maximum number of consecutive failures or the percentage of failed requests within a time window.
- Fallback Response: A predefined, safe action to take when the circuit is open, such as returning cached data, a default value, or a user-friendly error message.
Common Patterns or Tools
- Netflix Hystrix: A pioneer library (now in maintenance) for implementing circuit breakers and providing latency/fault tolerance in distributed systems.
- Resilience4j: A lightweight, modular fault-tolerance library for Java inspired by Hystrix, supporting circuit breakers, rate limiters, and retries.
- Polly: A .NET resilience and transient-fault-handling library that allows developers to express policies like Circuit Breaker in a fluent manner.
- Service Mesh (Istio/Linkerd): Modern service meshes often provide circuit breaking functionality at the infrastructure level, allowing for consistent resilience policies across different languages and services.