A rate limiter decides, for every incoming request, whether the sender is still within their quota. If they are, the request goes through. If not, it's rejected before it reaches your servers. It protects a backend from abuse, runaway scripts and one noisy user starving everyone else. The hard part is that it sits in front of every request, so it must be fast and must never become the thing that takes the system down. This page runs it through the Interview Approach, one step at a time.
1. Functional requirements#
The limiter has one job, broken into three steps:
- A user makes a request.
- The system tracks requests per user, identified by user ID, or by IP address when the user isn't signed in.
- It checks the request against that user's quota, say 1,000 requests per minute:
- Within the quota, the request is allowed and forwarded to the server.
- Over the quota, the request is declined with
429 Too Many Requests.
The quota rules can change without a deploy, for example a higher limit for paying customers or a lower one for a sensitive endpoint.
Out of scope: billing for usage, per-request pricing, and blocking attacks at the network layer (that's DDoS protection, a separate system).
2. Non-functional requirements#
These three qualities shape the design:
| Quality | Requirement |
|---|---|
| Highly scalable | Handle 1 million requests per second across 100 million users |
| Availability over consistency | The limiter must keep answering when parts fail. Letting a few extra requests through is better than blocking everyone |
| Low latency | The limiter adds under 10 ms to each request |
A quick estimate shows what these numbers mean:
- Throughput: every request needs one check, so the counter store takes 1 million operations per second. A single Redis node handles very roughly 100,000 simple operations per second, so you need on the order of 10 nodes, more with headroom. One node won't do.
- Memory: one bucket per user holds a token count and a timestamp, about 100 bytes with the key and overhead. 100 million users × 100 bytes ≈ 10 GB. That fits in memory across a small cluster.
- Latency: 10 ms leaves room for one network round trip to a nearby store (well under 1 ms in the same data center), not for a disk read or several hops.
3. Actors#
Three parties touch the limiter:
| Actor | What they do | Access |
|---|---|---|
| Client | Calls the API and is counted against a quota | Identified by user ID, or by IP address if anonymous |
| Backend server | Handles requests the limiter allows | Internal, never sees rejected requests |
| Admin | Sets and changes quota rules | Internal, authenticated |
The client is the one being limited. The admin only changes rules, which is rare, while client requests arrive a million times a second.
4. API routing#
The limiter doesn't add a public endpoint of its own. It wraps every existing route:
| Actor | Endpoint | Response |
|---|---|---|
| Client | Any API route | 200 from the server if allowed, 429 if over the quota |
| Admin | PUT /v1/rules/{ruleId} |
200 with the saved rule |
A rule says who it applies to, how many requests, and over what window:
{
"key": "userId" | "ip",
"limit": number,
"windowSeconds": number
}
A rejected request gets a 429 with headers that tell the client when to retry, so well-behaved clients back off instead of hammering you:
HTTP/1.1 429 Too Many Requests
Retry-After: <seconds>
X-RateLimit-Limit: <limit>
X-RateLimit-Remaining: 0
See status codes for where 429 sits among the others.
5. High-level design#
Build the design one functional requirement at a time.
A user makes a request (requirement 1). The client sends its request to an API gateway, the single entry point in front of your servers. The gateway is the natural place for the limiter: it already sees every request, and a rejected request never reaches the server. See API gateway.
Track requests per user (requirement 2). The gateway reads the user ID from the auth token, or falls back to the client's IP address. It uses that as the key into a token bucket service backed by Redis servers. Each key holds one bucket:
| Field | Type and meaning |
|---|---|
key |
string: user ID or IP address |
tokens |
number: tokens left in the bucket |
last_refill |
timestamp: when tokens were last added |
Allow or decline (requirement 3). The token bucket works like this. Each user has a bucket that holds up to 1,000 tokens. Tokens refill at a steady rate, here 1,000 per 60 seconds, about 16.7 per second. Each request takes one token:
- Work out how many tokens have refilled since
last_refill, add them, and cap at 1,000. - If at least one token is left, take it, save the bucket, and allow the request. The gateway forwards it to the server, which answers with
200. - If the bucket is empty, decline the request. The gateway answers the client directly with
429 Too Many Requests.
The token bucket allows short bursts (a full bucket can spend 1,000 tokens at once) while holding the average to the quota. Other algorithms make different trade-offs:
| Algorithm | How it counts | Trade-off |
|---|---|---|
| Fixed window | A counter per minute, reset on the minute | Simplest, but a user can send 2× the limit across a window boundary |
| Sliding window log | Stores a timestamp per request | Exact, but memory grows with traffic |
| Token bucket | Tokens refill at a steady rate | Small state, allows controlled bursts. Common default |
Where the rules live. The quota rules are kept in a rule config store. The gateway needs them on every request, so it keeps a copy in memory rather than fetching them each time.
6. Address the non-functional requirements#
The basic design works for one gateway and one Redis server. Now take each quality and find where the design breaks it.
Low latency#
Two things threaten the 10 ms budget.
Several round trips per check. Read the bucket, compute, then write it back is two trips to Redis, and two gateways handling the same user at once could both read 1 token and both allow. Run the whole check as a single Lua script in Redis. Redis runs a script atomically, so the read, refill and decrement happen in one round trip with no race.
Fetching rules per request. The rule config is pushed to every gateway when it changes, and/or each gateway polls it periodically, say every 30 seconds. Checking a rule is then a memory lookup:
| Approach | How it works | Trade-off |
|---|---|---|
| Push | Rule config sends changes to gateways | New rules apply in seconds, but you need to handle gateways that miss a message |
| Poll | Gateways fetch rules on a timer | Simple and self-healing, but changes take up to one interval to apply |
| Both | Push for speed, poll as a safety net | A bit more work, best of both. A good default |
Rules change rarely, so a short delay before a new rule applies is fine.
Highly scalable#
At 1 million checks per second, one Redis server is the bottleneck. Replace it with a Redis cluster: several primaries, each owning a share of the keys. Redis Cluster splits keys into hash slots by hashing the key, so all checks for one user go to the same primary, and adding primaries spreads the load. See distributed cache and choosing a shard key. The user ID is a good shard key: it's on every request and spreads evenly. Watch for one huge user (or a shared office IP) making one shard hot.
The gateways keep no state of their own, so you add more of them behind a load balancer as traffic grows. Set a TTL on each bucket, a little longer than the time to refill it, so the buckets of idle users expire and memory stays near the 10 GB estimate.
Availability over consistency#
A Redis primary fails. Give each primary a read replica. If the primary dies, the replica is promoted and takes over its keys. See replication models. Replication is asynchronous, so the newest few decrements may be lost in a failover and some users get a few extra requests. That's the availability-over-consistency choice from step 2. Note that the token check itself is a write, so it always goes to the primary. Replicas are for failover, and for read-only uses such as showing a user their remaining quota.
All of Redis is unreachable. The gateway has to pick:
- Fail open: allow every request while the limiter is down. The service stays up, but it's unprotected for that time.
- Fail closed: reject every request. The backend is safe, but you've caused a full outage yourself.
Fail open matches "availability over consistency" and is the usual choice for general APIs. Fail closed fits only where an overload is worse than an outage, such as a login endpoint under attack. Either way, use a short timeout on the Redis call, a few milliseconds, so a slow Redis doesn't blow the latency budget.
Counts across regions. If you run in several regions, a global quota means a cross-region call on every request, which breaks the 10 ms budget. Keep a separate cluster per region and split the quota between them, accepting that a user spread across regions may get slightly more than the exact limit. See CAP theorem and consistency.
Whiteboard sketch#
The sketch below shows the full flow from steps 5 and 6: the client's request passes through the API gateway, which checks a token bucket in Redis, forwards allowed requests to the server and returns 429 to the client when the limit is exceeded, while a rule config feeds the gateway its rules.

Key takeaways#
- The limiter keys every request by user ID, or IP address if anonymous, and allows it or returns
429 Too Many Requestswith aRetry-Afterheader. - Put it in the API gateway, so rejected requests never reach your servers.
- A token bucket stores two numbers per user, allows short bursts and holds the average to the quota.
- Run the check as one atomic Lua script in Redis: one round trip, no race between gateways.
- Keep rules in gateway memory. Push changes and poll as a safety net.
- Scale with a Redis cluster sharded by user key. Add a replica per primary for failover.
- Choose availability: accept a few extra requests after a failover, and fail open if Redis is down.