Token Bucket vs Leaky Bucket
What You'll Learn
You'll learn the key differences between token bucket and leaky bucket algorithms, specifically how they handle bursts, smooth traffic, and when to choose each.
Comparing the Two Algorithms
Both algorithms control request rates, but they handle traffic patterns very differently.
Burst Handling
Token bucket allows bursts up to the bucket capacity. If you've accumulated 100 tokens, you can process 100 requests instantly—perfect for handling legitimate traffic spikes. Think of it like a bank account that lets you spend your savings all at once when needed.
Leaky bucket prevents bursts entirely. Requests flow out at a constant rate regardless of how many are queued. It's like a funnel with a fixed-size hole at the bottom—no matter how fast you pour water in, it drips out at the same steady pace.
Traffic Smoothing
Token bucket produces variable output rates. When tokens are available, requests go through immediately; when empty, requests wait. Output traffic mirrors the input (up to limits).
Leaky bucket produces perfectly uniform output. Requests are processed at exactly the configured rate, creating smooth, predictable traffic—ideal for protecting downstream services that can't handle variability.
Use Cases
Choose token bucket when:
- You need to allow reasonable bursts (user uploads multiple files)
- Clients benefit from immediate responses when capacity exists
- Downstream systems can handle variable load
Choose leaky bucket when:
- You must guarantee smooth, constant traffic flow
- Protecting fragile downstream services
- Network bandwidth management requires strict consistency
- You want to prevent any possibility of overload
Key Takeaway: Token bucket favors flexibility and allows bursts while maintaining average rate limits; leaky bucket enforces strict traffic smoothing at the cost of user experience during bursts. Choose based on whether burst tolerance or output consistency matters more to your system.