Rate limiting
How to stop a single client from consuming the capacity of an API that every other client also uses.
Step by step
- Reaching the API
- 72 req/s
- API queue
- 0
- App response
- ~ms
- key_rex bucket
- —
Step 1 of 9
Three clients, 72 req/s
WeatherAPI answers GET /forecast and handles about 200 req/s. The app, the website and Rex Store’s script add up to 72 req/s, and everyone gets answers in milliseconds.
A weather forecast API with three clients
WeatherAPI answers GET /forecast?city=…. Its servers handle about 200 requests per second. The numbers in this example were made up for the lesson.
| Client | API key | Normal traffic |
|---|---|---|
| Mobile app | key_app | 40 req/s |
| Website | key_site | 30 req/s |
| Rex Store’s script | key_rex | 2 req/s |
That adds up to 72 req/s. The API answers in milliseconds and every client is happy.
A bug at Rex Store leaves everyone without a forecast
Rex Store ships a new version of its script. Because of a bug, every error response makes the script retry immediately. It falls into a loop and starts sending 500 req/s.
Now 572 req/s arrive and only 200 get served. Every second, 372 more pile up in a queue that keeps growing. The app’s and website’s requests wait behind Rex’s, hit their timeout and fail. The timeouts make Rex’s script retry even more, and things get worse on their own.
The capacity belongs to everyone, but nobody has a quota
The server serves requests in order of arrival, without knowing who sent each one. To it, Rex’s 500 count the same as the app’s 40. Two things are missing:
- Identifying the client. The API does not tell
key_rextraffic apart fromkey_apptraffic. - A quota per client. There is no rule like “each key may make at most X requests per second”. That is why a single client can take the whole capacity.
Stripe’s post about its rate limiters describes the same case: one user’s traffic spike must not take the API down for everyone else.
Rate limiting
Rate limiting is controlling how many requests each client may make in a time interval and rejecting the ones above that. HTTP has a status just for this rejection: 429 Too Many Requests, defined in RFC 6585. It means the user has sent too many requests in a given amount of time. The response may include a Retry-After saying how long to wait.
Every API key gets its own quota. Whoever goes over it gets a fast, cheap 429, and the remaining capacity stays available to everyone else.
One token bucket per API key
The algorithm Stripe uses is the token bucket. Each key has a bucket with a capacity of b tokens. The bucket gains tokens at a rate r and, when it is full, new tokens are thrown away. Each request spends 1 token. If the bucket is empty, the request gets a 429.
At WeatherAPI, each key gets b = 10 and r = 5 tokens/s.
Here is what happens to Rex during the loop, second by second:
| Time | Tokens before | Rex’s requests | Result |
|---|---|---|---|
| t = 0 s | 10 | 500 | 10 go through (burst), 490 get a 429 |
| t = 1 s | 5 | 500 | 5 go through, 495 get a 429 |
| t = 2 s… | 5 | 500 | 5 go through per second while the loop lasts |
The API now receives 40 + 30 + 5 = 75 req/s, well below the 200 it can handle. The app and the website don’t even notice the problem. The 495 rejections per second are cheap: the limiter checks a counter and answers, without ever reaching the database.
Try the key_rex bucket below. The refill really runs, at 5 tokens per second.
Each part of the fix removes one piece of the problem:
- One bucket per API keyFixes: the server didn’t tell clients apart. Rex’s excess only spends Rex’s tokens.
- Rate r = 5/sFixes: one client could take the whole capacity. The sustained maximum per key is now 5 req/s.
- Capacity b = 10Fixes: a limit that is too rigid would reject legitimate spikes. A client that has been idle can send up to 10 at once.
- 429 response with Retry-AfterFixes: the client retrying blindly. The response says how long to wait, and a well-built client waits.
- Counters in shared storage (Stripe uses Redis)Fixes: with several servers, a counter on each one would give every server its own quota.
The response Rex gets when the bucket is empty:
HTTP/1.1 429 Too Many Requests Retry-After: 1 RateLimit-Policy: "per-key";q=10;w=2 RateLimit: "per-key";a=0;w=1 Content-Type: application/json { "error": "Limit of 5 req/s per key exceeded. Try again in 1 s." }
Retry-After and the 429 status come from RFC 6585. The RateLimit-Policy field (quota q over a window of w seconds) and the RateLimit field (available quota a) come from an IETF proposal, draft-ietf-httpapi-ratelimit-headers-12, which is not an RFC yet. Since that format describes quotas per window, the values above approximate the bucket. Per the RFC, a 429 response must not be stored by a cache.
Practical advice from Stripe’s post: fail open, meaning that if Redis goes down, let requests through instead of rejecting all of them. Before turning a limit on, run it in observation mode (a dark launch) to see who it would block. Write clear error messages that tell the client what to do.
- Rate limiting
- Limiting how many requests each client makes in a time interval and rejecting the excess. Avoid: “blocking”, “firewall”.
- Quota
- The number of requests a client may make in a time window. Avoid: “global limit”.
- Partition key
- What identifies whose quota it is: the API key, the user or the IP. Avoid: “ID”, which is vague.
- Token bucket
- Algorithm in which a bucket of capacity b gains tokens at a rate r and each request spends one token. Avoid: “simple counter”.
- Burst
- Several requests in quick succession. In a token bucket, the largest possible burst is the capacity b.
- 429 Too Many Requests
- HTTP status for “you went over the request limit”, defined in RFC 6585. Avoid: using 503, which means “I am overloaded”.
- Retry-After
- HTTP header with how many seconds the client should wait before trying again.
- Fail open
- If the limiter itself breaks, letting requests through instead of rejecting all of them.
- Load shedding
- Dropping low-priority traffic when the whole system is overloaded, whatever the client. Avoid: mixing it up with rate limiting, which is per client.
1. A key’s bucket has b = 20 and r = 2/s, and the client has been idle for 1 minute. It sends 30 requests at once. How many go through?
Tokens that arrive while the bucket is full are thrown away, so it stops at 20. The other 10 get a 429, or go through later as the bucket refills at 2 per second.
2. WeatherAPI runs on 4 servers behind a load balancer, and each one keeps the buckets in its own memory. What is Rex’s real limit?
Each server lets 5 per second through without knowing about the others. That is why the state lives in shared storage, such as Redis.
3. An app gets a 429 with Retry-After: 30. What should it do?
Retrying right away is Rex’s loop. And RFC 6585 forbids caches from storing 429 responses.
4. The Redis that holds the buckets went down. Following Stripe’s advice, what should the limiter do?
A problem in the limiter must not turn into an API outage. For a while, it is better to have no protection than no service.
- Scaling your API with rate limiters, Paul Tarjan, Stripe (2017). The four kinds of limiter, the token bucket in Redis and the operational advice.Read next
- RFC 6585, section 4: 429 Too Many Requests. The definition of the status, Retry-After and the caching ban.
- draft-ietf-httpapi-ratelimit-headers-12, IETF HTTPAPI (November 2026). The RateLimit and RateLimit-Policy fields. Still a draft.
- Token bucket, Wikipedia. Capacity, refill rate and maximum burst size.