KatInterview

Building a Request Throttling Service That Scales Across Regions

System DesignModel answer
MediumModel answerSystem Designrate-limitingdistributed-systemsredis

Last updated

Design a service that decides per caller whether each API request is allowed, then scale it to many users and regions and make its limits more accurate, discussing the trade-offs.

Design a throttling service that checks every incoming API request and decides whether the caller who sent it is still within its permitted request quota. Requests over quota are rejected.

Each request carries a caller identifier, such as a user ID or API key. Traffic volume, caller count, quota sizes and latency budget are not specified, so state and justify your own assumptions. You may also ask clarifying questions, for example:

  • Is the throttle in a gateway in front of the services, or called by each service?
  • Is the quota the same for all callers, or does it vary by caller, plan or endpoint?
  • If the throttle's storage is unreachable, should requests be allowed or blocked?
  • Is it acceptable for a caller to exceed its quota by a few percent?

Answer in three parts.

Part 1: Basic design. Describe the components of a simple throttling service and trace a single request through them. Cover where the throttle sits, where rules and counters live, how a check reads and updates them, what a rejected client receives, and what operators can observe.

Part 2: Growth. The user base grows a great deal. How do you scale the service, which problems appear, and how do you address them? Consider counters shared across many throttle instances and how they are partitioned, atomic updates under concurrent requests from one caller, very busy callers, the latency added to every request, failure of the counter store, and traffic arriving in several regions.

Part 3: Better accuracy. How would you make the limiting more precise, and what does it cost? Consider the weakness of counting in fixed time buckets, alternative algorithms and their accuracy, memory and per-check cost, and the precision lost through distribution versus the latency and availability price of exact counting.

Hints

Hint 1

A single instance with in-process counters stops working as soon as you have two instances behind a load balancer. Think about where the counter must live and how to update it without a read-then-write race.

Hint 2

Decide the failure policy deliberately: fail open protects availability, fail closed protects the backend. Different endpoints may deserve different answers, and a short local fallback budget is a middle path.

Hint 3

Fixed buckets allow up to twice the limit around a boundary. Compare sliding-window log, sliding-window counter and token bucket on memory per caller, cost per check and accuracy, then ask how much exactness the use case really needs.