For complete understanding of Resilience4j Retry module and how we can use it inside the Spring Boot application you can checkout our blog.
Blog Link: Resilience4j Retry: Building Fault-Tolerant Spring Boot Applications
A simple app highlighting how we can implement retry mechanism using Resilience4j in Spring Boot
This is a simple app wherein we are fetching the movie details based on the movie id. The movie details are fetched from external service that is called using the Spring Rest Template. For simplicity, we have created a mock controller which acts as a external service for returning the movie details.
We have created a single controller endpoint which accepts movie id as path parameter and query parameter retryType which accepts predefined set of values to mimic the different retry examples.
a. 1 or 2 - Mock controller returns valid movie information
b. 3 - Mock controller returns HTTP status code 404
c. 4 or any other numeric value - Mock controller returns null which leads to MovieNotFound Exception
Different retry instances are defined inside the application.yml. To mimic different retry scenarios use:
a. simple-retry: simpleRetry retry instance will be triggered
b. retry-on-exception: retryOnException retry instance will be triggered.
c. retry-on-exception-predicate: retryBasedOnExceptionPredicate retry instance will be triggered.
d. retry-on-conditional-predicate: retryBasedOnConditionalPredicate retry instance will be triggered.
e. retry-using-exponential-backoff: retryUsingExponentialBackoff retry instance will be triggered.
f. retry-using-randomized-wait: retryUsingRandomizedWait retry instance will be triggered.
g. retry-with-fallback: simpleRetry retry instance will be triggered and fallback method logic will be executed in this case.
h. retry-with-custom-config: customRetryConfig retry instance defined in RetryConfiguration class will be triggered.
i. retry-with-event-details: retryWithEventDetails retry instance will be triggered.
Check the application logs in order to get the better understanding of different retry scenarios.
curl 'http://localhost:8080/movies/3?retryType=simple-retry'
curl 'http://localhost:8080/movies/3?retryType=retry-on-exception'
curl 'http://localhost:8080/movies/4?retryType=retry-on-exception-predicate'
curl 'http://localhost:8080/movies/4?retryType=retry-on-conditional-predicate'
curl 'http://localhost:8080/movies/3?retryType=retry-using-exponential-backoff'
curl 'http://localhost:8080/movies/3?retryType=retry-using-randomized-wait'
curl 'http://localhost:8080/movies/4?retryType=retry-with-fallback'
curl 'http://localhost:8080/movies/3?retryType=retry-with-custom-config'
curl 'http://localhost:8080/movies/3?retryType=retry-with-event-details'