ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/loadbalancers/tasks/task-20-2-4-circuit-breaking
TASK

Implementation

Circuit states:. . CLOSED (normal). - Requests pass through to backend. - Track failures in a sliding window. - If failures > threshold → OPEN. OPEN (failing). - All requests fail fast (no backend attempts). - After timeout → HALF_OPEN. HALF_OPEN (testing). - Allow one probe request through. - If success → CLOSED. - If failure → OPEN. . Failure tracking:. typescript. state: "CLOSED" | "OPEN" | "HALF_OPEN";. failureCount: number;. lastFailureTime: number;. successCount: number;. // Configuration. failureThreshold: number = 5; // Open after 5 failures. timeout: number = 60000; // Try again after 60s. halfOpenMaxAttempts: number = 1; // One probe request. . Example circuit breaking:. json. // Backend is healthy (circuit closed):. // Backend starts failing (5 failures in 30s):. // After 60s timeout (half-open):. . Circuit breaker metrics:. json. "backend": "api-1",. "state": "OPEN",. "failure_count": 7,. "last_failure_time": 1680123456,. "opened_at": 1680123450,. "next_attempt_at": 1680123510.

Sample Test Cases

Circuit opens after threshold failuresTimeout: 5000ms
Input
{"src":"client","dest":"l7_proxy","body":{"type":"http_request","msg_id":1,"method":"GET","path":"/api/users","simulate_failures":5}}
{"src":"client","dest":"l7_proxy","body":{"type":"http_request","msg_id":2,"method":"GET","path":"/api/users"}}
Expected Output
{"src": "l7_proxy", "dest": "client", "body": {"type": "http_response", "in_reply_to": 1, "status": 500, "backend": "api-1"}}
Circuit closes after successful probeTimeout: 5000ms
Input
{
  "src": "client",
  "dest": "l7_proxy",
  "body": {
    "type": "http_request",
    "msg_id": 1,
    "method": "GET",
    "path": "/api/users",
    "circuit_state": "HALF_OPEN",
    "probe_result": "success"
  }
}
Expected Output
{"src": "l7_proxy", "dest": "client", "body": {"type": "http_response", "in_reply_to": 1, "status": 200, "backend": "api-1", "circuit_state": "CLOSED"}}

Hints

Hint 1
Track failures per backend: if failure_count > threshold (e.g., 5), open circuit
Hint 2
Open circuit: reject requests immediately without trying backend
Hint 3
After timeout (e.g., 60s), enter half-open state: send one probe request
Hint 4
If probe succeeds, close circuit; if fails, reopen circuit
Hint 5
This prevents cascading failures when a backend is overwhelmed
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

circuit breakerfailure thresholdhalf-open stateautomatic recoverycascade prevention
main.py
python
Implement Circuit Breaking - Load Balancers | Build Distributed Systems