Circuit Breaker
Retry is useful in a microservice environment, 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 allowing a certain number of failing requests and then opening a circuit that remains open for a period before allowing additional retry attempts.
The CircuitBreaker annotation is a variation of the @Retryable
annotation that supports a reset
member which 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 retries the findBooks
method three times and then opens 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.