Computing Exponential Backoff
Concepts covered: Retry & Failure Handling
Exponential backoff is a formula, and you should be able to run it in your head before you trust a library to run it for you. After the n-th consecutive failure, the wait before the next attempt is wait = min(cap, base * 2 ** (n - 1)). base is the wait after the first failure. The exponent doubles it with each further failure. cap is the ceiling the wait can never exceed. With the animation's settings, base is 1 second and cap is 60 seconds.
Libraries differ on whether the first failure is numbered 0 or 1. Check which before comparing numbers; the shape of the curve is the same either way.
| Failure number | base * 2^(n-1) | After the 60 s cap | Cumulative sleep |
|---|---|---|---|
| 1 | 1 s | 1 s | 1 s |
| 2 | 2 s | 2 s | 3 s |
| 3 | 4 s | 4 s | 7 s |
| 4 | 8 s | 8 s | 15 s |
| 5 | 16 s | 16 s | 31 s |
| 6 | 32 s | 32 s | 63 s |
| 7 | 64 s | 60 s | 123 s |
| 8 | 128 s | 60 s | No sleep: attempt 8 was the last of the budget |
Doubling beats adding a second each time because a linear ramp of 1, 2, 3, 4 seconds grows too slowly to matter: after 10 failures a worker is still calling every 10 seconds, and 100 such workers still send 10 calls per second into a service that may need a minute of quiet. Doubling reaches a full minute between calls by the seventh failure.
The cap keeps the wait finite
Without the cap the doubling never stops. By the eleventh failure the computed wait passes a quarter of an hour, and by the twentieth it is measured in days. A worker sleeping that long is indistinguishable from a hung task, and no outage is helped by a client that checks back next week. The cap turns the curve into a ramp with a flat top: doubling while the service most needs room, then a steady polling interval of 60 seconds once the wait is long enough. With the cap in place, the 7 waits in the table add up to about 2 minutes before the eighth and final attempt.
- 17 min
- 6 days
Jitter: a random point inside the window
Full jitter replaces the computed wait with a random draw between 0 and that wait: sleep = random.uniform(0, min(cap, base * 2 ** (n - 1))). The capped value becomes the top of a window rather than the wait itself. After the third failure a worker no longer sleeps 4 seconds; it sleeps somewhere between 0 and 4 seconds, and the 100 workers in a wave each draw their own number. Here are 3 workers that failed at the same moment, across their first 4 failures.
| Failure | Window (0 to) | Worker A draws | Worker B draws | Worker C draws |
|---|---|---|---|---|
| 1 | 1 s | 0.8 s | 0.2 s | 0.5 s |
| 2 | 2 s | 1.7 s | 0.4 s | 1.1 s |
| 3 | 4 s | 0.3 s | 3.6 s | 2.2 s |
| 4 | 8 s | 6.9 s | 1.5 s | 4.8 s |
The average sleep is half the window, so jitter also shortens the typical wait. The cap still matters, because the window stops at 60 seconds and no draw exceeds it. Each worker draws fresh on every attempt, which breaks the synchronisation. Workers A and B failed in the same millisecond, but their later attempts are independent, so they collide only by chance rather than every time.
import random
def backoff_seconds(failures, base=1.0, cap=60.0):
raw = base * 2 ** (failures - 1)
window = min(cap, raw)
return random.uniform(0, window)This function is the whole policy. Call it with the number of consecutive failures so far, sleep for what it returns, and stop calling it once the attempt budget is spent. The retry loop from the previous section needs 1 change: replace the constant WAIT_SECONDS with backoff_seconds(attempt).
A retry policy uses base 0.5 s, cap 30 s and full jitter. A worker has failed for the 6th consecutive time. Which statement about its next sleep is right?
- It sleeps exactly 16 s, because 0.5 multiplied by 2 to the 5th is 16 and that is under the cap
- It sleeps a random amount between 0 and 16 s, because 0.5 multiplied by 2 to the 5th is 16 and that is under the cap
- It sleeps a random amount between 0 and 30 s, because 0.5 multiplied by 2 to the 6th is 32 and the cap applies
- It sleeps a random amount between 0 and 32 s, because 0.5 multiplied by 2 to the 6th is 32 and nothing caps it
About this lesson
This section is part of the Failure Handling: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.