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

  1. @CircuitBreaker(reset = "30s")
  2. public List<Book> findBooks() {
  3. // ...

Applying CircuitBreaker Advice

  1. @CircuitBreaker(reset = "30s")
  2. List<Book> findBooks() {
  3. // ...

Applying CircuitBreaker Advice

  1. @CircuitBreaker(reset = "30s")
  2. open fun findBooks(): List<Book> {
  3. // ...

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.