Sub topic 10
Retries, backoff, jitter, and retry budgets
This chapter assumes you know HTTP status codes, have read the earlier chapters on load shedding and client signals, and can read short Python examples.
Why retries can make overload worse
A service that sheds load returns 429 or 503 to some clients. Many clients respond by retrying. If every failed request is retried three times, the service sees up to four times the original traffic exactly when it is least able to cope. This effect is called retry amplification.
Amplification compounds across layers. If a request passes through three services and each layer retries three times, one user action can become 27 calls to the bottom service.
Exponential backoff
Backoff means waiting before each retry, and waiting longer after each failure. With exponential backoff the delay doubles: 0.1 s, 0.2 s, 0.4 s, and so on, up to a cap. The wait gives the server time to drain its queue.
Adding jitter
Backoff alone is not enough. If 1,000 clients fail at the same moment, they all retry at 0.1 s, then all at 0.2 s. The load arrives in synchronized waves. Jitter adds randomness so the retries spread out.
import random
def backoff_delay(attempt: int, base: float = 0.1, cap: float = 10.0) -> float:
"""Exponential backoff with full jitter; attempt starts at 0."""
return random.uniform(0, min(cap, base * 2 ** attempt))This is "full jitter". The delay is drawn uniformly between 0 and the current ceiling. The ceiling grows with each attempt until it reaches cap.
If the server sent a Retry-After header, honor it. The server knows more about its own recovery than the client does. Use the larger of the header value and your computed delay.
Retry budgets
Backoff limits how fast clients retry. A retry budget limits how many retries they make at all. A common rule is that retries may add at most 10% to the normal request volume. When the budget is spent, the client fails immediately instead of retrying.
A simple version reuses the token bucket from the admission control chapter, but it adds tokens on successes instead of on a timer:
class RetryBudget:
def __init__(self, ratio: float = 0.1, max_tokens: float = 10.0) -> None:
self.ratio = ratio
self.max_tokens = max_tokens
self.tokens = max_tokens
def record_request(self) -> None:
self.tokens = min(self.max_tokens, self.tokens + self.ratio)
def can_retry(self) -> bool:
if self.tokens >= 1.0:
self.tokens -= 1.0
return True
return FalseEach original request earns 0.1 tokens, so ten requests pay for one retry. During a widespread failure, successes stop and the budget drains. Retries then stop on their own, which is the behavior you want.
Rules of thumb
| Practice | Reason |
|---|---|
| Retry only idempotent operations, or use idempotency keys | A retried payment must not charge twice |
| Retry only errors that may succeed later (429, 503, timeouts) | A 400 or 404 will fail the same way again |
| Retry at one layer, usually the outermost | Avoids multiplication across layers |
| Cap attempts and total time | Keeps within the caller's deadline |
Exercise
A service has two layers. The client retries up to 3 times (4 attempts in total), and the service retries its database call up to 3 times (4 attempts in total). The database is failing every request. How many database calls does one client request cause, and which two techniques from this chapter reduce it?
Show answer
4 × 4 = 16 database calls. A retry budget stops retries once failures dominate, and retrying at only one layer removes the multiplication. Exponential backoff with jitter does not reduce the count but spreads the calls over time.