Implementing Exponential Back-Off
Exponential back-off is the process of a client periodically retrying a failed request over an increasing amount of time. It is a standard error handling strategy for network applications. Besides being “required”, using exponential back-off increases the efficiency of bandwidth usage, reduces the number of requests required to get a successful response and maximizes the throughput of requests in concurrent environments.
The flow of implementing a simple exponential back-off is as follows:
Make a request to the API
Receive the response, check for error that has a retry-able error code (such as 503)
Wait
1s + random_number_milliseconds
secondsRetry request
Receive the response, check for error that has a retry-able error code (such as 503)
Wait
2s + random_number_milliseconds
secondsRetry request
Receive the response, check for error that has a retry-able error code (such as 503)
Wait
4s + random_number_milliseconds
secondsRetry request
Receive the response, check for error that has a retry-able error code (such as 503)
Wait
8s + random_number_milliseconds
secondsRetry request
Receive the response, check for error that has a retry-able error code (such as 503)
Wait
16s + random_number_milliseconds
secondsRetry request
If you still get an error, stop and log the error
In the above flow, random_number_milliseconds
is a random number of milliseconds less than or equal to 1000. This is necessary to avoid certain lock errors in some concurrent implementations.
The algorithm is set to terminate when n == 5. This ceiling is in place to prevent clients from retrying infinitely, and results in a total delay of 32 seconds before a deemed “unrecoverable error.”
Last updated
Was this helpful?