Leaky Bucket (GCRA)
| Token bucket | Leaky bucket | |
|---|---|---|
| Smooths | The long-run average — a saved-up burst goes through all at once | The instantaneous output — a steady drip |
| Good when | Bursts are friendly (a user clicking quickly) | The thing behind you (payment processor, legacy DB) falls over on any spike |
How it works
-- Like the token bucket this runs atomically, so GET -> compute -> SET is a
-- single indivisible step. This is GCRA: no queue is stored, just one timestamp.
local key = KEYS[1] -- bucket key, e.g. "rl:lb:user123"
local rate = tonumber(ARGV[1]) -- requests per second (the leak rate)
local capacity = tonumber(ARGV[2]) -- max queue depth / burst allowance
local now = tonumber(ARGV[3]) -- unix nanoseconds
-- interval_ns = spacing between two requests at the steady leak rate.
-- e.g. rate=10/sec -> one request every 100,000,000 ns (100ms).
local interval_ns = math.floor(1e9 / rate)
-- TAT = Theoretical Arrival Time: the earliest moment the NEXT request may
-- arrive if every request is spaced one interval apart. The only state.
local tat = tonumber(redis.call("GET", key))
if tat == nil then
tat = now -- first request: bucket is empty
end
-- Where TAT would move to if we allow this. max(tat, now) handles an idle
-- caller: the bucket fully drained, so measure forward from "now".
local new_tat = math.max(tat, now) + interval_ns
-- burst_offset = how far ahead of "now" the TAT may run. A full bucket of
-- "capacity" requests sits exactly capacity intervals into the future.
local burst_offset = interval_ns * capacity
local allow_at = new_tat - burst_offset
-- Allow only if that earliest-legal time has already arrived.
local allowed = 0
if allow_at <= now then
redis.call("SET", key, new_tat)
redis.call("PEXPIRE", key, math.ceil(capacity / rate * 1000) + 1000)
allowed = 1
end
-- Remaining = how many more requests fit before the TAT runs too far ahead.
local remaining = math.max(0, math.floor((now + burst_offset - math.max(tat, now)) / interval_ns))
return {allowed, remaining}Walking the TAT forward (worked example)
rate = 1/sec (interval = 1 s), capacity = 3. The bucket absorbs a burst of 3, then enforces one-per-second:
t=0.0s burst of 3 arrives back-to-back:
req1: tat=0 → new_tat=1, allow_at=1-3= -2 ≤ 0 → ALLOWED, tat=1
req2: tat=1 → new_tat=2, allow_at=2-3= -1 ≤ 0 → ALLOWED, tat=2
req3: tat=2 → new_tat=3, allow_at=3-3= 0 ≤ 0 → ALLOWED, tat=3
req4: tat=3 → new_tat=4, allow_at=4-3= 1 > 0 → REJECTED (bucket full)The TAT has now run 3 seconds ahead of now — exactly capacity intervals — so the bucket is full. The fourth request is the overflow that "spills on the floor." Crucially, TAT did not advance on the rejection, so the bucket isn't punished for being probed.
Once full: retries succeed strictly on the leak schedule
Unlike token bucket (where any tiny wait earns fractional credit), leaky bucket frees a slot only when now catches up to TAT − burst_offset:
wait to t=0.5s → allow_at for next req = 4-3 = 1 > 0.5 → still REJECTED
wait to t=1.0s → allow_at = 1 ≤ 1.0 → ALLOWED (one slot leaked out)A full bucket admits exactly one request per 1/rate seconds — the leak is steady no matter how hard the client retries. That constant output rate is the deliberate difference from token bucket.
| Token Bucket | Leaky Bucket | |
|---|---|---|
| Burst allowed | Yes, up to capacity tokens | Limited — TAT smooths output |
| Output rate | Variable (bursty OK) | Constant |
| Use case | Client-facing APIs, user quotas | Protecting downstream (DB, payment processor) |
| Memory / user | O(1) — one int64 timestamp |
|---|---|
| Output rate | Constant — spaced ≥ 1/rate apart |
| Latency | ~1 RTT (Lua script) |
| Best for | Downstream services that need a constant input rate |