Circuit Breaker Strategy
The circuit breaker reactive resilience strategy shortcuts the execution if the underlying resource is detected as unhealthy. When a circuit is broken, subsequent calls are immediately rejected without attempting execution.When to Use Circuit Breaker
Use the circuit breaker strategy when:- Protecting downstream services from being overwhelmed during failures
- Failing fast is better than making users wait for timeouts
- You want to give a failing system time to recover
- Preventing cascading failures in microservices architectures
- Implementing the “stop doing it if it hurts” principle
Installation
Circuit States
The circuit breaker has four states:Open (Broken)
Circuit is broken. All operations are immediately rejected with
BrokenCircuitException.Half-Open (Testing)
After the break duration expires, the circuit allows one test operation to check if the system has recovered.
Usage
Basic Circuit Breaker
Custom Thresholds
Dynamic Break Duration
Handling HTTP Status Codes
Monitoring Circuit State
Manual Circuit Control
State Transition Events
Configuration Options
Defines which results and/or exceptions are counted as failures.
The failure-success ratio that will cause the circuit to break.
0.1 means 10% of sampled executions must fail.The minimum number of executions that must occur within the sampling duration before the circuit can break.
The time period over which the failure-success ratio is calculated.
Fixed time period for which the circuit will remain broken before attempting to reset.
Dynamically calculates the break duration using runtime information like failure count. If set,
BreakDuration is ignored.Enables manual control of circuit state via
IsolateAsync() and CloseAsync() methods.Enables retrieving the current circuit state for health reporting and monitoring.
Invoked after the circuit transitions to the
Closed or Isolated state.Invoked after the circuit transitions to the
Open state.Invoked after the circuit transitions to the
HalfOpen state.Best Practices
Combine with Retry
Combine with Retry
Place circuit breaker inside retry strategy. This allows retry to respect the broken circuit and fail fast when the circuit is open.
Set appropriate thresholds
Set appropriate thresholds
Balance between sensitivity and stability:
- Too sensitive: circuit breaks on minor issues
- Not sensitive enough: failing system gets overwhelmed
Monitor circuit state
Monitor circuit state
Use
StateProvider to expose circuit state in health checks and monitoring dashboards. This provides visibility into system health.Use separate circuits per endpoint
Use separate circuits per endpoint
Don’t use a single circuit breaker for multiple endpoints. Isolate failures by creating separate circuits for each dependency.
Consider break duration carefully
Consider break duration carefully
Too short: may not give the system enough time to recover
Too long: increases user-perceived downtimeUse
BreakDurationGenerator for adaptive break durations that increase with repeated failures.Examples
HTTP Client with Circuit Breaker
Circuit Breaker with Retry and Fallback
Health Check Integration
What's the difference between Closed and Isolated states?
What's the difference between Closed and Isolated states?
- Closed: Normal operation. Circuit monitors for failures and can automatically transition to Open.
- Isolated: Manually held open. Circuit won’t automatically close; requires manual intervention via
CloseAsync().
Does the circuit breaker prevent exceptions from being thrown?
Does the circuit breaker prevent exceptions from being thrown?
No. During normal operation (Closed state), exceptions are thrown as normal. When Open or Isolated, it throws
BrokenCircuitException or IsolatedCircuitException instead.How do I implement a circuit breaker per user/tenant?
How do I implement a circuit breaker per user/tenant?
Use the
ResiliencePipelineProvider with keyed services or maintain a dictionary of circuit breakers per user/tenant identifier.Can I use one circuit breaker for multiple endpoints?
Can I use one circuit breaker for multiple endpoints?
It’s not recommended. Create separate circuit breakers for each dependency to isolate failures and avoid one failing service affecting others.