Circuit Breaker
In a Microservice environment retry is useful, but in some cases excessive retries can overwhelm the system as clients repeatedly re-attempt failing operations.
The Circuit Breaker pattern is designed to resolve this issue by essentially allowing a certain number of failing requests and then opening a circuit that remains open for a period before allowing any additional retry attempts.
The CircuitBreaker annotation is a variation of the @Retryable
annotation that supports a reset
member that indicates how long the circuit should remain open before it is reset (the default is 20 seconds).
Applying CircuitBreaker Advice
@CircuitBreaker(reset = "30s")
public List<Book> findBooks() {
// ...
Applying CircuitBreaker Advice
@CircuitBreaker(reset = "30s")
List<Book> findBooks() {
// ...
Applying CircuitBreaker Advice
@CircuitBreaker(reset = "30s")
open fun findBooks(): List<Book> {
// ...
The above example will retry to findBooks
method 3 times and then open the circuit for 30 seconds, rethrowing the original exception and preventing potential downstream traffic such as HTTP requests and I/O operations flooding the system.