Design a rate limiter that decides, for each incoming request, whether it may proceed or must be rejected because the caller has sent too many requests recently. Different APIs and different users can have different limits.
It must
allow(key) answers yes or no for one caller (a user id, an API key or an IP).- Limits are written as N requests per time window and can differ per API or per user.
- Support several algorithms: fixed window, sliding window, token bucket, leaky bucket.
- Choose the algorithm from configuration, without code changes.
- Rejected requests get a clear answer (HTTP 429).
Keep in mind
allow must be fast: it runs on every request.- It must be correct under concurrency: ten requests at once must not all slip through a limit of five.
- Distributed limiting (many servers sharing counts) is a follow-up, but the design must not block it.