KatInterview

Build a Shared Throttling Layer for a Multi-Server Web API

System DesignModel answer
HardModel answerSystem Designrate-limitingredisdistributed-systems

Last updated

Design a throttling service that caps per-key request volume across a fleet of servers, with low added latency, correct behavior under concurrency, and a clear plan for backend failures.

Design a Shared Throttling Layer for a Web API

Your company exposes a public HTTP API served by many identical application servers behind a load balancer. You need a throttling component that limits how many calls each client can make in a time window, for example 100 calls per minute per API key.

What the system must do

  • Apply limits per API key.
  • Allow different limits for different endpoints (for example, a write endpoint may be much stricter than a read endpoint).
  • Reject over-limit calls with HTTP 429 Too Many Requests.

Quality targets

  • Latency: the check should add very little to each request (aim for under 10 ms).
  • Correctness under concurrency: simultaneous calls from one client, landing on different servers, must be counted correctly.
  • Scale: thousands of requests per second across the whole cluster.
  • Resilience: if the throttling component has trouble, the API must not go down entirely. Decide whether to fail open or fail closed, and when.

What to cover in your answer

  1. The interface between application code and the throttler, plus what clients see in responses.
  2. The storage backend for counters or timestamps, and why you picked it.
  3. The high-level architecture: how app servers talk to the throttler and where enforcement happens.
  4. How you prevent race conditions when several nodes handle requests for the same client at once.

State your assumptions about traffic and key counts, compare at least two counting algorithms, and discuss failure behavior and trade-offs.

Hints

Hint 1

Start by estimating scale (requests per second, active keys, memory per key) so your storage and latency choices are backed by numbers.

Hint 2

Think about the check-then-act bug: two servers read the same count, both see room, and both let the request through. What single atomic operation avoids it?

Hint 3

Compare fixed window, sliding log, sliding window counter, and token bucket on memory use and accuracy, then pick one and justify it.

Hint 4

Decide separately for each rule what happens when the store is unreachable. A login or payment endpoint may deserve different behavior than a search endpoint.