Design the core rate limiting algorithms and their integration in a distributed API gateway or service. Out of scope: detailed user authentication, billing, or analytics.
Functional Requirements
FR1: Limit the number of requests a user or client can make in a given time window
FR2: Support burst traffic without dropping all requests immediately
FR3: Prevent system overload by smoothing request rates
FR4: Provide configurable limits per user or API key
FR5: Handle millions of requests per second across distributed servers
FR6: Ensure fairness and prevent abuse
Non-Functional Requirements
NFR1: Latency impact on request processing must be minimal (p99 < 10ms)
NFR2: System must be highly available (99.9% uptime)
NFR3: Rate limits must be consistent across distributed instances
NFR4: Support horizontal scaling to handle increasing traffic
NFR5: Memory usage per user/client must be efficient
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
Key Components
API Gateway or Load Balancer
Rate Limiter module implementing token bucket or leaky bucket
Distributed cache or in-memory store (e.g., Redis) for counters
Configuration service for rate limit policies
Monitoring and alerting system
Design Patterns
Token Bucket algorithm for burst handling
Leaky Bucket algorithm for smoothing traffic
Distributed counters with atomic increments
Sliding window rate limiting
Client-side vs server-side rate limiting
Reference Architecture
Client --> API Gateway --> Rate Limiter --> Backend Service
| |
| v
| Distributed Cache
v
Configuration Service
Components
API Gateway
Nginx or Envoy proxy
Entry point for client requests, routes traffic, and enforces rate limits
Rate Limiter
Custom module or middleware
Implements token bucket or leaky bucket algorithm to allow or reject requests
Distributed Cache
Redis with atomic commands
Stores counters and tokens per user/client for distributed consistency
Configuration Service
Central config store (e.g., Consul, etcd)
Manages rate limit policies and parameters
Request Flow
1. Client sends request to API Gateway
2. API Gateway forwards request to Rate Limiter module
3. Rate Limiter fetches current token count or leak state from Distributed Cache
4. Rate Limiter applies token bucket or leaky bucket logic:
5. - Token Bucket: refill tokens at fixed rate, consume token per request
6. - Leaky Bucket: requests enter queue, leak out at fixed rate
7. If tokens available or bucket not full, allow request and update cache
8. If limit exceeded, reject request with HTTP 429 Too Many Requests
9. Allowed requests proceed to Backend Service
10. Configuration Service updates rate limit parameters dynamically
Database Schema
Entities:
- User or Client: id, rate_limit_policy_id
- RateLimitPolicy: id, max_tokens, refill_rate, bucket_capacity, algorithm_type
- TokenBucketState: user_id, tokens, last_refill_timestamp
- LeakyBucketState: user_id, queue_size, last_leak_timestamp
Relationships:
- User references RateLimitPolicy
- TokenBucketState and LeakyBucketState track per-user algorithm state
Scaling Discussion
Bottlenecks
Distributed cache becoming a single point of failure or bottleneck
High latency due to frequent cache reads/writes for counters
Inconsistent rate limiting state across distributed nodes
Memory overhead for storing state of millions of users
3. Consider a token bucket with a capacity of 5 tokens and a refill rate of 1 token per second. Initially, the bucket is full. If 7 requests arrive instantly, how many requests will be allowed immediately?
medium
A. 5
B. 7
C. 1
D. 0
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.
Hint: Max allowed = tokens in bucket at request time [OK]
Common Mistakes:
Assuming all 7 requests allowed instantly
Ignoring token bucket capacity
Confusing refill rate with immediate tokens
4. A developer implemented a leaky bucket rate limiter but notices that bursts are still allowed. What is the most likely cause?
medium
A. The bucket capacity is set too low
B. Requests are dequeued faster than the leak rate
C. Tokens are refilled too quickly
D. The leak rate is set to zero
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 B
Quick Check:
Dequeue rate > leak rate = bursts allowed [OK]
Hint: Leaky bucket bursts mean dequeue faster than leak rate [OK]
Common Mistakes:
Confusing token refill with leaky bucket
Assuming bucket capacity controls bursts
Ignoring dequeue speed vs leak rate
5. You need to design a rate limiter for an API that allows short bursts but enforces an average rate limit over time. Which algorithm should you choose and why?
hard
A. Leaky Bucket, because it smooths out bursts by processing requests steadily
B. Fixed Window Counter, because it counts requests in fixed intervals
C. Sliding Log, because it logs every request timestamp
D. Token Bucket, because it allows bursts by accumulating tokens and enforces average 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 D
Quick Check:
Bursts + average rate = Token Bucket [OK]
Hint: Token bucket = bursts + average rate control [OK]
Common Mistakes:
Choosing leaky bucket which smooths bursts
Confusing fixed window with burst allowance
Ignoring average rate enforcement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.