Rate limiting
A service can cap how often a caller makes requests, such as allowing 60 requests per minute per account. This keeps one caller from using too much capacity and helps protect the service from overload.
Also known as: ограничение частоты запросов, рейт-лимит
Start with the policy
Suppose an API lets each account export five reports per minute. Before starting an export, the service checks whether that account still has room under the limit. Other accounts have their own allowance. The key identifies who shares that allowance: it might be an account ID, an API key, or a combination such as account ID plus route. An IP address can be useful when there is no account, but users behind the same network gateway may share an address and hit the limit together.
Choose how to measure the rate
- Fixed window: count requests in intervals such as calendar minutes. This needs little state, but five requests just before the minute ends and five just after it starts can all pass.
- Sliding window: count requests over the last 60 seconds at the time of each check. Storing individual timestamps makes this exact; grouping counts into smaller time buckets saves memory at the cost of precision.
- Token bucket: refill a balance at a steady rate up to a capacity and charge tokens for each accepted request. The refill rate controls sustained traffic, while the capacity controls how large an immediate burst can be when the bucket is full.
Walk through a burst
Consider a full token bucket that holds 10 tokens and refills at two tokens per second. If requests cost one token each, 10 can pass immediately. An eleventh request arriving at the same instant is rejected, but after half a second one token is available again if no other request has taken it. An HTTP API typically returns 429 Too Many Requests when the caller exceeds its limit. It may send Retry-After: 1 to ask the caller to wait one second before trying again; this does not reserve a token for that retry.
Put the check where it saves work
An API gateway can reject excess traffic before it reaches the application. Application middleware is useful when the limit depends on details such as the authenticated customer or the cost of an operation. On several application instances, independent in-memory counters enforce a limit per instance, not one shared across the fleet. A shared store such as Redis can enforce a common budget, provided the check and update are atomic. Otherwise, two workers may both see the last token and admit a request.
Explain the trade-offs in an interview
Clarify whether the system should allow bursts, how strict the limit must be, and what happens if the limiter's store is unavailable. Letting requests through favors availability but leaves the backend less protected; rejecting them preserves protection but may block legitimate traffic. Also distinguish request rate from concurrency: a service can admit only 10 requests per second and still accumulate many in-flight requests if each takes a long time. A separate concurrency limit protects resources such as worker or connection pools.