In Resilience4j, the CircuitBreaker class protects a call to an unreliable dependency. It records the outcome of recent calls, and once the failure rate or slow-call rate crosses a threshold it opens and rejects further calls immediately, so a failing service is not hammered and your threads are not tied up waiting on it.
After a wait period it lets a few trial calls through (half-open) and closes again if they succeed. This prevents cascading failures in microservice systems.
A CircuitBreaker is a state machine with three normal states and three special ones.
| State | Behaviour |
|---|---|
CLOSED | Calls pass through and outcomes are recorded in a sliding window |
OPEN | Calls are rejected with CallNotPermittedException |
HALF_OPEN | A limited number of trial calls are allowed to test recovery |
DISABLED, FORCED_OPEN, METRICS_ONLY | Special states for testing and operations; no automatic transitions |
Transitions:
CLOSED to OPEN when the failure rate or slow-call rate reaches its threshold, once the minimum number of calls has been recorded.OPEN to HALF_OPEN after waitDurationInOpenState elapses (automatically only if automaticTransitionFromOpenToHalfOpenEnabled is true, otherwise on the next call attempt).HALF_OPEN to CLOSED if the trial calls stay under the thresholds, or back to OPEN if they do not.| Property | Default | Meaning |
|---|---|---|
failureRateThreshold | 50 | Percentage of failed calls that opens the circuit |
slowCallDurationThreshold | 60 s | A call slower than this counts as slow |
slowCallRateThreshold | 100 | Percentage of slow calls that opens the circuit |
slidingWindowType | COUNT_BASED | Count the last N calls, or calls in the last N seconds (TIME_BASED) |
slidingWindowSize | 100 | Size of the window |
minimumNumberOfCalls | 100 | Calls needed before the rate is evaluated |
waitDurationInOpenState | 60 s | How long the circuit stays open |
permittedNumberOfCallsInHalfOpenState | 10 | Trial calls in the half-open state |
recordExceptions, ignoreExceptions | none | Which exceptions count as failures |
The defaults are conservative for low-traffic services: with minimumNumberOfCalls at 100, a service receiving ten calls a minute takes a long time to trip. Tune these values to your real traffic.
Resilience4j 2.x requires Java 17 or newer.
decorateSupplier wraps the call so its result or exception is recorded. When the circuit is open, get() throws CallNotPermittedException without calling the remote service, and you return a fallback. executeSupplier is a shortcut for decorate-and-call.
With Spring Boot 3, add io.github.resilience4j:resilience4j-spring-boot3 and Spring AOP (spring-boot-starter-aop). Configure an instance in application.yml:
Then annotate the method:
The fallback must have the same return type and the original parameters, optionally followed by an exception parameter. As with other Spring AOP annotations, calls from within the same class bypass the proxy. Spring Cloud CircuitBreaker offers a vendor-neutral abstraction with a Resilience4j implementation if you prefer it.
Retry, TimeLimiter and Bulkhead. Order matters; a circuit breaker usually wraps the retry so that retried failures are counted.ignoreExceptions or recordExceptions.Hystrix has been in maintenance mode for years. Resilience4j, often via Spring Cloud CircuitBreaker, is the common replacement.
Usually minimumNumberOfCalls has not been reached, or the exceptions thrown are in ignoreExceptions. Check the window size and counts.
The circuit is open (or half-open with no permitted calls left) and the call was rejected without executing.
Each named CircuitBreaker keeps its own state in one JVM. Use one per downstream dependency, not one shared across unrelated services.
The CircuitBreaker class in Resilience4j monitors call outcomes in a sliding window, opens when failure or slow-call rates cross a threshold, and probes for recovery in the half-open state. Tune the thresholds to your traffic, provide a fallback, and monitor state transitions.