Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How API rate limiting works: token bucket, leaky bucket, fixed and sliding window algorithms, distributed counters with Redis, and 429 responses.
Quick answer: A rate limiter caps how many requests a client can make in a period of time. For each client (identified by API key, user or IP address) it keeps a small piece of state, such as a counter or a bucket of tokens, and checks it on every request. If the client is within its allowance, the request passes. If not, the server rejects it, usually with HTTP status 429 Too Many Requests, and tells the client when to try again. The most common algorithm is the token bucket, which allows short bursts while enforcing an average rate.
Stripe's engineering post Scaling your API with rate limiters describes running several kinds of limiter together for these different purposes.
Divide time into fixed windows, such as each minute, and count requests per window. Reset at the start of the next.
Store the timestamp of every request. On each new request, discard timestamps older than the window and count the rest.
A compromise: keep counters for the current and previous fixed windows and estimate with a weighted sum.
estimate = current_count + previous_count × (share of previous window still in range)
Thirty seconds into the current minute, half of the previous minute's count is included.
Picture a bucket that holds up to N tokens. Tokens are added at a steady rate. Each request removes one token. If the bucket is empty, the request is rejected.
A bucket of 10 refilled at 1 per second lets a client make 10 requests at once after a quiet period, then 1 per second.
No background timer is needed. Store only the token count and the time of the last update, and calculate lazily:
def allow(bucket, now, rate, capacity):
elapsed = now - bucket.last
bucket.tokens = min(capacity, bucket.tokens + elapsed * rate)
bucket.last = now
if bucket.tokens >= 1:
bucket.tokens -= 1
return True
return False
The token bucket is the most widely used algorithm because it matches real usage: clients are bursty, and a short burst is usually harmless.
Requests enter a queue (the bucket) and are processed at a constant rate, like water dripping from a hole. If the queue is full, new requests are dropped.
| Algorithm | Memory per client | Allows bursts | Accuracy |
|---|---|---|---|
| Fixed window | One counter | Yes, up to 2x at boundaries | Low |
| Sliding window log | Every timestamp | No | Exact |
| Sliding window counter | Two counters | Limited | Good |
| Token bucket | Two numbers | Yes, by design | Good |
| Leaky bucket | A queue | No; smooths them | Good |
Most systems use layers: coarse limits at the edge, finer ones in the application.
| Key | Pros | Cons |
|---|---|---|
| API key or user ID | Accurate and fair | Only works for authenticated requests |
| IP address | Works for anonymous traffic | Many users can share one IP behind NAT; attackers rotate IPs |
| Endpoint | Protects expensive operations | Needs per-route configuration |
| Combination | Most precise | More state |
Shared IP addresses are a real issue: a whole office or mobile network can appear as one address. See how NAT works. Login endpoints are usually limited per account and per IP to slow password guessing.
With one server, a counter in memory is enough. With twenty servers behind a load balancer, each would keep its own count and a client could get twenty times the limit.
The usual fix is a shared, fast store, most often Redis. See why Redis is fast.
The subtle part is atomicity. "Read the counter, check it, then increment" is a race: two servers can both read 99 and both allow the hundredth request. Solutions:
INCR with an expiry.Each check adds a network round trip, so high-volume systems sometimes keep local counters and sync with the central store periodically, accepting a little inaccuracy for speed.
Decide in advance what happens if the store is unavailable. Failing open (allowing requests) keeps the service usable; failing closed (rejecting them) is safer for sensitive endpoints.
The standard response is 429 Too Many Requests, defined in RFC 6585, with a Retry-After header:
HTTP/1.1 429 Too Many Requests
Retry-After: 30
RateLimit-Limit: 100
RateLimit-Remaining: 0
RateLimit-Reset: 30
Many APIs also send the limit and remaining allowance on every response, so clients can pace themselves.
When you receive a 429:
Retry-After.Rate limiting is not full protection against large denial-of-service attacks. Those need to be absorbed at the network edge.
You have sent more requests than the server allows in a given time. Wait for the period given in Retry-After, then try again.
A token bucket allows bursts up to the bucket size while enforcing an average rate. A leaky bucket releases requests at a constant rate, smoothing bursts out.
The terms overlap. Rate limiting usually means rejecting excess requests; throttling often means slowing or queuing them.
Keep the counters in a shared store such as Redis and update them atomically, so all servers see the same count.
A rate limiter is a small amount of state and a simple rule applied on every request. Token buckets cover most needs, sliding windows give stricter control, and a shared atomic counter makes it work across a fleet. Pair server-side limits with clear 429 responses and clients that back off politely, and one misbehaving caller stops being everyone's problem.