Design a Distributed Rate Limiter

Normal+ 4 Source: PracHub
ConsistencyRate Limiting Anthropic

Design the rate limiter for a public API that runs on hundreds of servers in three regions. Each API key gets limits like 1,000 requests per minute and 100,000 tokens per minute, and the limit has to hold across every server, not per server.

Functional:

  • Enforce per-key limits on requests per minute and tokens per minute. Different plans have different limits.
  • Tell callers how much they have left and when to retry (headers and a 429 with Retry-After).
  • Let support raise or lower a key's limit and have it apply within a minute.
  • Count tokens after the response, since output length is not known up front.

Non-functional:

  • The check adds under 2 ms at p99 to a request.
  • A key cannot get more than about 10% over its limit by spreading traffic across servers or regions.
  • If the limiter's store is down, the API keeps serving rather than failing every request.
  • 500 API servers across 3 regions, 200,000 requests per second at peak.
  • 2 million active keys; the top 100 keys send 40% of traffic.
  • Token counts per request range from 100 to 200,000.

Billing, abuse detection, and the API's own business logic.

System design
Loading editor...