top of page

Implementing Retry with Resilience4J

What is Resilience4J

Resilience4j is a lightweight, easy-to-use fault tolerance library inspired by Netflix Hystrix. Resilience4j provides higher-order functions (decorators) to enhance any functional interface, lambda expression or method reference with a Circuit Breaker, Rate Limiter, Retry or Bulkhead. You can stack more than one decorator on any functional interface, lambda expression, or method reference. The advantage is that you have the choice to select the decorators you need and nothing else. With Resilience4j you don’t have to go all-in, you can pick what you need.

​Modules

​Purpose

​Retry

Automatically retry a failed operation​

RateLimiter​

Limit how many times we call a remote operation in a certain period

TimeLimiter​

Set a time limit for operation

Circuit Breaker

Fail fast when the operation is continuously failing

Bulkhead

Limit the number of concurrent operations

Cache

Caches the result of the operation

Retry

Enable an application to handle transient failures when it tries to connect to a service or network resource, by transparently retrying a failed operation. This can improve the stability of the application. For example, a database service that's processing a large number of concurrent requests can implement a throttling strategy that temporarily rejects any further requests until its workload has eased. An application trying to access the database might fail to connect, but if it tries again after a delay it might succeed.


How to use Resilience4J Retry

Add the following dependency:

Using default retry configuration:

By default, it will invoke the decorated function 3 times, retrying all the exceptions.


You can change this behavior by providing your custom configuration:

All the available configuration properties you can find here: https://resilience4j.readme.io/docs/retry


Integration with Spring Boot

Add the following spring boot starter:

Provide the configuration in application.yml

And finally, add the @Retry annotation to your retryable function:

As you can see it is also possible to specify a fallback method that will be triggered when all attempts have failed.


Conclusion

In this article, we learned what Resilience4j is and how we can use its retry module to make our applications resilient to temporary errors.

2,264 views
bottom of page