0

When we have a piece of code that often fails and must be retried. Then we use retry pattern.

try {
        //do request here
} catch (Exception e) {
        //wait for some millisecond and retry
        retry();
}

We normally delay for some millisecond before retry that request. I want to know that why we need some delay? What will happen if retry request did not wait and send request again.

Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190
  • 4
    What can happen is that you hammer the server with requests and potentially make the problem worse – JonK Dec 01 '16 at 10:10
  • 3
    It is unlikely that a problem with the server is fixed in a few microseconds so an immediate retry is very unlikely to work and as JonK said would only contribute to problem. – Henry Dec 01 '16 at 10:15
  • So that we don't make the original problem even worse. – user207421 Dec 01 '16 at 10:25
  • Ooo I see. Many thanks for adding this concept in my knowledge. – Muhammad Imran Tariq Dec 01 '16 at 10:29
  • Before you hand-build lots of stuff like this, also check out libraries that have many ready-made primitives for retries and other resilience, eg https://github.com/jhalterman/failsafe [no personal involvement with this] – mountain traveller Dec 01 '16 at 12:41
  • Possible duplicate of [What is the benefit of using exponential backoff?](http://stackoverflow.com/questions/28732327/what-is-the-benefit-of-using-exponential-backoff) – Paul Sweatte Feb 21 '17 at 13:19

1 Answers1

2

It is unlikely that a problem with the server is fixed in a few microseconds so an immediate retry is very unlikely to work and would only contribute to problem. So a delay between retry is best practice to follow.

Muhammad Imran Tariq
  • 22,654
  • 47
  • 125
  • 190