Implement Repeated API call until success with exponential wait time - Retry API Call

Try API call until success or upto n retry with exponential wait time

  Shubham Verma      February 25, 2022


Implement Repeated API call until success with exponential wait time - Retry API Call

Implement Repeated API call until success with exponential wait time - Retry API Call

Re-Try Sometimes, we need to make an API call until getting the success response or retry up to n times for some reason. Suppose we have a very important API call that needs to be a success and they are independent API call and the dependent API call takes some time to process operation-x, in between someone hit the API call to get the result of operation-x, but operation-x is not yet finished by 3rd API, at this time it will send a response with ERROR and after some time when operation-x will be finished then the calling API will be a success. To handle this situation we need to write some retry mechanisms.

Didn't understand?

Let me give you an example. Please have a look on below image.

Implement Repeated API call until success with exponential wait time - Retry API Call

Implement Repeated API call until success with exponential wait time - Retry API Call

In the above image, you can see, there are so many things, let me describe you.
There is a Front End and Back End. Front End raised an API call to Back End. Now Back End will have some logic to do retry an API call upto 5 times and with exponential wait time.

Exponential Wait Time:
It means the API call will wait 2 second and if get failed, then the next API call will wait 4 second, if get failed, then the next API call will wait for 8 seconds and so on.


So, the Back End have a method called retryRequest() and this retryRequest() will call a 3rd party API myAPI().
Now, retryRequest() call myAPI() 1st time and if this get failed, then retryRequest() will call myAPI() 2nd time after 2 seconds, and again the 3rd party API myAPI() will be failed.

Now, retryRequest() call myAPI() 3rd time after 4 seconds, and if this get failed, then retryRequest() will call myAPI() 4th time after 8 seconds and so on. But retry will happened upto 5 times.

If we get the success before or on 5th retry then the Back End will send the success response to the Front End.

And if the in the 5th retry the API myAPI() still getting failed then the Back End will send the response to Front End with error details.

Now it's time to Implement Repeated API call until success with exponential wait time.

Write the codes:

First thing first, Let's create a dummy callback function myAPI(), it will give errir 3 times and 4th time it will be success with data.

Implement 3rd party API/callback:

File: retry.js:

Run the above codes:

Now let's run the above codes using below command and see the output.

Output:

Implement Repeated API call until success with exponential wait time - Retry API Call

Implement Repeated API call until success with exponential wait time - Retry API Call

Describe possible error codes:


The above error codes will be used in the retry logic. If any of the above error codes will come in response as error then the retry function would be called. You can change this logic as per your requirements, You can add more logic or can update this logic as per response.

Check the retry error in depth:


In the above codes, we have defined all the possible error response structure that we can get from the 3rd party APIs. You can change this logic as per your requirements, You can add more logic or can update this logic as per response.

Implement Exponential Wait Time:


In the above codes, we have implemented Exponential Wait Time. We have also checked that what would be the maximum wait time that the "Repeated API call until success with exponential wait time" logic will wait. So, the wait time would be minimum from maxExponentialCount and exponentialCount. Here the maximum wait time is 32 seconds.

Implement Retry Logic:


In the above codes, we have implemented the "Repeated API call until success with exponential wait time". We have a retryFn() that will be call in loop untill the 3rd party get the success or the upto limit is ehausted. Also the retryFn() will wait for the given wait time to call the next round of API call.

How to call:


We can call the retryRequest() with given callback function. This given callback function will be called as 3rd party API and will be called again and again and will be repeated API call until success with exponential wait time.

Complete Codes:

File: retry.js:

Run the above codes:

Now let's run the above codes using below command and see the output.

Output:

Implement Repeated API call until success with exponential wait time - Retry API Call

Implement Repeated API call until success with exponential wait time - Retry API Call


Conclusion:

In this article, we have implemented the Repeated API call until success with exponential wait time means implemented Retry API Calls. We have written our logic with exponential wait time and upto n retry times. Also we have fixed the wait time upto a level. So, this would be a greate code to implement the repeated API call until success with exponential wait time.


Support our IDKBlogs team


Welcome to IDKBlogs.

If our contents help you and you learned from this platform, please consider supporting what we do.

Thank you.




Strongly Recommended Books For You:

Reading books opens the door to allow in more lights, If we want to know a man of rare intellect, try to know what books he/she reads.

For India:

For Other Countries (United States, United Kingdom, Spain, Italy, Germany, France, Canada, etc)


Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.



Funny Programming | Idkblogs.com | Shubham Verma

👈

Enjoyed this moment?