Limit demand at the identity and resource that matter#
One client can send 50,000 requests per second to an API built for ordinary traffic. A rate limiter protects fairness, abuse boundaries, infrastructure, downstream dependencies, and product quotas. First choose the dimension: user, IP, API key, tenant, endpoint, global traffic, or a combination.
Algorithms encode burst policy#
Fixed window
user:42:2026-08-14T12:30 → 73 of 100 requestsOne counter per calendar bucket is simple, but traffic can burst at the end of one window and start of the next.
Sliding window
Count the actual preceding interval. Boundary behavior improves, while exact logs or approximations require more state and computation.
Token bucket
Tokens refill at a sustained rate up to a capacity. Requests consume tokens, allowing a bounded burst while limiting the long-run rate.
Token bucket: 10-token burst, 5-token/sec refill
Each request needs one token. Pause refill or send a burst to see when requests are rejected.
Leaky bucket
Model work leaving at a controlled rate. It emphasizes smoothing output, while token bucket emphasizes permitting bounded bursts. Implementations vary, so describe the behavior you need before naming the algorithm.
Local counters undercount a global limit#
If three API servers each enforce 100 requests per minute independently, a client routed across them can exceed an intended global 100. Shared counters, partitioned ownership, local allowances plus reconciliation, or edge enforcement provide different trade-offs.
| Counter placement | Benefit | Cost |
|---|---|---|
| Local per server | Fast and available | Approximate across replicas |
| Shared store | Coordinated decision | Network latency, hot keys, store dependency |
| Partitioned ownership | Scales counter responsibility | Routing and rebalancing |
| Local + global approximation | Lower decision latency | Temporary overshoot and reconciliation |
Return a useful rejection and design limiter failure#
429 Too Many Requests communicates rate limiting. The response can explain the condition and may include retry timing; clients should back off rather than create a retry storm.
Plan for an unavailable counter store, hot identities, partitions, clock/window errors, global contention, and synchronized retries. Fail-open versus fail-closed depends on what is protected: login-abuse controls and a low-risk recommendation endpoint can make different choices.
- Saying “use Redis INCR” without defining the window, atomic operation, hot-key behavior, regions, or failure policy.
- Limiting by IP when many users share an address or attackers rotate addresses.
- Demanding globally exact limits without accepting coordination latency and availability cost.
- Building a distributed limiter for a low-traffic single-instance service.
Use these prompts to extend the design. The useful answer is a reasoned change, not a memorized sentence.
- How is one identity limited across 100 API servers?
- What happens if shared limiter state is unavailable?
- How are short bursts supported?
- How would a cross-region limit trade accuracy for availability?
Related practice problems
Use the manifest-backed prompts below to apply this lesson in a complete design.
- RFC 6585: Additional HTTP Status CodesIETF / RFC Editor
- Rate Limiting patternAzure Architecture Center