Advanced retry policy

Hi … we have a requirement of retry : retry every 10 sec for the first min. Then retry every 1 min for the first hour, then retry every hour up to 6 hours. ( retionale is that a condition will likely be ready within 1min, but 99% percentile is under 1 hours. The long tail will be upto 6 hours)

I do not think retrypolicy can implement this requirement. It seems the only option is to implement it as a part of workflow logic. Something like this:

class workflowImpl{
public void flow() {
TimeStamp startTime= new Timestamp(System.currentTimeMillis());
bool isReady=checkCondition();

             while( !isReady) {
                   if ( isWithinFirstMin(startTime)) {
                        thread.sleep(10 sec); //first min retry 
                   }else if (isWithinFirstHour(startTime)) {
                        thread.sleep (1 min); //first hour retry
                   }else if (isWithinSixHour(startTime)) {
                        thread.sleep( 1 hour);  // 6 hour retry
                   }else {
                        break; // timeout; 
                   }   

                   isReady= checkCondition(); 
           } //end while
           if (isReady) {
               //continue my flow;
           } else {
               // timeout flow.
           }
  }

}

Welcome any suggestion.

Thanks,

The existing retry policy is exponential. I think you can tweak its parameters to achieve what you want without discrete steps as in your example. For example with initial interval of 1 second and exponential coefficient of 1.4 you are going to have the following intervals (used this calculator):

Retry	Seconds	Timestamp
1	1.00	2021-02-28 17:42:41
2	2.40	2021-02-28 17:42:42
3	4.36	2021-02-28 17:42:44
4	7.10	2021-02-28 17:42:47
5	10.95	2021-02-28 17:42:51
6	16.32	2021-02-28 17:42:56
7	23.85	2021-02-28 17:43:04
8	34.39	2021-02-28 17:43:14
9	49.15	2021-02-28 17:43:29
10	69.81	2021-02-28 17:43:50
11	98.74	2021-02-28 17:44:19
12	139.23	2021-02-28 17:44:59
13	195.93	2021-02-28 17:45:56
14	275.30	2021-02-28 17:47:15
15	386.42	2021-02-28 17:49:06
16	541.99	2021-02-28 17:51:42
17	759.78	2021-02-28 17:55:20
18	1,064.70	2021-02-28 18:00:25
19	1,491.58	2021-02-28 18:07:32
20	2,089.21	2021-02-28 18:17:29
21	2,925.89	2021-02-28 18:31:26
22	4,097.24	2021-02-28 18:50:57

You can limit the maximum retry interval to one hour. So after the 21st attempt, all retries will be 60 minutes long. You can tweak the initial interval, exponential coefficient and the maximum retry interval to match your requirements.

1 Like

good implementation although not exact to the requirements. it makes the flow clean.

Thanks