📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Microservices Circuit Breaker

Circuit Breaker

5 min read Quiz at the end
Circuit breaker stops calling failing services after N failures — prevents cascading failures across services.

Circuit Breaker Pattern

# States: CLOSED -> OPEN -> HALF-OPEN -> CLOSED
# CLOSED:    requests pass through normally
# OPEN:      service failing, reject immediately (fast fail)
# HALF-OPEN: send one request to test recovery

import pybreaker

breaker = pybreaker.CircuitBreaker(
    fail_max=5,       # open after 5 consecutive failures
    reset_timeout=30  # try half-open after 30 seconds
)

@breaker
def call_payment(order_id: int) -> dict:
    return httpx.post("http://payment-svc/charge",
                      json={"order_id":order_id},
                      timeout=5.0).json()

try:
    result = call_payment(order_id)
except pybreaker.CircuitBreakerError:
    # Circuit OPEN -- fail fast, use fallback
    return {"status":"queued","message":"Retry shortly"}
Topic Quiz · 1 questions

Test your understanding before moving on

1. What are the three states of a Circuit Breaker?
💡 CLOSED=normal, OPEN=failing (reject fast), HALF-OPEN=testing recovery — prevents cascading failures.