Problem
In distributed systems, a single slow or failed downstream service can exhaust thread pools, saturate connections, and cause cascading failures that take down the entire application. Retries without protection make this worse.
Prevent cascading failures in distributed systems by detecting when a downstream service is unhealthy and failing fast instead of blocking the entire call chain.
In distributed systems, a single slow or failed downstream service can exhaust thread pools, saturate connections, and cause cascading failures that take down the entire application. Retries without protection make this worse.
Prevent cascading failures in distributed systems by detecting when a downstream service is unhealthy and failing fast.
class CircuitBreaker:
def __init__(self, failure_threshold, recovery_timeout, half_open_max_calls):
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
self.half_open_max_calls = half_open_max_calls
self.failure_count = 0
self.state = "closed"
self.last_failure_time = None
async def call(self, func, *args, **kwargs):
if self.state == "open":
if self._should_attempt_reset():
self.state = "half-open"
else:
return self._fallback()
try:
result = await func(*args, **kwargs)
self._on_success()
return result
except Exception as e:
self._on_failure()
raise
def _on_success(self):
self.failure_count = 0
if self.state == "half-open":
self.state = "closed"
def _on_failure(self):
self.failure_count += 1
if self.failure_count >= self.failure_threshold:
self.state = "open"
self.last_failure_time = time.time()
def _should_attempt_reset(self):
return self.last_failure_time and \
time.time() - self.last_failure_time >= self.recovery_timeout
| Category | Assessment | |----------|-----------| | Infrastructure | Low — circuit breaker logic in service code | | Operational | Low — self-regulating once configured | | Cognitive | Medium — understanding when/how to tune |