Design a rate limiter for an API
Limit each API key to 100 requests per minute
System design, Medium
Design a service that limits each API key to 100 requests per minute. The API runs on 20 servers behind a load balancer in one region. Peak traffic is 20,000 requests per second across about 50,000 active keys. The check must add less than 2 ms at the 99th percentile. Cover: the algorithm and why you chose it, where the counters live, what you return to a blocked caller, what happens when the counter store is down, how the counter store grows and what you do about one very busy key, and how you know it works. Out of scope: billing and per-plan limit configuration.
Examples
Constraints
- The limit is 100 requests per minute per API key.
- 20 API servers behind one load balancer in one region. Peak 20,000 requests per second across about 50,000 active keys.
- The check adds less than 2 ms at the 99th percentile.
- Out of scope: billing and per-plan limit configuration.
Your answer must contain
- Rough load and memory numbers, the algorithm with its main weakness, the data layout and the atomic step, the failure choice, how the counter store grows and what you do about one very busy key, and a test or metric plan.
A strong answer covers A strong answer covers the load numbers, an algorithm with its weakness, an atomic counter step, the 429 reply, the store-down choice and a test plan.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Do the numbers first. Is 20,000 checks per second a heavy load for one store? How much memory do 50,000 keys need?
Hint 2 the approach
A counter on each server gives each of the 20 servers its own limit. Keep one shared counter per key, and make the read and the update one atomic step.
Hint 3 the key step
Compare a fixed window with a token bucket or a sliding window counter, and say which bursts each allows. Decide what happens when the counter store is down, and why.
Walkthrough
Full walkthrough brute force to best
Naive design
Each server keeps its own in-memory counter for each key. It is fast and needs no shared store. But with 20 servers, a key can send 100 requests to each one, so the real limit is about 2,000 per minute, and a restart resets the counters.
Improved design
A shared Redis counter for each key and minute: increment it, then set an expiry of 60 seconds. The limit is now global and the check is one round trip. But a fixed window lets a caller send 100 requests at 0:59 and 100 more at 1:00, so twice the limit passes in a short time.
Final design
Numbers: 20,000 checks per second is small for Redis, which handles about 100,000 simple operations per second, and 50,000 keys at about 100 bytes is about 5 MB. One primary with a replica in the same region is enough, with one round trip per request to stay under 2 ms. Algorithm: a token bucket with 100 tokens refilled over a minute, two numbers per key (tokens and last refill time). It allows controlled bursts with cheap state. A sliding window counter is a close second. On the second example, a caller sends 100 requests at 0:59 and 100 more at 1:00. The bucket is empty after the first 100 and refills about 1.7 tokens in one second, so about 101 of the 200 pass. A strict sliding window log lets exactly 100 pass in any 60 seconds, but it stores up to 100 timestamps per key, about 40 MB for 50,000 keys, where the bucket keeps two numbers. Atomic step: one Lua script reads the tokens, adds those earned since the last refill, subtracts 1 if possible, and writes back with an expiry. Use the Redis clock inside the script, not the app server clocks. Reply: HTTP 429 with a Retry-After header and the rate-limit headers. Store down: decide per route. Fail open protects availability and suits a general API. Fail closed protects a fragile backend or a paid quota. A middle path is a local limit of one twentieth per server during the outage, plus an alert. Verify: test the script with a fake clock, load test at 20,000 per second, and check that request 101 in a minute is refused.
Follow one request
Follow one key through 101 requests in a minute across 20 servers. Every request runs the same script against the same bucket in Redis. The bucket starts with 100 tokens. Requests 1 to 100 each take one token and pass. Request 101 finds fewer than 1 token, so the script refuses it and the API answers HTTP 429 with a Retry-After header that says when a token will exist.
API and data model
Lua script, one atomic call for each request
KEYS[1] = rl:<apiKey> ARGV = capacity 100, refill per second 100 / 60, cost 1
read tokens and last refill time from the key
now = Redis server time
tokens = min(100, tokens + (now - last) * refill)
if tokens >= 1: tokens = tokens - 1, allowed
else: refused, retry-after = (1 - tokens) / refill
write tokens and now, set an expiry of 120 secondsNumbers and capacity
About 20,000 checks per second, about 5 MB for 50,000 keys, and under 2 ms per check with one same-region round trip.
Common mistakes
- Counters in each server memory. With 20 servers the real limit is 2,000 per minute.
- A read and then a write from different servers with no atomic step. Two servers can both pass the same request.
- A fixed window with no word on the burst at the edge of the window.
- No stated choice for when the counter store is down.