Rate limiter design
Проектирование rate limiter
Designing a rate limiter means deciding how fast each client can send requests and what happens when it goes over the limit. The goal is to protect the service from overload and keep one client from consuming too many resources.
Also known as: token bucket, leaky bucket
Start with the policy
A rate limiter decides whether a request may proceed before the service does the work. Define who shares a budget: a signed-in user, an API key, or an entire customer account. Then decide what the budget covers. An API might enforce an account-wide limit and a separate limit on expensive report requests. An IP address can be useful when callers are anonymous, but several unrelated users may share it.
Choose how to handle bursts
- Fixed window: Count requests in intervals such as calendar minutes. This needs little state, but a client can use one minute's allowance just before the boundary and the next minute's allowance just after it.
- Sliding window: Count requests in the most recent interval, such as the last 60 seconds. Keeping a timestamp per request gives an exact count; grouping requests into smaller time bins trades some precision for lower memory use.
- Token bucket: Refill a balance at a steady rate, up to a capacity. Each admitted request spends tokens. A full bucket lets a short burst through immediately, while the refill rate controls how much traffic can be sustained.
- Leaky bucket: In the queue-based version, work leaves a bounded queue at a steady pace. A burst waits rather than hitting the downstream service all at once. Once the queue fills, new arrivals are rejected.
Example: protecting a reports API
Suppose each customer gets a bucket with a capacity of 20 tokens and a refill rate of 5 tokens per second. With one token per request, an idle customer can send 20 requests immediately. Once the bucket is empty, it takes 200 milliseconds to earn enough for one more. A request that arrives too early can get 429 Too Many Requests, with Retry-After to tell the client when to try again. The limiter can calculate the refill from elapsed time on each check rather than running a timer for every customer.
Make the quota consistent across instances
An in-memory bucket works on one server. If several servers each keep a bucket for the same customer, they enforce separate allowances, not a shared quota. A gateway can instead use Redis to store the balance and last update time for each key. Refill, check, and deduct in one atomic operation, such as a Redis Lua script, so two requests cannot spend the same token. Expire idle records to bound memory use, but not before the bucket would have refilled: recreating an empty bucket as full would grant extra capacity.
What interviewers look for
State whether the quota must be strict across regions or can tolerate some overshoot. A shared check adds latency and a dependency; assigning each region part of the budget avoids a cross-region check on every request but can leave capacity unused in quieter regions. Explain what happens when the quota store is unavailable: allow traffic, reject it, or fall back to a conservative local limit. Finally, distinguish request rate from concurrency. Even five requests per second can leave many reports running if each takes minutes, so the service may also need a cap on in-flight work.