Design Rate Limiter

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-04-18

Problem Statement

You are tasked with designing a Rate Limiter Service that restricts the number of requests a client (user, IP, or API key) can make to a system within a given time window. This is critical for preventing abuse, ensuring fair usage, and maintaining system stability under load.


✅ Functional Requirements


1.Limit API calls per client
•Example: Allow max 100 requests per user per minute.
•Configurable per API, per client, or per IP.
2.Different Algorithms Support
•Fixed Window Counter
•Sliding Window
•Leaky Bucket
•Token Bucket
3.Multi-tenancy
•Different clients can have different rate limits (premium vs free tier).
4.Distributed Deployment
•Must work across multiple application servers consistently.
5.Observability
•Track rate-limiting decisions (allowed/blocked).
•Provide metrics & dashboards (per client, per API).


❌ Non-Functional Requirements


•Low Latency: Decision must be made in microseconds–milliseconds.
•Scalability: Should handle millions of requests/sec.
•Fault Tolerance: Fail-safe mode (default allow/deny).
•Accuracy vs Performance Trade-off: Sliding window more accurate but costlier.
•Consistency: Strong consistency in counters (especially in distributed environments).


🧱 Core Components


1.Rate Limiter Library/Service
•API: isAllowed(clientId, apiName) → true/false.
2.State Store
•Fast key-value store (Redis, Memcached, in-memory).
•Stores counters, tokens, or buckets per client.
3.Algorithm Engine
•Implements chosen strategy (Token Bucket, Leaky Bucket, etc.).
4.Config Manager
•Stores per-client and per-API rate limit rules.
5.Metrics & Logging
•Expose Prometheus/Grafana dashboards for usage & throttling.
6.Distributed Coordination
•Use Redis atomic operations, or sharded consistent hashing.
•Optionally, use consensus protocols (e.g., Zookeeper/etcd) for global consistency.


🔄 Flow Example (Token Bucket)


1.A client sends a request.
2.RateLimiter fetches the token bucket for the client from Redis.
3.If ≥1 token → decrement bucket & allow request.
4.If 0 tokens → reject request with 429 Too Many Requests.
5.Tokens are refilled periodically at a fixed rate.


⚖️ Trade-offs to Discuss


•Accuracy vs Performance
•Fixed window is cheap but bursty.
•Sliding window smooths spikes but costly.
•Centralized vs Distributed
•Centralized Redis = consistent but adds network latency.
•Local + Sync = faster but risk of inconsistency.
•Enforcement Strategy
•Hard limit (block immediately).
•Soft limit (queue/delay requests).
•Scalability
•Sharding counters across Redis clusters.
•Pushdown enforcement to edge proxies (e.g., API Gateway, CDN).


🧪 Open Discussion Points


•Should this be implemented as an API Gateway plugin (e.g., Nginx, Envoy) or as a shared service?
•How to support global vs regional rate limits?
•How to integrate burst allowance for better UX?
•Should the system support dynamic reconfiguration of limits without downtime?


👉 This is a classic HLD problem where interviewers want you to talk about algorithms (token bucket, sliding window), distributed counters, consistency, and trade-offs.

Solution

Comments