๐Ÿ”ฅ 0
โญ 0
Lesson 2 of 5 18 min +200 XP

Circuit Breaker States

A circuit breaker is a state machine with three states. Understanding these states and their transitions is key to using circuit breakers effectively.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                                                                 โ”‚
โ”‚    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      Failures exceed      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”‚
โ”‚    โ”‚  CLOSED  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€ threshold โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ถ โ”‚   OPEN   โ”‚         โ”‚
โ”‚    โ”‚  (Green) โ”‚                           โ”‚  (Red)   โ”‚         โ”‚
โ”‚    โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜                           โ””โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜         โ”‚
โ”‚         โ”‚                                      โ”‚               โ”‚
โ”‚         โ”‚                                      โ”‚ Timeout       โ”‚
โ”‚    Requests                                    โ”‚ expires       โ”‚
โ”‚    succeed                                     โ”‚               โ”‚
โ”‚         โ”‚                                      โ–ผ               โ”‚
โ”‚         โ”‚                               โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”          โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Success โ—€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ HALF-OPEN โ”‚          โ”‚
โ”‚                                         โ”‚ (Yellow)  โ”‚          โ”‚
โ”‚                                         โ””โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”˜          โ”‚
โ”‚                                               โ”‚                โ”‚
โ”‚                              Failure โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                โ”‚
โ”‚                              (reopens)                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

State 1: Closed (Normal Operation)

In the Closed state, the circuit breaker is "off" - all requests flow through normally.

๐ŸŸข

CLOSED STATE

Behavior: All requests are allowed through to the downstream service.

Monitoring: Tracks success/failure of each request in a sliding window.

Transition: Opens when failure rate exceeds threshold.

What Happens in Closed State

def call_in_closed_state(request):
    try:
        response = downstream_service.call(request)
        record_success()
        return response
    except Exception as e:
        record_failure()

        # Check if we should trip the circuit
        if failure_rate() > THRESHOLD:
            transition_to_open()

        raise e

Tracking Failures

Circuit breakers typically use a sliding window to track recent failures:

Count-Based Window

Track the last N requests.

Last 10 requests:
[โœ“, โœ“, โœ—, โœ“, โœ—, โœ—, โœ“, โœ—, โœ—, โœ—]
Failure rate: 60%

Time-Based Window

Track requests in the last N seconds.

Last 60 seconds:
1000 requests total
350 failures
Failure rate: 35%

State 2: Open (Failing Fast)

In the Open state, the circuit breaker rejects all requests immediately without calling the downstream service.

๐Ÿ”ด

OPEN STATE

Behavior: All requests fail immediately (no call to downstream).

Purpose: Protect system resources, give downstream time to recover.

Transition: Moves to Half-Open after timeout period.

What Happens in Open State

def call_in_open_state(request):
    # Don't even try to call the downstream service
    # Fail immediately with a circuit breaker exception

    if time_since_opened() > RESET_TIMEOUT:
        transition_to_half_open()
        return call_in_half_open_state(request)

    raise CircuitOpenException("Service unavailable, circuit is open")

Benefits of Open State

โšก
Fast Failure

Milliseconds instead of timeout seconds

๐Ÿงต
Save Threads

No threads blocked waiting

๐Ÿ”„
Recovery Time

Downstream can recover

๐Ÿ“‰
Reduce Load

Stop hammering broken service

State 3: Half-Open (Testing Recovery)

After the timeout period, the circuit enters Half-Open state to test if the downstream service has recovered.

๐ŸŸก

HALF-OPEN STATE

Behavior: Allow limited requests through to test recovery.

If success: Transition to Closed (service recovered).

If failure: Transition back to Open (still broken).

What Happens in Half-Open State

def call_in_half_open_state(request):
    # Only allow one request through at a time
    if not acquire_test_lock():
        raise CircuitOpenException("Circuit half-open, test in progress")

    try:
        response = downstream_service.call(request)

        # Success! Service has recovered
        transition_to_closed()
        return response

    except Exception as e:
        # Still failing - go back to open
        transition_to_open()
        raise e

    finally:
        release_test_lock()

Half-Open Strategies

Single Request Test

Allow one request. If it succeeds, close the circuit.

Pro: Simple, fast recovery
Con: One fluke success might close prematurely

Multiple Request Test

Allow N requests. Close only if all (or most) succeed.

Pro: More confident recovery detection
Con: Slower recovery, more test traffic

State Transition Diagram

Here's the complete flow with triggers:

From To Trigger
Closed Open Failure rate > threshold (e.g., 50%)
Open Half-Open Reset timeout expires (e.g., 30 seconds)
Half-Open Closed Test request(s) succeed
Half-Open Open Test request fails

Timeline Example

Let's trace a circuit breaker through a real scenario:

T+0s [CLOSED] Payment service healthy, processing requests
T+10s [CLOSED] Still healthy, 0% failure rate
T+20s [CLOSED] Payment DB goes down, failures starting
T+25s [CLOSED] Failure rate at 30%, monitoring...
T+30s [CLOSED โ†’ OPEN] Failure rate hits 50% threshold!
T+35s [OPEN] Rejecting all requests immediately
T+45s [OPEN] Payment DB recovered, but circuit still open
T+55s [OPEN] Waiting for reset timeout (30s)...
T+60s [OPEN โ†’ HALF-OPEN] Timeout expired, testing...
T+61s [HALF-OPEN] Test request sent...
T+61s [HALF-OPEN โ†’ CLOSED] Test succeeded! Service recovered!
T+65s [CLOSED] Normal operation resumed

Common Configurations

Different thresholds for different scenarios:

Critical Service

Payment processing - fail fast, recover carefully

failure_threshold: 25%
reset_timeout: 60s
half_open_requests: 3

Non-Critical Service

Recommendations - tolerate more failures

failure_threshold: 50%
reset_timeout: 30s
half_open_requests: 1

Key Takeaways

  • Closed - Normal operation, monitoring failures
  • Open - Failing fast, protecting resources, waiting to test
  • Half-Open - Testing if service recovered
  • Sliding window tracks recent failures for threshold decisions

Next up: Building a Circuit Breaker - Implement your own circuit breaker from scratch.

๐Ÿง  Quick Quiz

Test your understanding of this lesson.

1

In which state does a circuit breaker allow all requests to pass through?

2

What triggers the transition from Closed to Open?

3

What is the purpose of the Half-Open state?

Why Circuit Breakers?