Circuit_breaker
Chapter 21: Circuit Breaker Pattern
Section titled “Chapter 21: Circuit Breaker Pattern”Protecting Systems from Cascading Failures
Section titled “Protecting Systems from Cascading Failures”21.1 What is Circuit Breaker?
Section titled “21.1 What is Circuit Breaker?”The Circuit Breaker pattern prevents cascading failures by detecting when a service is failing and stopping requests to that service temporarily.
Circuit Breaker Analogy =======================
Electrical Circuit Breaker: +----------------------------------+ | | | Power ---> [Breaker] ---> Appliance | | | | Too much current -> Breaker trips | | Appliance protected! | +----------------------------------+
Software Circuit Breaker: +----------------------------------+ | | | Request ---> [Breaker] ---> Service | | | | Too many failures -> Circuit opens | | Fail fast, protect system! | +----------------------------------+21.2 How Circuit Breaker Works
Section titled “21.2 How Circuit Breaker Works”Three States
Section titled “Three States” Circuit Breaker States =====================
+----------+ +----------+ +----------+ | CLOSED | ---->| OPEN | ---->| HALF-OPEN | +----------+ +----------+ +----------+ ^ | | | v | | +----------+ | +----------- | RESET | <--------+ +----------+
CLOSED: - Normal operation - Requests pass through - Failures counted
OPEN: - Service considered failing - Requests fail immediately - No calls to failing service
HALF-OPEN: - Testing if service recovered - Limited requests allowed - If success -> Close circuit - If fail -> Open again21.3 Implementation
Section titled “21.3 Implementation”Configuration
Section titled “Configuration” Circuit Breaker Parameters ==========================
+--------------------+--------------------------------+ | Parameter | Description | +--------------------+--------------------------------+ | Failure Threshold | Failures before opening | | | e.g., 5 failures in 10 sec | +--------------------+--------------------------------+ | Success Threshold | Successes to close circuit | | | e.g., 3 successes | +--------------------+--------------------------------+ | Timeout | Time before half-open | | | e.g., 30 seconds | +--------------------+--------------------------------+ | Half-open requests | Requests allowed in half-open| | | e.g., 3 requests | +--------------------+--------------------------------+Code Example
Section titled “Code Example”class CircuitBreaker: def __init__(self, failure_threshold=5, timeout=30): self.failure_threshold = failure_threshold self.timeout = timeout self.state = CLOSED self.failures = 0 self.last_failure_time = None
def call(self, func): if self.state == OPEN: if time.time() - self.last_failure_time > self.timeout: self.state = HALF_OPEN else: raise CircuitOpenException()
try: result = func() self.on_success() return result except Exception as e: self.on_failure() raise e
def on_success(self): self.failures = 0 self.state = CLOSED
def on_failure(self): self.failures += 1 self.last_failure_time = time.time() if self.failures >= self.failure_threshold: self.state = OPEN21.4 Benefits
Section titled “21.4 Benefits”Why Use Circuit Breaker?
Section titled “Why Use Circuit Breaker?”| Benefit | Description |
|---|---|
| Fail Fast | Don’t wait for timeouts |
| Protect System | Prevent cascading failures |
| Recovery | Allow services to recover |
| Resilience | Handle partial outages |
| Visibility | Monitor service health |
21.5 Integration with Libraries
Section titled “21.5 Integration with Libraries”Popular Libraries
Section titled “Popular Libraries”| Language | Library |
|---|---|
| Java | Resilience4j, Hystrix |
| .NET | Polly |
| Node.js | opossum |
| Python | pybreaker |
| Go | circuitbreaker |
Summary
Section titled “Summary”Key circuit breaker concepts:
- Three states - Closed, Open, Half-Open
- Fail fast - Don’t wait for timeouts
- Auto-recovery - Test if service is healthy
- Prevents cascading - Isolate failures
- Monitor - Track circuit state
- Configure thresholds - Tune for your system