Sub topic 3
Retries, Backoff, Jitter, and Retry Budgets
Assume you know basic client/server programming, HTTP status codes, latency, and Python async tasks. This chapter focuses on preventing retries from making overload worse.
Retries Without Retry Storms
Retry only failures that are likely temporary, such as 503 Service Unavailable, and retry only operations that are safe to repeat. Exponential backoff increases the delay after each attempt. Jitter adds randomness so clients do not retry together.
import random
def retry_delay(attempt: int, base: float = 0.1, cap: float = 10.0) -> float:
window = min(cap, base * 2**attempt)
return random.uniform(0, window) # full jitterA retry budget limits retries as a fraction of normal traffic. For example, a client might permit one retry for every ten original requests. When the budget is empty, fail immediately.
Admission Control and In-Flight Limits
Admission control decides whether work may enter the service. A token bucket allows a steady rate while permitting a bounded burst.
import time
class TokenBucket:
def __init__(self, rate: float, capacity: float):
self.rate = rate
self.capacity = capacity
self.tokens = capacity
self.updated = time.monotonic()
def allow(self) -> bool:
now = time.monotonic()
self.tokens = min(
self.capacity,
self.tokens + (now - self.updated) * self.rate,
)
self.updated = now
if self.tokens < 1:
return False
self.tokens -= 1
return TrueA concurrency limit caps work already being processed. Reject excess requests with 503 and Retry-After: 1, or use 429 when the client should reduce its request rate.
Protocol Flow Control and Saturation
TCP, HTTP/2, and reactive-streams libraries provide forms of flow control. Receivers advertise available capacity, so senders slow down instead of endlessly buffering data. Application queues still need explicit bounds.
Track queue depth, queue age, rejection rate, in-flight work, worker utilization, latency percentiles, timeout rate, and retry rate. Rising queue age and latency usually reveal saturation before errors peak.
Combining and Testing
Use bounded queues with deadlines, concurrency limits, selective shedding, and client-visible overload signals. Back pressure preserves work by slowing producers. Load shedding protects the service by refusing work. Retries improve temporary failures but consume capacity, so combine them with backoff, jitter, and budgets.
Test beyond capacity with a controlled load test. Increase arrival rate past the known service rate and verify that memory stays bounded, latency eventually stabilizes, rejected requests receive usable signals, and recovery occurs after load falls.
> callout warning > A retry policy without a retry budget can multiply overload across every client and service layer.
Exercise
A service has a full bounded queue. Should it block every producer indefinitely?
Show answer
No. Apply a deadline or reject excess work with an appropriate overload response so resources remain available for recoverable requests.