Concepts/ReliabilityIntermediate4 min

Circuit Breaker

Stop calling an unhealthy dependency before failures cascade through the system.

circuit-breakerresiliencefailuresreliability

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.

Loading diagram...
Circuit breaker state machine: closed, open, half-open

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.

See Webhooks, Rate Limiting, Caching Strategies, Netflix, Slack, and GitHub.

Seen in deeper dives

Related concepts