KatInterview

Rate-Limiting Service with Token Buckets at Global Scale

System DesignModel answer
MediumModel answerSystem Designrate-limitingredisdistributed-systems

Last updated

Design a distributed rate limiter that enforces per-user request quotas across multiple regions with low latency and no single point of failure.

Scenario

Your team operates a public API gateway that serves traffic from clients around the world. Product wants to enforce per-user request quotas — for example, "a Free-tier user may make at most 100 requests per minute" — to protect backend services and enforce monetization tiers. You need to design the rate-limiting service that sits in the request path to make the allow/deny decision.

The gateway runs on dozens of application servers spread across several geographic regions. Any rate-limiting solution must work correctly even when many servers handle requests from the same user concurrently.

Functional Requirements

  • Enforce a configurable request quota per user ID (requests per time window).
  • Expose a check interface the gateway calls on every incoming request.
  • Support multiple user tiers, each with its own quota (e.g., Free, Pro, Enterprise).

Non-Functional Requirements

  • Latency: the rate-check call should add under 10 ms to the request path (p99).
  • Availability: a failure in the rate-limiting layer must not take down the main API.
  • Accuracy: small, bounded overshoot is tolerable; large-scale limit violations are not.

What Your Design Should Cover

  1. API — what does the interface between the gateway and the rate limiter look like?
  2. Storage strategy — centralized, local, or hybrid? Justify your choice.
  3. State management — how is the token-bucket state (token count and last-refill timestamp) stored and updated?
  4. Concurrency — how do you prevent race conditions when multiple servers update the same bucket simultaneously?
  5. Fault tolerance — what happens when a storage node or an entire region is unavailable?

Feel free to ask clarifying questions before diving in. State your assumptions, and call out the trade-offs in your choices.

Hints

Hint 1

Start by sizing the problem: estimate requests per second, number of active users, and bucket state size. These numbers dictate whether a single Redis node, a cluster, or a more complex topology is needed.

Hint 2

Think through the read-modify-write race condition carefully. What primitive guarantees atomicity for a multi-field update in Redis without introducing lock contention on hot keys?

Hint 3

Global consistency (one shared counter across all regions) and low latency are fundamentally at odds. Enumerate the options — home-region routing, split quotas, async gossip — and state the accuracy bound each one gives you.