Want the details?
What is a rate limiter?
A server can answer only so many requests per second. If one user, one buggy script, or an attacker sends far too many, everyone else waits, or the server falls over. A rate limiter stands at the door and counts. It lets a normal amount of traffic through and turns the rest away with a polite “too many requests, try later”.
The most common design is the token bucket. A bucket holds tokens. A tap adds tokens at a fixed rate. Every request must take one token to pass. If the bucket is empty, the request is turned away.
How to read the bench
| Part on the bench | What it means in a real system |
|---|---|
| Hopper (left) | The clients. It sends requests in the pattern you choose: steady, bursts, or a flood. |
| Ball | One request. |
| Token tray (behind the lane) | The bucket. Each disc is one token. Closed places have a lid; the bucket size opens 2, 6 or 10. |
| Tap tower | The refill: one new token every second. You can see the next token grow in its place. |
| Gate | The rate limiter. A ball with a token goes under the lifted bar. A token is used up each time. |
| Tray marked 429 | Requests that were turned away. 429 is the real HTTP status code for “Too Many Requests”. |
| Server (right) | Where the accepted requests go. |
| Meters | Tokens left, requests turned away since the last change, and the last 20 requests: filled if passed, hollow if turned away. |
Why a bucket, and not a simple counter?
Real traffic is uneven. A person opens a page, and the browser sends ten requests at once; then nothing for a while. A plain rule like “at most one request per second” would turn most of that page away. The bucket saves up tokens while it is quiet, so a short burst can pass. But it can only save as many tokens as it holds, so over a long time the limit stays the same: the refill rate.
So a token bucket has two settings: the size decides how big a burst may be, and the refill rate decides the long-run limit.
The numbers
- The tap adds 1 token per second. That is the long-run limit: 60 requests per minute.
- Steady traffic: one request every 1.25 seconds, 48 per minute. That is under the limit, so nothing is turned away.
- Bursts: 10 requests within 2 seconds, every 10 seconds. With a bucket of 6, 7 of each burst pass (6 saved tokens plus about 2 from the tap during the burst) and 3 are turned away. With 10, all pass. With 2, only 3 pass.
- Flood: 2.5 requests every second. Whatever the bucket size, exactly 1 per second passes, and 60% are turned away.
In real systems
- Everywhere on the internet. Public APIs limit each user, for example to a few thousand requests per hour. Nginx has a
limit_reqsetting. Cloudflare and other gateways rate-limit at the edge, before traffic reaches your servers. - Per key. A real limiter keeps one bucket per user, API key or IP address, often in Redis so that all servers share the counts.
- Being polite. A good 429 answer includes a
Retry-Afterheader, which tells the client how long to wait. Good clients wait and try again, a little later each time. - Other designs. A fixed window counts requests per minute and resets; it is simple, but allows double bursts at the edge of a minute. A sliding window fixes that. A leaky bucket sends requests out at a perfectly even pace.
Words to know
- Rate limit
- The most requests allowed in a period of time.
- Token
- A permission to send one request.
- Bucket size
- The most tokens that can be saved up: the biggest burst allowed.
- Refill rate
- How fast new tokens arrive: the long-run limit.
- Burst
- Many requests in a very short time.
- 429
- The HTTP status “Too Many Requests”.