KatInterview

Design a Rate Limiting Service for a Multi-Region API Platform

System DesignModel answer
MediumModel answerSystem Designrate-limitingdistributed-systemsredis

Last updated

Design a low-latency, distributed rate limiter that enforces per-key request quotas across a fleet of servers handling millions of requests per second.

Rate Limiting Service for a Multi-Region API Platform

Your platform exposes APIs consumed by millions of clients. Without traffic controls, a single misbehaving client can starve everyone else. Your job is to design a service that enforces quotas such as "500 requests per minute per API key" across a horizontally scaled gateway layer.

Functional requirements

  • A caller presents an identifier (e.g., api_key, user_id, or a composite like tenant_id + endpoint) and the system decides: allow or deny.
  • Limits are configurable per tier (e.g., free = 60/min, pro = 1,000/min) and can be overridden per key.
  • Denied requests receive enough metadata to know when to retry.

Non-functional requirements

  • Latency: the rate-limiting decision must add less than 10 ms at p99.
  • Scale: the system must sustain millions of requests per second spread across three or more geographic regions.
  • Availability: a failure in the rate-limiting layer must never take down the API itself.
  • Accuracy: a small overshoot (roughly 1–5%) is acceptable in the common case; some limit categories (e.g., authentication attempts) require strict enforcement.

What to cover

  1. Client-facing API — what does the gateway call, and what does it return?
  2. Algorithm selection — compare at least two approaches and justify your choice, including memory and accuracy trade-offs.
  3. Storage and architecture — where does per-key state live, how is it sharded, and how does the gateway access it within the latency budget?
  4. Atomicity — how do you prevent two gateway nodes from both approving a request when only one token remains?
  5. Clock skew — how do you ensure refill calculations are consistent across nodes?
  6. Failure modes — what happens when the backing store is unreachable?
  7. Cross-region enforcement — how would you handle a user who hammers the API from two continents simultaneously?

Hints

Hint 1

Start by estimating the per-key state size and total memory footprint. 100 million active keys at ~100 bytes each is only about 10 GB — that changes which storage options are viable.

Hint 2

A naive GET → compute → SET is a race condition the moment two gateway nodes act on the same key concurrently. Think about what Redis primitive lets you make that whole sequence atomic without a distributed lock.

Hint 3

A central store that every request must hit is a single point of latency. Consider how a local in-process cache on each gateway node can absorb already-denied traffic before it ever reaches Redis.