Whether you’re making API calls from Node.js or in the browser, connection failures are going to happen eventually. Some request errors are valid. Maybe the endpoint was wrong or the client sent the wrong data. Other times you can be sure that the error is the result of a problem with the connection to the server or one of the many hops in-between. While API and web service monitoring can inform you about the problem, a more active solution can take care of it for you.
To fix this, you can improve your HTTP request library by adding intelligent retry functionality. This kind of remediation is crucial to ensuring your API calls are successful. Some libraries, like got support retrying failed requests out of the box, while others like axios require a separate plugin. If your preferred library doesn’t support them, this article is for you. We’ll explore adding status-code-specific retries to a request, making them incrementally longer with a technique called “back-off”, and more.
To decide when to retry a request, we need to consider what to look for. There are a handful of HTTP status codes that you can check against. This will let your retry logic differentiate between a failed request that is appropriate to retry—like a gateway error—and one that isn’t—like a 404. For our examples, we will use 408, 500, 502, 503, 504, 522, and 524. You could also check for 429, as long as you incorporate the Retry-After header into the back-off logic.
The next consideration we want is how often to retry. We will start with a delay, then increase it each additional time. This is a concept known as “back-off”. The time between requests will grow with each attempt. Finally, we’ll also need to decide how many attempts to make before giving up.
Here’s an example of the logic we’ll be using in pseudo-code:
We could also check for things like error codes (from Node.js), and limit retrying to certain methods. For example, ignoring POST is often a good idea, to ensure no duplicate entries are created.
#api #retry-mechanism #nodejs #programming #javascript #fetch #node-fetch