14.2System Design Building Blocks

Circuit Breaker

Protect your system from cascading failures with the Circuit Breaker pattern. Watch the three-state finite state machine (Closed, Open, Half-Open) respond to service health changes.

Presets
Threshold:3
Timeout:10t
State Machine
CLOSEDfails: 0/3
OPEN
HALF-OPEN
fails >= 3timeoutsuccessfailure
Request Flow
Client
Closed
Service
Failure Counter
0 / 3 failures
Closed Statet=0

Requests pass through normally. The failure counter tracks errors. If failures exceed the threshold, the breaker trips to Open.

State Timeline
State transitions will appear here
SuccessFailureRejected
Request Outcomes
Success: 0Failures: 0Rejected: 0
Event Log
Events will appear here. Press Play or Step to begin.
1.0x
Total Requests0
Successes0
Failures0
Rejections0
StateClosed
Time in State0 ticks
Failure Rate0.0%
Understanding the Circuit Breaker Pattern

How It Works

The Circuit Breaker pattern prevents cascading failures in distributed systems. It wraps calls to an external service and monitors for failures. Like an electrical circuit breaker that trips to prevent damage, it stops sending requests to a failing service, giving it time to recover. The pattern uses a three-state finite state machine: Closed (normal), Open (rejecting), and Half-Open (testing).

The Three States

Closed

Normal operation. Requests pass through. Failures are counted. Trips to Open when threshold is exceeded.

Open

Fail-fast mode. All requests are immediately rejected. A timeout timer runs. Transitions to Half-Open when timer expires.

Half-Open

Testing mode. Limited probe requests are sent. Success closes the breaker. Failure re-opens it.

Key InsightWithout a circuit breaker, when a downstream service fails, every upstream caller keeps sending requests, consuming threads, sockets, and memory while waiting for timeouts. This cascading failure can bring down the entire system. The circuit breaker "fails fast" during outages, preserving resources and giving the failing service time to recover. Netflix Hystrix popularized this pattern, and it is now implemented in Resilience4j (Java), Polly (.NET), and built into service meshes like Istio and Envoy.

Configuration Parameters

ParameterCurrentTypicalDescription
Failure Threshold33-5Number of failures before tripping to Open
Recovery Timeout10t30-60sTime in Open before transitioning to Half-Open
Half-Open Probes21-3Successful probes needed to close the circuit

Real-World Applications

The Circuit Breaker pattern was popularized by Michael Nygard in "Release It!" and later by Netflix Hystrix. It is now a standard pattern in microservices architectures. Spring Cloud Circuit Breaker, Resilience4j, Polly (.NET), and gobreaker (Go) are popular implementations. Service meshes like Istio and Envoy provide circuit breaking at the infrastructure level, and cloud providers like AWS offer circuit breaker support in their App Mesh and API Gateway services. It is commonly used for database connections, HTTP calls, message queue producers, and any external service dependency.