Retry Pattern

Problem

When service or system is trying to access a network resource like database which is not accessible at that moment but after few milliseconds it becomes available then how to deal with such situation where success can be expected after some time?

Solution

When a network resource is temporary not available or temporary failing like connection to the database is failing temporary but can be successfully connected if retries after some time then retry pattern can be used. In simple meaning, retry pattern can be implemented when success is expected.

In Retry Pattern, retry a task/process for a defined number of times and after each retry, wait for a specified time. If that process/task still fails after attempting defined numbers of retry (count of retry) then throw an exception and don’t block the thread.

Steps:

  • Define Max Retry count
  • Define variable to check current retry count
  • Define another variable to mention delay time which will be used when an operation failed.
  • Retry a task/process until currentRetry reaches to the max retry count. If operation fails then increment currentRetry counter and delay the operation for a specified time.
  • If process/tasks still fails after all retries then throw an exception and don’t block the thread.
Please share this

Leave a Reply