Normal operation — every call is forwarded to the downstream service. The circuit breaker tracks outcomes in a sliding window. When the failure rate exceeds the configured threshold, it trips to OPEN.
OPEN
The breaker has tripped. All calls are immediately short-circuited with a CallNotPermittedException — no call reaches the service. This gives the downstream system time to recover. After the wait duration, the breaker moves to HALF-OPEN.
HALF-OPEN
A probe state. A limited number of test calls are permitted through. If they succeed (failure rate below threshold), the breaker resets to CLOSED. If any fail, it goes back to OPEN and the wait restarts.
Sliding Window
Resilience4j uses a count-based or time-based sliding window to measure failure rate. This demo uses count-based: the last N calls are evaluated. Slow calls (calls that exceed a duration threshold) are counted as failures when the slow call rate threshold is also exceeded.
In Spring Boot
Configure via application.yml under resilience4j.circuitbreaker.instances. Use @CircuitBreaker(name="myService") on any @Service method. Wire Micrometer metrics via the Actuator dependency to expose state transitions to Prometheus.