Retries That Cannot Run Away: Backoff, Jitter and Where They Live
A rate-limited endpoint turns away ten thousand mobile clients in the same second. Each one waits a second and tries again, then two seconds, then four. The server now meets ten thousand simultaneous requests at every one of those moments. Retries are supposed to give a struggling dependency room to recover. Written carelessly, they become the load that keeps it down. Waiting is not the same as…
In the event of an endpoint turning away ten thousand mobile clients in a single second, each client must wait a second before retrying, then two seconds, and so on. This creates a scenario where the server is handling ten thousand simultaneous requests at every one of those moments. The intention behind retries is to provide a struggling dependency with enough time to recover, but if executed carelessly, they can exacerbate the issue.
Exponential backoff is a technique that lengthens the gap between attempts, which can help, but it doesn't address the issue of timing. If multiple callers begin failing at the same instant, they all compute the same schedule and remain in step until the outage concludes. The solution to this problem lies in randomness. By having each caller pick its wait time from a range rather than a fixed number, the group thins out into a stream.
This concept is referred to as the "thundering herd" hazard, and it is effectively addressed by combining backoff with jitter. A backoff function that incorporates randomness can be written to ensure that the final wait time never exceeds the predetermined cap. By placing the randomness within the bound, the problem is avoided from the outset.
The provided code demonstrates an example of such a backoff function in JavaScript, along with an async retrying function that implements the backoff strategy. It is crucial to keep error classification pure in the Utils layer and the sleeping mechanism in the Resources layer. Retrying should only occur at one layer, ideally the lowest layer that can determine if an error is transient.
Additionally, it is essential to retry only what is safe to repeat, such as reads or writes protected by an idempotency key, while avoiding retrying unsafe operations like a bare "create order" call.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to
More from Dev.to on urgent.news
Report a problem with this story
Get the day's news by email. Daily, weekly or monthly. Sign up