Resilience4j | Retry basics & runtime behavior | Simple example for beginners

In this article we will look at very simple basic example of Resilience4j retry feature & look at runtime behavior of retry. Here is the maven dependency for resilience4j-retry required for this example.

Retry Concept

Sometimes there might be intermittent failures which are might not be long running issue but might be a glitch or temporary problems. In such cases, call can just be made again & mostly it will end up in success.

Example in this article

Retry Configurations: Retry configuration which will try service call 2 times in total. Retry will be done if result is “FAILURE” or if BadProcessingException is thrown.

  • Create mock external service which intermittently fails or throws exception (randomly for test purpose)
  • Make 20 calls to the service so that few might end up in failure or exceptions.
  • We will observe how retry behaves for all threads.



Mock Service

Here is a mock service that will randomly return SUCCESS or FAILURE or throw BadProcessingException.



Retry in action



Output

Key behavior to look for in output

  • Success in first attempt (Call count = 2):
    • Call succeeds in first attempt, then simply result will be returned..
  • Exception first & retry gets success (Call count = 1):
    • Call ends up with BadProcessingException. Retry attempt is made & result is success.
  • FAILURE first & retry gets success (Call count = 9):
    • Call returns FAILURE status. Retry attempt is made & result is success.
  • FAILURE first & retry also gets FAILURE (Call count = 10):
    • Call returns FAILURE status. Retry attempt is made & result is again FAILURE, so this is considered as final status.





Further reading

Resilience4j Complete Tutorial | Basics with runtime behavior | Simple examples for beginners

Leave a Reply

Your email address will not be published.