Distributed Rate Limiting
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2023-11-29
Problem Statement
Design a Distributed Rate Limiting System that can enforce request limits across multiple instances of a service in a scalable and consistent manner. The system should prevent clients from exceeding their allowed number of requests per time window while supporting horizontal scaling and high availability.
Functional Requirements (FR)
1.Request Limiting
•Enforce limits on requests per client/user/IP globally across multiple service instances.
•Support various strategies: Fixed Window, Sliding Window, Token Bucket, Leaky Bucket.
2.Flexible Policies
•Different limits per client, endpoint, or subscription plan.
•Support dynamic updates to rate limiting rules.
3.Throttling & Enforcement
•Reject or delay requests that exceed limits.
•Optionally, send warnings when nearing the limit.
4.Metrics & Monitoring
•Track requests per client in real-time.
•Provide dashboards or APIs for usage stats.
5.High Availability
•No single point of failure; system should continue enforcing limits even if some nodes fail.
Non-Functional Requirements (NFR)
•Scalability – Handle millions of requests per second across distributed services.
•Low Latency – Rate checks must be fast, ideally under a few milliseconds.
•Consistency – Ensure accurate rate limiting across distributed nodes.
•Fault Tolerance – System should gracefully handle node failures.
•Extensibility – Easy to add new rate limiting strategies or client policies.
Components & Architecture
1.API Gateway / Proxy
•Intercepts requests and enforces rate limits before forwarding to backend services.
2.Rate Limiter Service
•Stateless or lightweight service that checks request counts and applies policies.
•Uses distributed data store or cache for shared counters.
3.Distributed Counter Store
•Centralized or distributed storage for maintaining request counts (e.g., Redis, DynamoDB, Cassandra).
•Supports atomic increments and expiry for time windows.
4.Policy Management Service
•Manages rate limit configurations per client or endpoint.
•Provides APIs to update limits dynamically.
5.Monitoring & Analytics
•Collect metrics such as total requests, throttled requests, and active clients.
•Dashboards for visualization and alerts.
Data Flow
1.Client sends a request → API Gateway.
2.Gateway calls Rate Limiter Service → checks counters in Distributed Store.
3.Rate limit is enforced:
•If allowed → forward to backend service.
•If exceeded → reject or delay request.
4.Metrics collected → Monitoring Service.
Design Considerations
1.Consistency Models
•Strong consistency (atomic increments in Redis or distributed locks) vs eventual consistency (may allow slight over-limit bursts).
2.Data Structures
•Counters with expiry per client/endpoint.
•Sliding window logs for accurate request tracking.
3.Scaling
•Partition counters by client ID or API key.
•Use consistent hashing for distributing load across multiple Redis nodes.
4.Fault Tolerance
•Use replication in Redis or quorum writes in distributed DBs.
•Graceful degradation: allow soft limits when store is temporarily unavailable.
5.Trade-offs
•Accuracy vs performance: precise sliding window vs approximate counting.
•Latency vs consistency: atomic counters vs cached counters.
✅ This HLD problem tests:
•Understanding of distributed systems and consistency models
•Ability to design scalable and fault-tolerant services
•Knowledge of caching, atomic operations, and partitioning strategies
•Planning for monitoring, metrics, and operational concerns