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.
Define what a limiter promises, then build the easiest one.
We know the question (is this request allowed?) but have no code shape. A `RateLimiter` interface with a single method, `allow(key)`. The first implementation is the **fixed window**: count requests per key inside the current time window and reject once the limit is hit. One method keeps callers trivial: they never see counters or windows. Fixed window needs one counter per key, so it is cheap and easy to reason about. Fixed windows have a flaw. A caller can send the full limit at the end of one window and the full limit again at the start of the next: twice the limit in a short burst.
Add the other algorithms behind the same interface.
Fixed window lets bursts through at window edges, and different APIs want different behaviour (strict, bursty, smooth). `SlidingWindowLimiter` keeps timestamps and counts those in the last N seconds. `TokenBucketLimiter` refills tokens at a steady rate and spends one per request. `LeakyBucketLimiter` queues requests and drains them at a constant rate. This is the **Strategy** pattern: same `allow(key)`, different counting. Sliding window is accurate but stores timestamps. Token bucket allows controlled bursts. Leaky bucket smooths traffic to a steady output. Accuracy costs memory (sliding stores every timestamp) or complexity (token bucket tracks refill time). Pick per API: payments want strict, search is fine with bursty.
Build the right limiter from a rule instead of hard-coding it.
The calling code would have to pick and construct limiters itself, and every new API means code changes. A `RateLimitRule` holds the settings: limit, window, algorithm name and what to key on. A `RateLimiterFactory` reads a rule and returns the matching `RateLimiter`. Rules live in configuration, so product and ops can change limits without a deploy. The factory is the one place that knows the list of algorithms. A switch on the algorithm name is simple and fine for four options. A registry (name to constructor) is better if plugins add algorithms.
Control time, swap storage, and plug the limiter into requests.
Algorithms read the real clock, so tests are slow and flaky. State lives inside each limiter, so it cannot be shared between servers. And nothing connects the limiter to real requests. A `Clock` interface supplies the time. A `LimiterStore` holds each key's state, with `InMemoryStore` as the first version. A `RateLimitFilter` asks the limiter for every request and answers 429 when it says no. A fake clock makes tests instant and exact. Putting state behind an interface means a shared store (a cache server) can replace memory without touching the algorithms. The `atomicUpdate` call closes the check-then-increment race. An atomic update through a remote store adds a network hop to every request. Many systems keep a local approximation and sync in the background to avoid that cost.