Video in progress

How do the closed, open and half-open states of a circuit breaker switch?

In closed, calls go through and errors are counted. If the error rate goes above the threshold, it goes to open: calls are rejected immediately. After a set time it goes to half-open: a few trial calls are let through, and if they succeed it goes back to closed, otherwise back to open.

📖 Answer

          errors > threshold
 CLOSED ---------------------> OPEN
   ^                             |
   |  trial calls succeed        | wait time is over
   |                             v
   +------------------------ HALF_OPEN
          trial calls fail -> OPEN again

The main settings that control these states in Resilience4j:

resilience4j:
  circuitbreaker:
    instances:
      payment:
        sliding-window-size: 20
        minimum-number-of-calls: 10
        failure-rate-threshold: 50
        wait-duration-in-open-state: 30s
        permitted-number-of-calls-in-half-open-state: 3

Match minimum-number-of-calls to your traffic (the default is 100): if you set it too small, then at night when one or two requests fail it counts as "100% errors" and the breaker opens; if it's too large, the breaker never opens on a low-traffic service.

In practice it's useful to watch the breaker state in metrics: Actuator exposes the resilience4j.circuitbreaker.state metric. If the graph shows the breaker opening and closing all the time, either these thresholds are wrong or the service really is unstable.