How much work a key may consume.
Rate limiting protects finite work.
A limiter decides whether one request may proceed now. It can protect a backend, enforce a plan, reduce abuse, or keep one caller from crowding out others.
The key defines who shares a budget: a signed-in user, API key, tenant, endpoint, or—carefully—an IP address. IPs are easy to obtain but can group many people behind one network.
How capacity becomes available over time.
Extra stored tokens allowed briefly; it is separate from the sustained refill rate.
A rejected request stops here. It is not silently queued for later.
Design a distributed API rate limiter.
Support per-key quotas across gateway replicas while keeping the protected API available.
Protect a multi-tenant API at 100K requests/s. Enforce per-API-key limits across gateway replicas, permit documented bursts, return useful client headers, and define behavior when shared state is slow or unavailable.
Design the contract, algorithm, state, atomic operation, scaling, failure policy, and observability.- 01Identity and quota scope
- 02Sustained rate and burst allowance
- 03Accuracy and latency targets
- 04Multi-region requirements
- 05Failure and abuse policy
- 06Operational signals
Follow the decision between the boxes.
Select a workflow, then move through each component to see the data, purpose, and failure risk.
Choose the behavior you need.
Token bucket
Tokens refill continuously at the sustained rate. Capacity permits a bounded burst. Primary model in this lab.
Fixed window
A counter resets at fixed boundaries. Simple and cheap, but adjacent windows can admit a sharp double burst.
Sliding window counter
Blends adjacent counters for a smoother estimate. Less memory than a log, with approximation near boundaries.
Sliding window log
Stores request timestamps for an exact rolling window. Accurate, but memory and cleanup cost grow with traffic.
Leaky bucket
Drains work at a steady rate and is often used to shape queued traffic. Unlike this lab’s reject-first limiter, it introduces a queue.