Video in progress
How do the circuit breaker's states work, and what problems come up if it's configured wrong?
In the closed state calls go through and failures are counted; if the failure rate goes above the threshold, the breaker opens and rejects calls immediately; after a set time, in half-open a few trial requests are let through, and depending on the result it closes again or opens.
📖 Answer
The goal is not to tie up threads waiting on a dead dependency, and to give it time to recover.
resilience4j:
circuitbreaker:
instances:
partner:
sliding-window-type: COUNT_BASED
sliding-window-size: 50
minimum-number-of-calls: 20
failure-rate-threshold: 50
slow-call-duration-threshold: 2s
slow-call-rate-threshold: 60
wait-duration-in-open-state: 30s
permitted-number-of-calls-in-half-open-state: 5
@CircuitBreaker(name = "partner", fallbackMethod = "cachedRate")
public Rate rate(String code) { return client.rate(code); }
Rate cachedRate(String code, Throwable t) { return rateCache.lastKnown(code); }
Typical configuration mistakes:
minimum-number-of-callsis too small. At night, 2 of 3 requests fail — 66%, and the breaker opens. With little traffic the statistics are meaningless.- Not counting slow calls. The dependency doesn't return errors, it just answers in 10 seconds. If you count only errors, the breaker never opens. The
slow-callsettings exist for this. - Counting client errors. A
404or a validation error doesn't mean the dependency is unhealthy — add them toignore-exceptions. - One breaker for everything. If one endpoint of the partner falls over, the other healthy endpoints get blocked too.
- A fallback that lies. If, when asked for a payment status, the fallback returns "failed", it leads to a wrong decision. Sometimes the right fallback is to state the error openly.
Expose breaker states as metrics (Micrometer does this automatically) and set an alert for when it's open — otherwise the system quietly runs in a "degraded" state for weeks.
Sign up to mark studied questions and save your progress.