How to implement retry design pattern in Axios?

In the microservices world a lot of microservices often communicate with each other through API calls.

These calls can often break due to network issues and other problems.

It makes sense to retry these calls when these issues happen so that the likelihood of getting a response increases.

For node js applications this can be achieved through axios-retry library.

Install both axios and axios-retry libraries through npm in your app.

Then use a code similar to the sample below:

const express = require('express');
const cors = require('cors');
const axios = require('axios');
const axiosRetry = require('axios-retry');

const app = express();


app.use(cors());



axiosRetry(axios, {
  retries: 3,
  retryDelay: (retryCount) => {
    console.log(`Retrying request, attempt number ${retryCount}`);
    return retryCount * 1000; // wait 1s, 2s, 3s between retries
  }
});




app.get('/posts',async (req, res) => {
  

  console.log("Hitting posts api");
    try {


         const response = await axios.get('https://api.example.com/endpoint');
        console.log(response.data);
    
      } catch (error) {
        console.error("Error while hitting posts api");
    
      }

});


app.listen(3000, () => {
    console.log('Server listening on port 3000');
  });
  
Advertisements

In the above code the below block of code provides the configuration for the retry mechanism:


axiosRetry(axios, {
  retries: 3,
  retryDelay: (retryCount) => {
    console.log(`Retrying request, attempt number ${retryCount}`);
    return retryCount * 1000; // wait 1s, 2s, 3s between retries
  }
});

In this example, the retry option is set to 3, which means that the request will be retried up to 3 times if it fails. The retryDelay option is a function that returns the delay between retries in milliseconds. In this example, the delay is increased by 1 second each time.

Here is the output:

After 3 retries the app stops retrying and throws error .

Note that the retry option is an add-on for Axios, and it must be installed separately as earlier mentioned.





Retry option can often be confused with circuit breaker design pattern . Both these are different design patterns and can be often used together.

Here are the differences between the two:

The circuit breaker pattern and the retry pattern are two separate strategies for handling failures in a system.

The circuit breaker pattern is a design pattern that is used to prevent a system from repeatedly trying to perform an operation that is likely to fail. It does this by “tripping” a circuit breaker after a certain number of failures, causing the system to stop trying the operation for a certain period of time. This helps to prevent the system from becoming overloaded and unresponsive.

The retry pattern is a strategy for handling failures by automatically retrying an operation if it fails. The retry pattern can be used in conjunction with the circuit breaker pattern to allow the system to retry an operation after the circuit breaker has reset, or it can be used on its own to handle intermittent failures.

In summary, the circuit breaker pattern is used to prevent an operation from being tried too many times, while the retry pattern is used to automatically try an operation again if it fails.

That’s it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s