What should a server send back when it rejects a request because of a rate limit?
Sketch your own answer first, then compare it with the one below.
A strong answer
The standard answer is HTTP status 429, Too Many Requests. It says that this caller has sent too many requests in a given time. It is the right code for a per-caller limit. A 503 means the server itself cannot take the request, which is a different message.
Add a Retry-After header. It tells the client how many seconds to wait, or a date to come back at. Without it every client guesses, and many guess the same delay. With it the server sets the schedule.
Make the rejection cheap and fast. It should cost far less than serving the request, or the limiter protects nothing. A short body that says which limit was hit helps the people debugging a client.
Many APIs also return headers with the limit, the remaining allowance and the reset time on every response, so well behaved clients can pace themselves before they are rejected.
Clients have to honour the signal. Retrying at once on a 429 turns the limit into a loop.
Sources
- Mark Nottingham, Roy Fielding (2012). Additional HTTP Status Codes.
- Roy Fielding, Mark Nottingham, Julian Reschke (2022). HTTP Semantics.