| Users/Traffic | Token Bucket | Leaky Bucket | System Impact |
|---|---|---|---|
| 100 users | Simple in-memory counters, low memory | Simple queue with fixed leak rate | Works well on single server |
| 10K users | Needs distributed counters or shared cache | Queue size grows, needs distributed state | Single server may struggle with concurrency |
| 1M users | Distributed token buckets with sharding or Redis cluster | Distributed leaky bucket queues with partitioning | State synchronization and latency increase |
| 100M users | Highly scalable distributed caches, approximate counters (e.g. Bloom filters) | Approximate leak rate enforcement, multi-region coordination | Network and storage bottlenecks appear |
Rate limiting algorithms (token bucket, leaky bucket) in HLD - Scalability & System Analysis
The first bottleneck is the state storage for counters or queues that track tokens or leaks.
At low scale, in-memory counters suffice. As users grow, the system must maintain distributed state with low latency.
This state storage (e.g., Redis or distributed cache) can become overwhelmed by high QPS and concurrent updates.
- Horizontal scaling: Add more rate limiter instances behind a load balancer.
- Distributed caching: Use Redis clusters or similar to store counters with sharding.
- Approximate algorithms: Use probabilistic data structures to reduce storage and update costs.
- Local token buckets: Use local counters with periodic sync to reduce latency.
- Rate limit partitioning: Partition users by key (e.g., user ID hash) to spread load.
- Backpressure and queuing: For leaky bucket, use queues with controlled leak rates and backpressure to smooth bursts.
Assuming 1M users, each making 1 request per second:
- Requests per second: 1 million QPS total.
- Each request updates a token bucket counter or leaky bucket queue.
- Redis can handle ~100K ops/sec per instance; need ~10 Redis instances for counters.
- Network bandwidth: 1M QPS * ~200 bytes/request = ~200 MB/s (1.6 Gbps), requires multiple network links.
- Memory: Each token bucket counter ~100 bytes; for 1M users = ~100 MB RAM, feasible in distributed cache.
Start by explaining the rate limiting goal: controlling request rate per user or client.
Describe token bucket and leaky bucket simply, focusing on how they handle bursts and steady rates.
Discuss state storage challenges and how scaling affects latency and consistency.
Outline scaling strategies: horizontal scaling, distributed caches, sharding, and approximate methods.
Use real numbers to show understanding of system limits and trade-offs.
Question: Your database handles 1000 QPS for rate limiting counters. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Introduce a distributed cache like Redis with sharding to offload counters from the database, reducing latency and increasing throughput.
Practice
Solution
Step 1: Understand token bucket behavior
The token bucket algorithm accumulates tokens at a fixed rate, allowing bursts when tokens are available.Step 2: Compare with leaky bucket
The leaky bucket processes requests at a steady rate, smoothing bursts rather than allowing them.Final Answer:
Token Bucket -> Option AQuick Check:
Token Bucket = allows bursts [OK]
- Confusing leaky bucket with token bucket
- Thinking fixed window allows bursts
- Mixing sliding log with token bucket
Solution
Step 1: Recall leaky bucket mechanism
Leaky bucket processes requests at a constant rate, smoothing out bursts by queuing excess requests.Step 2: Eliminate other options
Tokens accumulate and requests consume tokens describes token bucket, C and D describe other rate limiting methods.Final Answer:
Requests are processed at a fixed rate regardless of burst -> Option CQuick Check:
Leaky bucket = fixed rate processing [OK]
- Confusing token accumulation with leaky bucket
- Mixing fixed window counting with leaky bucket
- Assuming leaky bucket allows bursts
Solution
Step 1: Understand token bucket capacity and refill
The bucket starts full with 5 tokens, each request consumes one token.Step 2: Calculate allowed requests
Since 7 requests arrive instantly but only 5 tokens are available, only 5 requests are allowed immediately.Final Answer:
5 -> Option AQuick Check:
Tokens available = 5, requests = 7, allowed = 5 [OK]
- Assuming all 7 requests allowed instantly
- Ignoring token bucket capacity
- Confusing refill rate with immediate tokens
Solution
Step 1: Analyze leaky bucket behavior
Leaky bucket allows steady output by leaking requests at a fixed rate.Step 2: Identify cause of bursts
If requests are dequeued faster than the leak rate, bursts can pass through, breaking the smoothing effect.Final Answer:
Requests are dequeued faster than the leak rate -> Option BQuick Check:
Dequeue rate > leak rate = bursts allowed [OK]
- Confusing token refill with leaky bucket
- Assuming bucket capacity controls bursts
- Ignoring dequeue speed vs leak rate
Solution
Step 1: Understand requirement for bursts and average rate
The system must allow short bursts but keep average rate limited over time.Step 2: Match algorithm to requirement
Token bucket allows bursts by using tokens that refill over time, enforcing average rate while permitting bursts.Final Answer:
Token Bucket, because it allows bursts by accumulating tokens and enforces average rate -> Option DQuick Check:
Bursts + average rate = Token Bucket [OK]
- Choosing leaky bucket which smooths bursts
- Confusing fixed window with burst allowance
- Ignoring average rate enforcement
Ready to prove it?
Pass the test and this pattern moves unknown
