KatInterview

Design a Scalable API Rate Limiter

System DesignModel answer
MediumModel answerSystem Designrate-limiterdistributed-systemsapi-gateway

Last updated

Architect a high-performance, distributed rate limiting system for an API gateway handling high-throughput, bursty traffic.

Design a distributed rate limiting system integrated into an API gateway layer.

System Requirements & Constraints

  1. Multi-dimensional Limiting: Support rate limits based on arbitrary keys (e.g., API key, user ID, client IP, or composite routes like user_id + endpoint).
  2. Flexible Rules: Support multiple concurrent limits per key (e.g., short-term burst limits like 50 requests/second alongside long-term quotas like 100,000 requests/day), with configuration varying by customer tier and endpoint.
  3. High Performance & Low Latency: Add no more than 1–2 ms of $p99$ latency overhead at a scale of 1M requests per second across a cluster of API gateways.
  4. Distributed Concurrency: Maintain strict correctness and thread safety across multiple stateless gateway instances without race conditions or over-admission during traffic spikes.
  5. Fault Tolerance & Reliability: Fail gracefully if the underlying storage tier (e.g., memory cache) suffers high latency or outages.
  6. Client Feedback: Provide informative HTTP headers and status codes on limit enforcement according to standard specifications.

Walk through your architecture, data structures, algorithms, concurrency management strategy, operational trade-offs, and failure handling mechanisms.

Hints

Hint 1

Consider how algorithm selection affects state memory footprint at scale (e.g., Token Bucket vs. Sliding Window Log).

Hint 2

Think about atomic operations in distributed caching systems to avoid read-modify-write race conditions.

Hint 3

How would you handle global enterprise keys handling 50k+ requests/sec hitting a single database shard (hot key problem)?