Sub topic 9
Telling Clients: 429, 503, and Retry-After
This chapter assumes you know basic HTTP status codes, have read the earlier chapters on load shedding and bounded queues, and can read short Python examples. Once a service decides to reject work, the client needs to know why and what to do next. A bare connection reset or a timeout gives it nothing to act on.
Two codes, two meanings
The two status codes most used for overload describe different causes.
| Code | Name | Meaning | Typical sender |
|---|---|---|---|
| 429 | Too Many Requests | This client sent more than its allowance. | Rate limiter, token bucket |
| 503 | Service Unavailable | The server cannot take the request right now, whoever sent it. | Concurrency limit, full queue |
Use 429 when you can blame a specific caller, such as a client that exceeded its token bucket. Use 503 when the whole service is saturated and the caller did nothing wrong. The difference matters because clients react differently. A 429 tells a client to slow down. A 503 tells it the service is struggling, so it should back off and may try another replica.
The Retry-After header
Both codes can carry a Retry-After header. Its value is either a number of seconds or an HTTP date. It tells the client the earliest time a retry is likely to succeed.
Pick the value from what you know. A token bucket can compute it exactly: the time until enough tokens exist. A saturated service can only estimate it, for example from current queue length divided by throughput. If you have no good estimate, a small value such as 1 second is better than none.
The in-flight limit from the earlier chapter already returns this pair:
MAX_IN_FLIGHT = 50
in_flight = 0
async def handle(request, process):
global in_flight
if in_flight >= MAX_IN_FLIGHT:
return 503, {"Retry-After": "1"}
in_flight += 1
try:
return 200, await process(request)
finally:
in_flight -= 1A token bucket can compute a precise 429 value. This helper assumes the TokenBucket class from the admission control chapter:
import math
def retry_after_seconds(bucket, cost: float = 1.0) -> int:
missing = max(0.0, cost - bucket.tokens)
return max(1, math.ceil(missing / bucket.rate))The result is rounded up so the client never retries too early, and it is at least 1 because the header value is a whole number of seconds.
How the signal flows
Common mistakes
- Rejecting slowly. A rejection that takes as long as real work defeats the purpose. Check the limit first, before parsing bodies or touching a database.
- Using 500 for overload. Clients and proxies treat 500 as a bug, not as a request to slow down.
- Setting Retry-After to the same value for everyone. If 1,000 clients are told "retry in 1 second", they all return together. Clients should add jitter, covered in the next chapter, and servers can vary the hint slightly.
- Ignoring the header on the client. A client that retries immediately after a 429 makes overload worse.
Exercise
A service has one client that sends 500 requests per second against an allowance of 100, while every other client is within its limit. The service itself is healthy. Which status code should it return to the heavy client, and how should it choose the Retry-After value?
Show answer
Return 429, because the cause is this client exceeding its own allowance and not service-wide saturation. Compute Retry-After from the token bucket as the time until enough tokens are available, rounded up to a whole number of seconds (at least 1).