What is the Leaky Bucket Algorithm?
The Kitchen Sink Analogy
Imagine your kitchen sink. Water comes in through the faucet (requests arrive), collects in the basin (the bucket), and drains at a fixed rate of 1 liter/sec. Pour in 3 liters in one second and 2 liters remain waiting to drain. Keep pouring faster than 1 liter/sec and eventually the basin overflows (requests get rejected).
The Key Insight: Traffic Smoothing
No matter how chaotically water arrives, it always leaves through the drain at the same steady rate. Leaky Bucket transforms spiky input (1,000 requests in 100ms, then nothing) into smooth output (100 requests per 100ms, steady). Downstream systems see constant flow regardless of upstream chaos.
Token Bucket vs Leaky Bucket
Token Bucket asks: "Do you have permission to proceed right now?" and lets bursts through immediately. A user with 500 tokens sends 500 requests in 10ms. Leaky Bucket asks: "Can I smooth this?" and queues for steady release. Those 500 requests at 100/sec leak rate take 5 seconds. Token Bucket prioritizes responsiveness; Leaky Bucket prioritizes downstream protection.
When to Reach for Leaky Bucket
Use it when downstream has nonlinear degradation. Databases often exhibit this: at 1,000 QPS latency is 10ms, at 1,500 QPS jumps to 100ms, at 2,000 QPS queries pile up and recovery takes 5 to 10 minutes. A 2x spike for 1 second can trigger a 10 minute outage. Leaky Bucket prevents that spike from reaching the database.
The Trade-off: Latency for Stability
Requests entering the queue experience delay. With a 100 item queue draining at 100/sec, a request at the back waits 1 second before processing. For user facing APIs, this may be unacceptable. For background jobs or database writes, the latency is worthwhile for stability.