KatInterview

Scalable Multi-Dimensional Throttling for an Inference API

System DesignModel answer
MediumModel answerSystem Designrate-limitingredisdistributed-systems

Last updated

Architect a distributed throttle service for an LLM API that enforces per-org request and token limits with sub-5ms latency at 200k RPS.

Context

You run the platform layer for a large-language-model API. Millions of developers call your inference endpoints, and each call consumes both a "slot" (a request) and a variable amount of compute (tokens). You need a centralized throttling service that sits in the request path of every gateway and decides, in milliseconds, whether a call should proceed or be rejected.

The tricky part is that you don't know the real cost of a request until the model finishes generating. So the limiter must reserve an estimate up front and adjust afterward.

What to Build

A distributed rate-limiting service with the following properties:

  1. Two limit dimensions per caller: requests per minute (RPM) and tokens per minute (TPM), enforced per organization. API keys inherit their org's limits.
  2. Tiered and per-model configuration: quotas differ by customer tier (free, paid tiers) and by model family (e.g. a flagship chat model vs. a lightweight embedding model). Rules must be changeable without redeploying gateways.
  3. Standard client-facing behavior: when a limit is hit, return HTTP 429 with Retry-After, X-RateLimit-Remaining, and X-RateLimit-Reset headers. On success, return remaining quota headers.
  4. Two-phase token accounting: reserve an estimated token count before the model runs, then reconcile the difference once the actual count is known. Handle refunds and overages.

Non-Functional Targets

  • Limiter overhead on the hot path: p99 under 5 ms.
  • Peak load: 200,000 requests/sec across globally distributed gateway nodes.
  • 10 million active API keys, ~1,500 tokens per request on average.
  • The system should favor availability over strict precision: slight over-allowance during failures is acceptable, but blocking legitimate traffic is not.

What Your Answer Should Cover

  • Choice of rate-limiting algorithm and why it fits variable-cost, refundable workloads.
  • High-level architecture: how gateways, the limiter, a shared counter store, and a config store interact.
  • Data model and how atomic multi-dimensional checks happen without distributed locks.
  • The reserve-and-reconcile flow for tokens, including what happens if a gateway crashes mid-request.
  • Failure handling: cache outages, network partitions, clock drift, and cold starts.

Hints

Hint 1

Start by translating the scale numbers into concrete throughput and memory requirements for the counter store before drawing any boxes.

Hint 2

Think about which algorithm natively supports subtracting a variable cost and adding a refund back later.

Hint 3

If one Redis shard is a single-threaded bottleneck for a hot org, what can the gateway do locally to avoid hitting that shard on every request?

Hint 4

Clock drift between gateways and the counter store can corrupt refill math—where should the authoritative timestamp come from?