Circuit Breaker State
CLOSED
Accepting requests
Failure Rate Threshold: 50%
Call History (last 50)
Event Log
Availability Metrics
Availability
100%
0 downtime periods
Implementation Code
Resilience4j (Java)
CircuitBreakerConfig config = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.slowCallRateThreshold(50)
.slowCallDurationThreshold(Duration.ofMillis(1000))
.waitDurationInOpenState(Duration.ofSeconds(30))
.minimumNumberOfCalls(10)
.permittedNumberOfCallsInHalfOpenState(3)
.build();
CircuitBreaker breaker = CircuitBreaker.of("myBreaker", config);
breaker.executeSupplier(() -> callDownstream());
Polly (.NET)
var policy = Policy.Handle()
.OrResult(r => !r.IsSuccessStatusCode)
.CircuitBreaker(5, TimeSpan.FromSeconds(30),
onBreak: (outcome, ts) => Console.WriteLine("Circuit opened"),
onReset: () => Console.WriteLine("Circuit closed"));
var response = await policy.ExecuteAsync(() =>
httpClient.GetAsync("https://api.example.com"));
opossum (Node.js)
const breaker = new CircuitBreaker(callFunction, {
timeout: 1000,
errorThresholdPercentage: 50,
resetTimeout: 30000,
rollingCountTimeout: 10000,
rollingCountBuckets: 10,
name: 'myService'
});
await breaker.fire(args).catch(err => {
if (err.breaker) console.log('Circuit open');
});
Hystrix Pattern (Overview)
public class MyService {
@HystrixCommand(
fallbackMethod = "fallback",
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
value = "1000"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",
value = "50"),
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",
value = "30000")
}
)
public String callDownstream() {
return restTemplate.getForObject("...", String.class);
}
public String fallback() {
return "Service unavailable";
}
}