Rate Limiter
Design a per-client rate limiter with pluggable algorithms (token bucket, fixed window, sliding log) and a testable clock.
Requirements
A rate limiter decides whether a request may go ahead or must be refused because the caller has sent too many. It protects a service from overload, abuse and one noisy client.
allow(key)answers yes or no for one client, identified by a key such as a user id or an IP address.- Each key has its own independent budget.
- The algorithm is swappable: token bucket, fixed window, sliding window log.
- Safe to call from many threads.
- Time comes from an injected clock, so tests do not need to sleep.
Out of scope for this lesson: sharing counters between several servers (see the truth box below).
1 of 7