Prerequisites
What it is
A circuit breaker tracks calls to a dependency and temporarily stops sending traffic when failures cross a threshold.
Why it matters
If a dependency is down, blindly retrying can make the outage worse. Circuit breakers fail fast, preserve resources, and give the dependency time to recover.
How it works
The breaker starts closed. After enough failures, it opens and immediately rejects or falls back. After a cooldown, it becomes half-open and allows limited test requests.
# state: status (closed|open|half_open), failures, last_failure_time
# config: threshold=5, cooldown=30s
function on_request(request):
if status == "open":
if now() - last_failure_time > cooldown:
status = "half_open" # try one request
else:
return fallback(request) # fail fast
try:
result = call_dependency(request)
on_success()
return result
except error:
on_failure()
return fallback(request)
function on_success():
failures = 0
status = "closed"
function on_failure():
failures += 1
last_failure_time = now()
if failures >= threshold:
status = "open"
The half-open state is the recovery probe: one request goes through. If it succeeds, the circuit closes. If it fails, the circuit reopens and the cooldown restarts.
Closed — requests flow through normally. Failures are counted against a threshold. Open — requests are rejected immediately or routed to a fallback. No calls reach the dependency. Half-open — a limited number of test requests are allowed through. Success closes the circuit; failure reopens it.
When to use it
Use it around remote services, payment providers, search clusters, notification providers, and any dependency that can become slow or unavailable.
Tradeoffs / failure modes
Thresholds are hard. Opening too eagerly causes avoidable downtime; opening too late allows cascading failure. Fallbacks must be honest about degraded behavior.
Tiny example
If a webhook provider times out repeatedly, the delivery service opens the circuit and queues retries instead of tying up workers.
Related guides and systems
See Webhooks, Rate Limiting, Caching Strategies, Netflix, Slack, and GitHub.
Seen in deeper dives
Capability
Caching Strategies
Cache-aside, write-through, invalidation patterns, and distributed caching with Redis
Capability
Rate Limiting & Throttling
Token bucket, sliding window, and distributed rate limiting to protect your APIs
Capability
Webhooks
Reliable event delivery to external systems with retry, HMAC verification, and monitoring
Case Study
GitHub
The world's largest code hosting platform powered by Git at scale
Case Study
Netflix
Global video streaming with personalized content delivery
Case Study
Slack
Channel-based messaging for the enterprise workplace
Related concepts
Backpressure
Slow producers down when consumers cannot keep up.
Idempotency
Make repeated requests safe so retries do not create duplicate side effects.
Message Queue
Buffer work between producers and consumers so systems can absorb spikes and retry safely.