Video in progress

Strong Middle 🧩 Microservices

What exactly happens in the HALF_OPEN state of a circuit breaker?

After the configured time in OPEN, the breaker moves to HALF_OPEN and lets through a limited number of trial requests. If enough of them succeed it goes back to CLOSED, otherwise it goes to OPEN again.

📖 Answer

There are three main states: CLOSED (all requests pass, statistics are collected), OPEN (requests are rejected immediately), HALF_OPEN (checking whether the service has recovered).

In HALF_OPEN, Resilience4j lets through only permitted-number-of-calls-in-half-open-state requests. The rest get a CallNotPermittedException. The failure rate is calculated over these trial requests and compared with failure-rate-threshold.

resilience4j.circuitbreaker.instances.inventory:
  sliding-window-size: 50
  failure-rate-threshold: 50
  wait-duration-in-open-state: 30s
  permitted-number-of-calls-in-half-open-state: 5
  automatic-transition-from-open-to-half-open-enabled: true

One subtle point: if automatic-transition... is off (the default), the OPEN → HALF_OPEN transition happens only when the next request arrives. With low traffic, the breaker looks like it stays open longer than expected.

Another prod situation: if 5 trial requests are too few, one random error reopens the breaker and it keeps "flapping". In that case, raising the number of half-open calls or increasing wait-duration helps. Watch state changes through /actuator/circuitbreakerevents in Actuator and through metrics.