5XX Retry Logic Best Practices
Review best practices to deal with 5XX errors

Introduction

Since DataHub is a proxy that passes through errors from full node software, our users will need to adopt defensive strategies for interacting with the API.
We are making it easy for our users to do the right thing and providing examples of pseudo-code to copy/paste.
For Example:
1
tries = 3
2
begin
3
r = RestClient.get(...)
4
rescue
5
tries -= 1
6
retry if tries > 0
7
end
Copied!

The basics of a retry

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. In our examples, we will use 408, 500, 502, 503, 504, 522, and 524.
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:
  • If total attempts > attempts, continue
  • if status code type matches, continue
  • if (now - delay) > last attempt, try request
  • else, return to the start
Let's explore the best practices for NodeJs, Pyhton, Ruby, and Go: