Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Fixed Window vs Token Bucket: I Let an AI Agent Build Both and Broke One With 20 Requests

A rate limit of 10 requests per second sounds like it means 10 requests per second. I asked an AI agent to write two rate limiters in Node.js, a fixed window counter and a token bucket, and I ran a boundary attack agains

A rate limit of 10 requests per second sounds like it means 10 requests per second. I asked an AI agent to write two rate limiters in Node.js, a fixed window counter and a token bucket, and I ran a boundary attack against both for real. The full run is on video: https://youtu.be/qP5apGGTbb0

The attack

The test is simple. On a simulated clock, send 10 requests just before a second boundary, then 10 more just after it. Twenty requests, landing within 95 milliseconds of each other, against a limiter that should allow 10 per second.

Fixed window: 20 of 20 allowed

The fixed window limiter counts requests inside the current one-second window and resets the counter when the window rolls over. That reset is the whole problem: my 10 requests at the end of window one and 10 at the start of window two are legal in both windows, so the limiter allowed 20 of 20 within 95 ms. Twice the intended rate, right through the middle of the limit.

This is not a bug in the implementation. It is what fixed window means. The algorithm is honest about a per-window count and blind to anything that straddles the edge.

Token bucket: 10 of 20 allowed

The token bucket works differently: a bucket holds up to 10 tokens and refills at 10 tokens per second; each request spends one token, and an empty bucket means rejection. The first 10 requests drain the bucket. The boundary does not reset anything, because there is no window, only a balance. Refill over 95 ms adds less than one token, so the second burst gets rejected: 10 of 20 allowed, exactly the intended rate.

Same limit, same traffic, opposite outcomes. The difference is that the bucket remembers how recently you were allowed, while the window forgets everything at the boundary.

What I did not test

Multiple servers, shared storage like Redis, real network delay, and the sliding window log algorithm are all still open. The test clock is simulated, not wall time. For production, use a maintained limiter. But if your mental model of a rate limit is "10 per second means 10 per second," the fixed window boundary case is worth watching fail.

Watch the run

The code, the terminal output, and every allow/reject are in the video: https://youtu.be/qP5apGGTbb0

Subscribe to Ship Stack AI on YouTube and tell me in the comments what to build and break next.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.