System design lab · Episode 3

How a rate limiter protects a server

A rate limiter lets a normal amount of traffic through and politely turns the rest away, so one busy client cannot overload a server. Here it is a gate on a lane: every request needs a token to pass, and a tap adds one token per second. Follow the lessons under the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
429
The request passing the gate nowThe token tray: the bucket, with 2, 6 or 10 placesThe tray marked 429: requests turned away

Controls

Bucket size
Traffic
Time

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 benchWhat it means in a real system
Hopper (left)The clients. It sends requests in the pattern you choose: steady, bursts, or a flood.
BallOne 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 towerThe refill: one new token every second. You can see the next token grow in its place.
GateThe rate limiter. A ball with a token goes under the lifted bar. A token is used up each time.
Tray marked 429Requests that were turned away. 429 is the real HTTP status code for “Too Many Requests”.
Server (right)Where the accepted requests go.
MetersTokens 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_req setting. 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-After header, 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”.