Skip to content

Rate limiting

A rate limiter caps how many requests a client may make in a period of time and rejects or delays the rest. It keeps one noisy client from using up shared capacity, slows down abuse such as credential stuffing and scraping, keeps costs predictable and enforces pricing tiers. Almost every public API has one, and “design a rate limiter” is a classic interview question.

Rate limits do four jobs, and each job points to a different place in the stack:

GoalTypical keyWhere
Absorb floods and abuseIP address, IPv6 /64 prefix, ASNCDN, WAF, edge proxy
Fair use and pricing tiersAPI key, user, tenantAPI gateway
Protect a fragile resourceEndpoint, tenant, or nothing (global)Inside the service, near the resource
Stay within someone else’s quotaYour own credentialsClient side, before calling a third-party API
flowchart TB
  user["Clients"] --> edge["Edge / WAF: per IP"]
  edge --> gw["API gateway:<br/>per API key and plan"]
  gw --> svc["Service: concurrency limit"]
  svc --> db[("Database")]
  svc --> ext["Third-party API:<br/>client-side limit"]

Choosing the key needs care. Many users share one IPv4 address behind carrier-grade NAT or a corporate proxy, while one IPv6 client controls billions of addresses, so limit IPv6 by prefix. Authenticated keys (user, API key, tenant) are fairer but only exist after authentication, so login endpoints also need limits per IP and per target account.

Rate limits also differ by cost. One search may cost 100 times as much as a profile read. Charge different numbers of tokens per endpoint, or keep separate limits for expensive operations. Stripe, for example, combines a request-rate limiter with a concurrency limiter, which caps requests in flight per user, and two load shedders that protect critical traffic when the fleet is short of capacity.

A bucket holds up to b tokens and refills at r tokens per second. Each request takes a token. When the bucket is empty, the request is rejected, or delayed until a token arrives. Over any interval t, at most b + r·t requests pass. Bursts are allowed up to b, and the long-run average is r. You store only the token count and the time of the last refill, and refill lazily on each request. Envoy’s local rate limiter and Stripe’s API limits both use it.

There are two variants. As a queue, requests enter a fixed-size FIFO that drains at a constant rate. Output is perfectly smooth, but bursts wait and latency grows. Nginx’s limit_req works this way: burst sets the queue size, and nodelay serves queued requests immediately while still counting them. As a meter (GCRA, the generic cell rate algorithm), it stores one timestamp per key, the “theoretical arrival time”, and behaves like a token bucket.

Count requests per key in calendar windows (user42:2026-10-08T10:15), and reset when the window rolls over. It is one INCR per request and trivially cheap. The flaw is the boundary: with a limit of 100 per minute, a client can send 100 requests at 10:15:59 and 100 more at 10:16:00, which is 200 in two seconds.

Store the timestamp of every accepted request, for example in a Redis sorted set. On each request, drop entries older than the window and count the rest. It is exact, but memory grows with the limit: with 1 million active keys and a limit of 1,000 per window, that is a billion timestamps, about 8 GB before any data-structure overhead.

Keep fixed-window counters for the current and previous windows, and weight the previous one by how much of it still overlaps the sliding window:

estimate = previous_count × (window − elapsed) / window + current_count

Limit 100 per minute, 15 s into the current minute, previous minute had 84 requests, current has 36: 84 × 45/60 + 36 = 99, so one more request is allowed. The estimate assumes requests in the previous window were spread evenly. Cloudflare measured this method on 400 million requests and found only 0.003% wrongly allowed or limited.

AlgorithmMemory per keyBurstsAccuracyGood for
Token bucket2 numbersUp to bucket size, by designExact for its modelDefault for APIs
Leaky bucket (queue)Queue of requestsQueued, output smoothedExactSmoothing traffic to a fragile backend
Leaky bucket (GCRA)1 timestampUp to a toleranceExactSame as token bucket, less state
Fixed window1 counterUp to 2× at boundariesCoarseDaily or monthly quotas, billing
Sliding log1 entry per requestNone beyond the limitExactLow limits where precision matters (logins)
Sliding-window counter2 countersSmall overshoot possibleApproximate, usually within a few %High-volume per-key limits
token_bucket.py
import threading
import time
from dataclasses import dataclass, field
@dataclass
class TokenBucket:
"""Allow `rate` requests per second on average, with bursts up to `capacity`."""
rate: float # tokens added per second
capacity: float # maximum burst size
tokens: float = field(init=False)
updated: float = field(init=False)
lock: threading.Lock = field(default_factory=threading.Lock, repr=False)
def __post_init__(self) -> None:
self.tokens = self.capacity # start full: allow an initial burst
self.updated = time.monotonic()
def try_acquire(self, cost: float = 1.0) -> tuple[bool, float]:
"""Return (allowed, seconds_until_enough_tokens)."""
with self.lock:
now = time.monotonic()
# Refill lazily: no background timer, just elapsed time × rate.
self.tokens = min(self.capacity, self.tokens + (now - self.updated) * self.rate)
self.updated = now
if self.tokens >= cost:
self.tokens -= cost
return True, 0.0
return False, (cost - self.tokens) / self.rate
if __name__ == "__main__":
bucket = TokenBucket(rate=5, capacity=10) # 5 req/s, bursts of 10
burst = [bucket.try_acquire()[0] for _ in range(12)]
print("burst of 12:", burst.count(True), "allowed") # 10 allowed
allowed, wait = bucket.try_acquire()
print(f"next allowed={allowed}, retry after {wait:.2f}s") # ~0.20s
time.sleep(1.0) # refill ~5 tokens
print("after 1 s:", sum(bucket.try_acquire()[0] for _ in range(10)), "allowed") # 5

The second value of try_acquire is exactly what the server should put in Retry-After, rounded up to whole seconds.

With 20 gateway nodes behind a load balancer, a per-node bucket only works if traffic is spread evenly. Giving each node limit / 20 is cheap and needs no coordination, but a client whose connections stick to one node gets a twentieth of its quota. For a precise global limit, keep the state in a shared store such as Redis or Valkey.

The classic bug is check-then-act: GET the counter, compare, then INCR. Two nodes read 99 at the same moment, both allow, and the counter ends at 101. The second classic bug is INCR followed by a separate EXPIRE. If the process dies between the two calls, the key never expires and the client stays blocked for good. Two safe patterns:

Fixed window with a transaction. INCR returns the new value atomically. EXPIRE … NX (Redis 7.0+) sets the TTL only if none exists, so retries cannot extend the window:

MULTI
INCR rl:user42:29857575 # window id = unix_time // 60 (10:15 UTC)
EXPIRE rl:user42:29857575 60 NX
EXEC # allow if the INCR result is <= limit

Token bucket as a Lua script. Redis runs a script atomically, so the read, the refill and the write cannot interleave with another client:

token_bucket.lua
-- KEYS[1]: bucket key, e.g. "rl:{api-key-42}"
-- ARGV[1]: refill rate (tokens/s), ARGV[2]: capacity, ARGV[3]: cost
local rate, capacity, cost = tonumber(ARGV[1]), tonumber(ARGV[2]), tonumber(ARGV[3])
local t = redis.call('TIME') -- one clock for all app servers
local now = tonumber(t[1]) + tonumber(t[2]) / 1000000
local s = redis.call('HMGET', KEYS[1], 'tokens', 'ts')
local tokens = tonumber(s[1]) or capacity
local ts = tonumber(s[2]) or now
tokens = math.min(capacity, tokens + math.max(0, now - ts) * rate)
local allowed = 0
if tokens >= cost then
tokens = tokens - cost
allowed = 1
end
redis.call('HSET', KEYS[1], 'tokens', tokens, 'ts', now)
redis.call('EXPIRE', KEYS[1], math.ceil(capacity / rate) + 1) -- idle buckets vanish
return {allowed, tostring(tokens)}

Using the Redis server’s TIME avoids clock skew between application servers. Load the script once with SCRIPT LOAD and call it by hash with EVALSHA.

Operational questions to settle:

  • Latency. Every request now waits for a round trip to the store, typically well under a millisecond inside one zone. Pipelining or a local pre-check can save it for clients that are far below their limit.
  • Store outage. Decide in advance: fail open (allow traffic, risk overload) or fail closed (reject, risk a self-inflicted outage). Most public APIs fail open with a coarse local limit as a backstop.
  • Hot keys. One huge tenant hammers one shard. Use local token caches that take several tokens from the shared bucket at once and sync periodically, which trades a little accuracy for far fewer round trips. Envoy uses a similar two-stage setup: a local token bucket absorbs bursts, then a global rate-limit service (with a Redis backend in the reference implementation) enforces the shared limit.
  • Multi-region. A single global counter across regions costs a cross-region round trip per request. Usually you give each region a share of the limit, or sync counters asynchronously and accept a bounded overshoot.
  • 429 Too Many Requests (RFC 6585) means this client exceeded its limit. 503 Service Unavailable means the server is overloaded for everyone. Nginx’s limit_req returns 503 by default, so set limit_req_status 429 when the limit is per client.
  • Retry-After (RFC 9110) gives either a number of seconds or an HTTP date. Send it with both 429 and 503.
  • Quota headers. Many APIs send non-standard X-RateLimit-Limit, -Remaining and -Reset headers. GitHub, for example, sends x-ratelimit-* headers and returns 403 or 429 when a limit is exceeded. The IETF httpapi working group is standardising this as RateLimit-Policy and RateLimit structured fields, for example RateLimit-Policy: "default";q=100;w=60 and RateLimit: "default";r=37;t=21. As of October 2026 it is still an Internet-Draft (revision -11, May 2026), and its syntax has changed between revisions. Pin to a version if you adopt it.
  • Make the body useful. Say which limit was hit and link to documentation. Never reveal limits that help an attacker tune a brute-force attack.
  • Shadow mode first. Log what would be rejected before enforcing a new limit. A wrong limit on a payment endpoint is an outage.

Good clients make rate limiting cheap for everyone:

  • Honour Retry-After. Otherwise use exponential backoff with full jitter and a cap on attempts. Never retry a 429 in a tight loop.
  • Throttle yourself. Put a token bucket in the SDK or the worker that calls a third-party API, sized just under your quota, so you queue locally instead of collecting 429s.
  • Spread scheduled work. Ten thousand clients that all sync at minute zero form a self-made thundering herd. Add random offsets.
  • Batch where the API allows it. Treat RateLimit-Remaining-style headers as a hint to slow down before you hit zero.
DecisionOption AOption B
PlacementEdge or gateway: cheap rejection, coarse keysIn-service: knows cost and tenant, runs after work has started
StateLocal per node: fast, approximateShared store: precise, adds a hop and a dependency
Store failureFail open: availability firstFail closed: protection first
Excess requestsReject (429): simple, client retriesQueue or delay: smoother, adds latency and memory
AlgorithmToken bucket: bursts allowedSliding log: strict, more memory

Based on The System Design Primer by Donne Martin (CC BY 4.0). Restructured, rewritten and updated; site content is licensed under CC BY 4.0.