Design a Rate Limiter
AmazonTechnicalDifficulty: Medium
Share on
Ready to answer it out loud?
Run a mock interview on this exact question and get instant AI feedback.
Question Explain
Design a rate limiter that limits the number of requests a client can send to an API within a time window.
Requirements:
- Support different time windows (per second/minute/hour)
- Handle concurrent requests
- Minimal latency
- Scalable across multiple servers
Example: Limit each client to 100 requests per minute.
Answer Example
Rate Limiter System Design:
-
High-Level Design:
- Token Bucket Algorithm
- Redis for distributed rate limiting
- Load balancer for distribution
-
Components:
interface RateLimiter {
isAllowed(clientId: string): boolean;
updateCount(clientId: string): void;
resetCount(clientId: string): void;
}
class TokenBucketRateLimiter implements RateLimiter {
private readonly redis: Redis;
private readonly limit: number;
private readonly window: number;
constructor(limit: number, windowInSeconds: number) {
this.limit = limit;
this.window = windowInSeconds;
}
async isAllowed(clientId: string): Promise<boolean> {
const count = await this.redis.get(clientId);
return count < this.limit;
}
}
-
Implementation Details:
- Use Redis INCR for atomic counters
- Sliding window with Redis ZSET
- Lua scripts for atomic operations
-
Scalability Considerations:
- Horizontal scaling with Redis cluster
- Cache synchronization
- Fault tolerance
-
Monitoring & Alerts:
- Request rate metrics
- Rejection rate alerts
- Latency monitoring
-
Trade-offs:
- Memory vs Accuracy
- Consistency vs Availability
- Complexity vs Performance
Company Context (Amazon):
- Focus on scalability
- Consider distributed systems
- Emphasize operational excellence
- Cost optimization
Related Interview Questions
Knowledge of storage architectures and types
AmazonMedium
Merge Two Sorted ArraysGoogleEasy
Basic String ManipulationAdobe
Investment Portfolio OptimizationGoldman SachsMedium
Can you share an example of using innovative problem-solving skills to overcome a major work challenge?GoogleHard
How would you implement a custom authentication mechanism for a new AEM feature while adhering to AEM's security best practices?PwCHard
What strategies help you solve complex technical problems under pressure?eBayHard
Cybersecurity SolutionsMetaHard