Sub topic 16
Layering limits: per-second bursts, per-day quotas, global caps
This chapter assumes you know the fixed window and token bucket algorithms from earlier chapters, how a limiter key is chosen, and how a shared store such as Redis holds counters. A single limit rarely fits real traffic, so production services stack several.
Why one limit is not enough
A limit of 1,000 requests per day does not stop a client from sending all 1,000 in one second. A limit of 10 requests per second still allows 864,000 requests per day. Each limit protects against a different problem:
| Tier | Typical window | What it protects |
|---|---|---|
| Burst | 1 second | Server CPU, connection pools, downstream calls |
| Sustained | 1 minute | Fair sharing between clients |
| Quota | 1 day or 1 month | Cost, billing plans, abuse |
| Global cap | whole service | Total capacity, regardless of who sends traffic |
How the tiers combine
A request must pass every tier. If any tier rejects it, the request is rejected. The tiers can use different algorithms and different keys. A token bucket suits the burst tier. A fixed window counter suits a daily quota, because the boundary burst problem matters little over 24 hours. A global cap uses a constant key such as global, so every client shares one counter.
A layered limiter in Python
This example reuses the idea of a fixed window counter and a token bucket, and it checks both tiers for one client. It takes the time as an argument so you can test it without waiting.
class Tier:
"""Fixed window counter for one tier."""
def __init__(self, limit, window):
self.limit = limit
self.window = window
self.counters = {} # key -> (window_index, count)
def peek(self, key, now):
index = int(now // self.window)
window_index, count = self.counters.get(key, (index, 0))
if window_index != index:
count = 0
return count < self.limit
def record(self, key, now):
index = int(now // self.window)
window_index, count = self.counters.get(key, (index, 0))
if window_index != index:
count = 0
self.counters[key] = (index, count + 1)
class LayeredLimiter:
def __init__(self, tiers):
self.tiers = tiers # list of (name, Tier, key_function)
def allow(self, client, now):
keys = [(name, tier, fn(client)) for name, tier, fn in self.tiers]
for name, tier, key in keys:
if not tier.peek(key, now):
return False, name
for name, tier, key in keys:
tier.record(key, now)
return True, None
limiter = LayeredLimiter([
("burst", Tier(limit=2, window=1), lambda c: c),
("daily", Tier(limit=5, window=86400), lambda c: c),
("global", Tier(limit=100, window=1), lambda c: "global"),
])
for i in range(3):
print(limiter.allow("alice", now=1000.0))The output is (True, None), (True, None), then (False, 'burst').
Common errors
- Charging a tier for a rejected request. The limiter above checks all tiers first and records only if all pass. If it recorded as it went, a request denied by the global cap would still use up the client's daily quota.
- Reporting the wrong limit. Tell the client which tier rejected it. A burst rejection means "retry in a second". A quota rejection may mean "retry tomorrow". Set
Retry-Afterfrom the tier that denied. - Hot global key. Every request touches one counter, so it becomes a bottleneck in a distributed store. Split it into several shards with a share of the cap each, or accept an approximate value.
- Race between check and record. In a distributed limiter, run the peek-then-record sequence in one atomic Redis script.
Exercise
A client has a burst limit of 10 per second and a daily quota of 1,000. The client sends 10 requests per second for 200 seconds. What happens, and which tier rejects the requests?
Show answer
The burst tier allows all of them, because 10 per second is within its limit. After 100 seconds the client has used 1,000 requests, so the daily quota tier rejects every later request. The client should get a 429 whose Retry-After points to the start of the next quota window, not one second.