Implementing Circuit Breaker Patterns

1. Circuit Breaker Pattern

AspectDetail
PurposePrevent cascading failure by failing fast when downstream is unhealthy
WrapEach remote call
ToolsResilience4j, Hystrix (legacy), Polly (.NET), Istio outlier detection

2. Circuit Breaker States

StateBehaviorTransition
CLOSEDCalls pass; tracks failure rateFailures > threshold → OPEN
OPENCalls fail fast (no remote attempt)After cool-down → HALF-OPEN
HALF-OPENPermits N probe callsSuccess → CLOSED; Failure → OPEN

State Machine

CLOSED ──failures>threshold──> OPEN ──cool-down──> HALF-OPEN
   ^                                                   │
   └──────success in HALF-OPEN─────────────────────────┘
                                          (failure → OPEN)
        

3. Circuit Breaker Fallback Pattern

Fallback StrategyExample
Cached Last-KnownReturn last successful response
Default ValueEmpty list, zero, "not available"
Alternative ServiceFailover to secondary provider
Degraded ModeSkip non-critical feature
Fail Fast ErrorHTTP 503 with Retry-After

4. Circuit Breaker Threshold Configuration

ParamTypicalEffect
failureRateThreshold50%% of failures to trip
slowCallRateThreshold100%% slow calls counted as failure
slowCallDurationThreshold2sAbove this = "slow"
slidingWindowSize100 callsSample size for stats
minimumNumberOfCalls20Min calls before stats considered
waitDurationInOpenState30sCool-down before HALF-OPEN
permittedCallsInHalfOpen5Probe count

Example: Resilience4j Config

resilience4j.circuitbreaker:
  instances:
    paymentService:
      failureRateThreshold: 50
      slowCallDurationThreshold: 2s
      slidingWindowSize: 100
      minimumNumberOfCalls: 20
      waitDurationInOpenState: 30s
      permittedNumberOfCallsInHalfOpenState: 5

5. Circuit Breaker with Timeout

PatternDetail
CombineTimeout + circuit breaker (slow calls trip too)
OrderTimeout → fail call → counted by breaker
TuningTimeout shorter than upstream timeout in chain

6. Hierarchical Circuit Breaker Pattern

LevelScope
Per EndpointTrip only failing endpoint
Per ServiceAggregate across endpoints
Per Cluster/RegionFailover at regional level

7. Circuit Breaker Metrics Pattern

MetricUse
state (CLOSED/OPEN/HALF_OPEN)Dashboards / alerts on OPEN
failureRateTrend analysis
slowCallRateLatency degradation
trip countOutage frequency
callsNotPermittedRequests rejected while open

8. Circuit Breaker Recovery Pattern

StepAction
Cool DownOPEN for fixed duration
ProbeHALF-OPEN allows limited test calls
Gradual Re-OpenLinear/exponential ramp on success
Manual ResetOperator override via mgmt endpoint

9. Per-Host Circuit Breaker Pattern

AspectDetail
DefinitionIndependent breaker per upstream instance/IP
BenefitIsolates one bad replica without tripping whole service
ImplementationEnvoy outlier detection, Istio

10. Adaptive Circuit Breaker Pattern

AspectDetail
AdaptsThresholds change based on observed traffic patterns
ExamplesSREcon adaptive concurrency, Netflix concurrency-limits
AlgorithmTCP Vegas-like, Little's Law
BenefitNo static tuning; reacts to real load