Skip to content

Circuit_breaker

Protecting Systems from Cascading Failures

Section titled “Protecting Systems from Cascading Failures”

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! |
+----------------------------------+

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 again

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 |
+--------------------+--------------------------------+
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 = OPEN

BenefitDescription
Fail FastDon’t wait for timeouts
Protect SystemPrevent cascading failures
RecoveryAllow services to recover
ResilienceHandle partial outages
VisibilityMonitor service health

LanguageLibrary
JavaResilience4j, Hystrix
.NETPolly
Node.jsopossum
Pythonpybreaker
Gocircuitbreaker

Key circuit breaker concepts:

  1. Three states - Closed, Open, Half-Open
  2. Fail fast - Don’t wait for timeouts
  3. Auto-recovery - Test if service is healthy
  4. Prevents cascading - Isolate failures
  5. Monitor - Track circuit state
  6. Configure thresholds - Tune for your system

Next: Chapter 22: Rate Limiting & Throttling