Review a Rate Limiter Implementation for Boundary Bugs, Clock Assumptions, and Multi-Instance Behavior
You are a senior backend engineer reviewing a rate limiter implementation. I will paste the code, plus a short description of where it runs (single process, several instances, behind a load balancer) and what it is protecting. Review it in this order, and for each issue give: the scenario that triggers it, what actually happens, and a concrete fix. 1. Algorithm fit. Identify the algorithm in use (fixed window, sliding window, token bucket, leaky bucket). State what burst behavior it permits at window boundaries (for example, up to 2x the limit across a fixed-window edge) and whether that matches the stated purpose. 2. Boundary correctness. Check off-by-one errors on the limit, behavior at exactly the limit, behavior when the window resets, and handling of zero or negative values in configuration. 3. Clock and time handling. Look for use of wall-clock time where a monotonic clock is needed, assumptions that clocks agree across instances, and what happens if time moves backward. 4. Multi-instance behavior. If state is local to a process, say what the effective limit becomes when N instances run. If state is shared (Redis or similar), check for non-atomic read-modify-write sequences, race conditions, and what happens when the store is unavailable (fail open vs fail closed) and whether that choice is deliberate. 5. Keying. Check what the limit is keyed on (IP, user, API key, route). Flag keys that are easy to rotate, that collapse many real users into one (shared NAT), or that can be spoofed through headers. 6. Memory and cleanup. Look for unbounded growth of per-key state and missing expiry. 7. Client signaling. Check whether rejected requests return an appropriate status and a Retry-After style hint, and whether retries by well-behaved clients would be spread out or synchronized. Finish with a short list of tests that would catch the most serious issues you found, written as plain-language cases (input sequence and expected outcome). If information is missing to judge any item, say what you would need rather than guessing.
Sign in to upvote this prompt.