KatInterview

Building a Concurrent Per-Client Throttling Service at Scale

System DesignModel answer
MediumModel answerSystem Designrate-limitingtoken-bucketconcurrency

Last updated

Design a throttling component that caps each client's request rate under heavy concurrency and millions of checks per second, then extend it across many servers.

Your team runs an API platform and needs a component that throttles traffic so no single client can overload the backend. Design it, and sketch the core implementation.

Cover the following points:

  1. Per-client quota. Enforce a cap for each caller, identified by user ID, IP address, or API key, for example a fixed number of requests per second.
  2. Concurrency safety. Many worker threads will call the limiter at once. Per-client state must never be corrupted, and the limiter must never admit more traffic than the quota allows because of races.
  3. Throughput. The component must handle millions of checks per second with very low added latency. Identify the bottlenecks and how you would remove them.
  4. Algorithm. Choose a throttling algorithm (token bucket, leaky bucket, fixed window, sliding window, etc.). State its burst behavior, accuracy and fairness, and what you give up compared with the alternatives.
  5. State layout. Describe what you store for each client and in which data structures, including how memory stays bounded.
  6. Denied requests. Say what the caller experiences when a request is over quota (an error status, a retry hint, queuing or delaying).
  7. Multi-instance deployment. Explain how the design changes when the service runs on many machines and any one client's requests can reach any of them.

Before designing, list the clarifying questions you would ask: what the limit is scoped to (user, IP, route, or global), whether bursts are acceptable or the rate must be strict, single node versus distributed, exact versus approximate counting, and what should happen when the limiter itself fails.

Hints

Hint 1

Start by writing down the assumptions (key scope, burst tolerance, exact vs approximate, failure mode). Each one pushes you toward a different algorithm and architecture.

Hint 2

Pick an algorithm whose per-client state is just a couple of numbers and that refills lazily on each check, so no background timer is needed. Then ask what the read-modify-write race looks like and how to make it atomic cheaply (lock striping, CAS).

Hint 3

For scale-out, compare three options: a central atomic store, routing each key to one owner node, and local enforcement with periodic syncing or token leasing. Weigh accuracy against added latency and availability.