KatInterview

Build a Distributed API Rate Limiter with Per-User Quotas

System DesignModel answer
MediumModel answerSystem Designrate-limitingdistributed-systemscaching

Last updated

Design a low-latency distributed rate limiter that enforces different token budgets for different users across a fleet of stateless API gateway nodes.

The Problem

You're building the rate-limiting layer for a large API platform. The wrinkle that shapes the whole design: every user can have a different quota. A free-tier user might be allowed 100 tokens per second. A premium enterprise customer might get 10,000. Each request can also cost more than one token — a lightweight read might cost 1, while a compute-heavy export might cost 50.

Your system must decide allow or deny on every incoming request, add minimal latency to the critical path, and enforce limits correctly even when requests from the same user land on different gateway machines.

Walk through your design end to end: the algorithm you'd use, where quota definitions live and how they reach the enforcement point, how live counter state is stored and updated atomically, the full request-time decision flow, and how the system handles scale and partial failures.

Constraints and assumptions

  • Tens of millions of registered users. Most fall into a small set of tiers (free, pro, enterprise). A few thousand have per-user quota overrides.
  • Peak load is in the range of hundreds of thousands to roughly 1 million rate-limit checks per second across the entire fleet.
  • The rate-limit check must add no more than 1–2 ms to request latency.
  • Quotas are expressed as tokens per second; each request specifies how many tokens it costs.
  • Quota changes (e.g., a user upgrading their plan, an operator applying a manual override) must take effect within seconds, with no redeploy.
  • The API gateway is stateless and horizontally scaled; any node can receive any user's request.

Clarifying questions worth raising

Before designing, discuss these with your interviewer:

  • Is the limit a hard guarantee, or is small bounded over-admission acceptable in exchange for lower latency?
  • What should the system do when the backing counter store is unreachable — admit traffic (fail open) or reject it (fail closed)?
  • Should users be allowed short bursts above their sustained rate, or should the rate be smoothed strictly per second?
  • Is this enforced at a shared API gateway, as a per-service sidecar, or inside each microservice?
  • Is enforcement single-region, or must a user's quota be shared across multiple geographic regions?
  • What does a rejected caller receive — an HTTP 429 with retry guidance, a queue, or gracefully degraded service?

What's expected

Explain your algorithm choice and why it fits variable costs and burst semantics. Describe the separation between quota configuration and live counter state, and how each reaches the enforcement path. Provide rough numbers to justify your shard count and memory estimates. Address atomicity, clock consistency, hot keys, and failure handling. Call out the trade-offs you're making.

Hints

Hint 1

Think of per-user quotas as parameters to a single algorithm, not as separate algorithms per user. Which algorithm naturally accepts a per-user rate and capacity, handles variable request costs with no extra logic, and keeps only a tiny amount of state per user?

Hint 2

Split the problem into two distinct planes: one that holds quota definitions (slow-changing, read-constantly) and one that holds live counter state (fast-changing, needs atomic updates). Design each plane independently, then connect them at request time.

Hint 3

A Redis round trip on the hot path costs roughly 0.2–0.5 ms in-region, which fits in your 1–2 ms budget. But what happens when a single high-throughput user hammers one shard? And what happens when Redis is temporarily unreachable? Have a concrete answer for both before you call the design done.