Rate limiting protects a service from overload and abuse: requests that come in too often are dropped or queued, while the rest are handled as usual. Limits are set per client, IP address, API key or endpoint.
How token bucket and leaky bucket work
The classic schemes are token bucket and leaky bucket. In token bucket, tokens are added to the bucket at a constant rate; each request consumes one token, and if the bucket is empty the request is rejected. A full bucket allows a short burst: at a rate of 10 tokens per second and a capacity of 20, a client can make 20 requests in a row, and after that no more than 10 per second.
| Algorithm | How it works | What it provides |
|---|---|---|
| Token bucket | tokens are added at a constant rate, each request consumes one token | allows bursts up to the bucket capacity |
| Leaky bucket | requests are queued and processed at a constant rate, the queue is bounded | even flow: processing strictly at the set pace |
What the client sees when the limit is exceeded
When the limit is exhausted, the server responds with the status 429 Too Many Requests — added by RFC 6585 in 2012 — and may send a Retry-After header: a number of seconds or a date when the request can be repeated. Some tools respond differently by default: nginx returns 503 Service Unavailable, and the status is changed with the limit_req_status directive. The client is expected to retry with exponential backoff and respect Retry-After.
HTTP/1.1 429 Too Many RequestsRetry-After: 30 {"error": "rate limit exceeded", "retry_after": 30}Where it is used
- API gateways and reverse proxies: nginx with the
limit_reqdirective, per-API-key quotas in gateways - Protection against brute force and abuse: login attempts, form submissions, registration
- Limiting expensive operations: search, text generation, calls to third-party paid APIs
Sources
the term is written from these pages- RFC 6585 — Additional HTTP Status Codes (429 Too Many Requests)rfc-editor.org/rfc/rfc6585.html(opens in a new tab)
- RFC 9110 — HTTP Semantics, поле Retry-Afterrfc-editor.org/rfc/rfc9110.html(opens in a new tab)
- nginx — Module ngx_http_limit_req_modulenginx.org/en/docs/http/ngx_http_limit_req_module.html(opens in a new tab)
Verified 8 Nov