Design a API Rate Limiter
Design the rate limiter for a public API. Every request carries an API key, and each key has limits that depend on its plan, such as 1,000 requests a minute on the free plan, with stricter limits on some endpoints, such as 5 login attempts a minute. A request over its limit gets 429 Too Many Requests and a hint of when to retry; everything else passes through untouched.
The API takes 1 million requests a second at peak through about 40 gateway servers, from 10 million keys. A key’s requests land on any gateway, so the gateways must agree on how much of each limit is used, yet the check sits in front of every single request and may add no more than about 1 ms. When a few keys flood us at 25 times their limit, the limiter is what keeps the backend up, and if the limiter itself breaks, it must not take the API down with it.
Good questions to ask first
Decide what you would assume for each before you look at ours.
- 1What do we limit by?
- 2Which time windows?
- 3How exact must it be?
- 4Bursts?
- 5Is the limit global or per region?
- 6What if the limiter fails?
- 7How fast must a limit change apply?
- 8Do clients need to see their usage?
How to spend the 45 minutes
- 5minPart 1: Requirements and scope
- 5minPart 2: Estimates
- 5minPart 3: API
- 15minPart 4: Architecture, stage by stage
- 15minPart 5: Deep dives and trade-offs