Circuit Breaker And Retry with Spring Cloud Resiliance4j

Introduction

Integrating Retry

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
@org.springframework.stereotype.Service
public class Service {

@Value("${service2.url:http://localhost:6060/service2}")
String serviceUrl;

@Retry(name = "myRetry", fallbackMethod = "fallback")
public String fetchData() {
System.out.println(" Making a request to " + serviceUrl + " at :" + LocalDateTime.now());

RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject(serviceUrl, String.class);
}

public String fallback(Exception e) {
return "fallback value";
}
}
@RestController
public class Controller {

@Autowired
private Service service;

@GetMapping("/")
public String getValues() {
return service.fetchData();
}
}
resilience4j:
retry:
instances:
myRetry:
max-attempts: 3
wait-duration: 5s

Exponential Retries

resilience4j:
retry:
instances:
myRetry:
max-attempts: 3
wait-duration: 5s
enable-exponential-backoff: true
exponential-backoff-multiplier: 2
wait-duration * (exponential-backoff-multiplier ^ (retry iteration count))
1st Retry  - 5s * 2^0 = after 5 seconds
2nd Retry - 5s * 2^1 = after 10 seconds
3rd Retry - 5s * 2^2 = after 20 seconds
..
..
nTh Retry - 5s * 2^n

Conditional Retry on Exceptions

resilience4j:
retry:
instances:
myRetry:
max-attempts: 5
wait-duration: 5s
enable-exponential-backoff: true
exponential-backoff-multiplier: 2
retry-exceptions:
- org.springframework.web.client.RestClientException
ignore-exceptions:
- com.amrut.prabhu.IgnoreException

Integrating a Circuit Breaker

@CircuitBreaker(name = "myCircuitBreaker", fallbackMethod = "fallback")@Retry(name = "myRetry")
public String fetchData() {
System.out.println(" Making a request to " + serviceUrl + " at :" + LocalDateTime.now());

RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject(serviceUrl, String.class);
}
resilience4j:
retry:
instances:
myRetry:
max-attempts: 3
wait-duration: 5s
enable-exponential-backoff: true
exponential-backoff-multiplier: 2
retry-exceptions:
- org.springframework.web.client.RestClientException
ignore-exceptions:
- com.amrut.prabhu.IgnoreException
circuitbreaker:
instances:
myCircuitBreaker:
wait-duration-in-open-state: 1m
permitted-number-of-calls-in-half-open-state: 3
sliding-window-type: count-based
sliding-window-size: 5
minimum-number-of-calls: 5
slow-call-duration-threshold: 10s
slow-call-rate-threshold: 60
failure-rate-threshold: 60

Understanding the Circuit Breaker Config

Circuit Breaker and Retry Config Together

Circuit Breaker and Retry Aspects Order

 Retry  ( Circuit Breaker ( function ) ) 
resilience4j:
retry:
retry-aspect-order: 2
instances:
myRetry:
max-attempts: 3
wait-duration: 5s
enable-exponential-backoff: true
exponential-backoff-multiplier: 2
retry-exceptions:
- org.springframework.web.client.RestClientException
ignore-exceptions:
- com.amrut.prabhu.IgnoreException
circuitbreaker:
circuit-breaker-aspect-order: 1
instances:
myCircuitBreaker:
wait-duration-in-open-state: 1m
permitted-number-of-calls-in-half-open-state: 3
sliding-window-type: count-based
sliding-window-size: 5
minimum-number-of-calls: 5
slow-call-duration-threshold: 10s
slow-call-rate-threshold: 60
failure-rate-threshold: 60

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amrut Prabhu

Amrut Prabhu

Software Craftsman, Tech Enthusiast. I run https://refactorfirst.com to post all my articles