How to Fail Fast on Recurring Failures

If an operation fails too often, it is possible to prevent executing that operation and fail fast using a circuit breaker.

The circuit breaker starts closed, in which state it allows all invocations to proceed. If too many invocations fail, the circuit breaker moves to open, in which state it prevents all invocations and fails fast. After some time in open, the circuit breaker moves to half-open, in which state it determines whether it should continue failing fast (and move back to open) or allow invocations again (and move to closed).

@CircuitBreaker

The @CircuitBreaker annotation specifies that method invocations should be prevented if the method failed too often recently.

@ApplicationScoped
public class MyService {
    @CircuitBreaker (1)
    public String hello() {
        ...
    }
}
1 Declares that hello() must be guarded with a circuit breaker.

If some callers attempt to invoke the method when the circuit breaker is open, they will fail with CircuitBreakerOpenException.

It is possible to configure the number of recent invocations the circuit breaker should track, the failure rate, and so on.

For more information, see the Circuit Breaker reference guide.