Sub topic 6
Adding jitter to avoid synchronized retries
This chapter assumes you can read basic Python, know what an HTTP 429 or 503 response means, and have seen the idea of exponential backoff from the previous chapter. Here you will see why backoff alone is not enough, and how adding randomness fixes it.
The problem: retries that line up
Exponential backoff spaces out the retries of a single client. It does not spread out many clients. Suppose 200 workers all hit a rate limit at the same moment. With plain backoff, each worker waits 0.5 seconds, then 1 second, then 2 seconds. All 200 workers retry at the same instant each time. The provider sees a burst, rejects many requests again, and the cycle repeats. This pattern is called a thundering herd.
Jitter is a small amount of randomness added to each wait. Workers that failed together now retry at different times, so the load arrives as a spread-out stream instead of spikes.
Common jitter strategies
All strategies start from the same ceiling: min(cap, base * 2 ** (attempt - 1)). They differ in how they choose a delay below it.
| Strategy | Delay | Notes |
|---|---|---|
| No jitter | `ceiling` | Clients stay synchronized. |
| Full jitter | `random.uniform(0, ceiling)` | Best spread. Some waits are very short. |
| Equal jitter | `ceiling/2 + random.uniform(0, ceiling/2)` | Guarantees a minimum wait, with less spread. |
Full jitter is a good default for LLM calls. It spreads load the most, and a short wait costs little because the provider rejects a too-early retry cheaply.
Full jitter in code
The retry loop from the topic uses full jitter in one line:
import random
def full_jitter_delay(attempt: int, base: float = 0.5, cap: float = 30.0) -> float:
"""Return a random wait between 0 and the exponential ceiling."""
ceiling = min(cap, base * 2 ** (attempt - 1))
return random.uniform(0, ceiling)
for attempt in range(1, 6):
print(attempt, round(full_jitter_delay(attempt), 2))Each run prints different values, and the maximum possible delay doubles each attempt until it reaches the cap. In the full retry function, this delay is then compared with the Retry-After header. If the provider asks for a longer wait, the code uses that value instead of the random one.
delay = full_jitter_delay(attempt)
retry_after = parse_retry_after(resp.headers.get("retry-after"))
if retry_after is not None:
delay = max(delay, min(retry_after, 30.0))Taking the larger of the two values keeps jitter from undercutting the provider's instruction.
Common errors
- Adding jitter to the cap only. Applying
random.uniformaftermin(cap, ...)is correct. Adding a fixed random amount, such asdelay + 0.1, barely changes the spread. - Seeding the random generator identically in every worker. Workers that call
random.seed(42)produce the same "random" delays. Leave the generator unseeded in production. - Jittering a delay the provider set. A
Retry-Aftervalue is a minimum, so never shorten it with jitter. If you want to spread those retries, add a small random amount on top. - Expecting jitter to fix a sustained limit. If your steady request rate exceeds the quota, jitter only spreads the failures out. Use client-side throttling for that, which a later chapter covers.
Exercise
A team runs 50 workers with exponential backoff (base 1 s, no jitter). All of them get a 429 at the same time. At what times do they retry, and how does full jitter change the third retry?
Show answer
Without jitter, all 50 workers retry together after 1 s, 2 s, and 4 s, so each retry is a burst of 50 requests. With full jitter, the third retry wait is a random value between 0 and 4 s for each worker, so the 50 requests spread across that 4-second window.