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

  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 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.