"Design a rate limiter" is one of the most common system design questions. The usual answer is a counter in Redis: read the count, compare it with the limit, write the count plus one. It passes every test that sends one request at a time.
I spent a week building the version that holds up, as a runnable system in the interview-traps repo: one design lesson, five traps and a rollup. Each trap has the naive code, the fix and a proof test. This post is the short version: one rule that covers most of the bugs, the example that shows it best, and how the other bugs fit around it.
One caveat up front, from the repo itself: Redis in this week is an in-process stand-in (shared/kv.ts) with Redis semantics, so everything runs with nothing but Node. The clock is pinned and crashes are injected on purpose, so each run is repeatable. Read the numbers below as demonstrations of a failure shape, not benchmarks.
Start from a design that runs in production
The design lesson starts from Stripe's engineering post, Scaling your API with rate limiters. It describes four layers: a request rate limiter per user, a concurrent requests limiter, and two load shedders that protect the fleet when it is busy. The interview question is almost always about the first one.
Stripe's request limiter is a token bucket in Redis, and the check-and-spend runs as a Lua script, so nothing else can run between reading the bucket and updating it (their gist). Keep that detail in mind. It is the whole lesson of this post.
The example: limit 10, and 50 requests get in
Here is the counter most people write first:
const count = Number((await redis.get(key)) ?? 0); if (count >= limit) return "429 Too Many Requests"; await redis.set(key, count + 1, { ttlSeconds: 60 }); return "200 OK"; One request at a time, it is perfect. Ten pass and the eleventh gets a 429. That is why it survives QA.
Now send 50 requests at once on one API key with a limit of 10. Every request that reads the count before the first write lands sees zero, passes the check and writes one. When I run it, the output is: "Limit 10, 50 of 50 requests got through. The counter says 1." Every write overwrote the others, so the counter even hides the damage.
This is the same shape as the double-booked seat and the coupon that was applied 50 times: read, decide, write, with a gap in between that concurrent requests walk through.
The fix: one atomic step
Increment first, then compare the result:
const count = await redis.incrWithExpiry(key, 60); // INCR and TTL in one atomic step if (count > limit) return "429 Too Many Requests"; return "200 OK"; Redis runs INCR one command at a time, so every request gets its own number. Requests 1 to 10 pass and 11 to 50 are rejected. The fixed run prints: "Limit 10: exactly 10 of 50 got through, the rest got 429. Counter: 50."
Notice the helper is not plain INCR followed by EXPIRE. That pair is two calls again, and the gap between them is its own bug.
The same rule, a second time: the key that never expires
A textbook fixed-window counter calls INCR, and when the count is 1 it calls EXPIRE to start the minute. It works every day you test it.
Now a deploy kills a server right after INCR and before EXPIRE. The key exists with no expiry. It never resets, so a user sending 50 requests a minute against a limit of 100 keeps adding to one lifetime total. In the trap, a simulated deploy in minute one kills requests after their first Redis call. The result: "the user was blocked for 4 of 6 minutes and never unblocked: the key has no TTL. 99 of 300 allowed."
Peak traffic is when deploys get rushed and pods get killed, so this tends to hit your most active users during the busiest hour.
The fix is the same as before. Create the counter and its expiry in one atomic step, for example a small Lua script that runs INCR and sets EXPIRE on the first hit. Redis runs a script as one unit, so a crash cannot land in the middle. Same deploy, same traffic: all 300 requests allowed. Add an alert on limiter keys that have no TTL as a backstop.
The rule
The rollup states it plainly: anything that takes more than one step must be one atomic step. That covers the counter race, the immortal key, and the token bucket's refill-and-spend, which is exactly why Stripe runs it as a Lua script.
When you review a limiter, look for any place where the code reads a value from Redis and makes a decision in application code before writing back. Each one is a gap.
The two bugs the rule does not cover
The other two traps are not about atomicity. They are about where the count lives and what happens when the store is gone.
One limiter per server. An in-memory limiter is atomic and correct on one machine. Behind a load balancer, each server counts on its own, so the limit multiplies by the number of servers. In the rollup run, a limit of 12 a minute across 4 servers let 48 of 50 requests through. The fix is one shared store that every server uses. A limit is only as global as the store its count lives in.
Redis goes down, the API goes down. A limiter that awaits Redis with no plan B turns Redis into a single point of failure. In the trap, Redis is down and the database is healthy, yet 100 of 100 requests fail with a 500, catalog reads included. The fix is a policy per endpoint, decided before the outage. Cheap reads fall back to a local limit on each server, divided by the number of servers. Costly actions, like sending an SMS code, return a 503. The fixed run served 50 of 50 catalog reads and refused 50 OTP sends without sending a single SMS. Stripe's post gives the same advice in general terms: limiters should fail open on errors, and teams should show clear 429 or 503 errors and keep a kill switch.
One detail that is easy to miss: give the Redis call a short timeout. If the call hangs instead of failing, the policy never runs.
There is also a fifth trap, the window boundary burst, where a fixed window per calendar minute allows twice the limit in a fraction of a second at the boundary. The fix is a token bucket, and that brings you back to the rule: its refill-and-spend must be one atomic step.
The scoreboard
Running every trap with npm run limiter:all ends with: "5/5 naive versions broke. 5/5 fixes held." That run uses 50 concurrent requests per trap. The individual day pages use bigger numbers where the story needs them, such as 400 requests for the per-server trap.
When internal work is enough, and when to get help
If your product has one API and one Redis, you can check a limiter for all five problems in an hour. Search for a GET followed by a SET on the same key, INCR followed by a separate EXPIRE, counters kept in process memory, fixed windows keyed by the current minute, and limiter calls with no timeout or error handling.
It gets harder when limits live in several places at once: an API gateway, middleware, a queue consumer, and an SMS or email provider with its own quotas. That is where a structured review, like the one in what to audit in a vibe-coded MVP, saves time.
Frequently Asked Questions
Should a rate limiter fail open or fail closed when Redis is down?
Decide per endpoint. Cheap reads can fail open to a stricter local limit on each server. Costly or abusable actions, such as sending an OTP, should fail closed with a 503. Either way, give the Redis call a short timeout so the policy actually runs.
Why is GET, check, SET not safe for a rate limit counter?
Reading the count and writing it are two round trips. Concurrent requests all read the same old value, all pass the check, and overwrite each other's writes. An atomic INCR, or a Lua script for a token bucket, gives every request its own number.
Related Reading
- Five Bugs Hiding in a URL Shortener (and the Fix for Each)
- The Double-Booked Seat: Fixing a Booking Race Condition in Postgres
- The OTP the AI Wrote: Why Every Login Code Needs an Attempt Limit
Next Step
Run the traps yourself: clone the interview-traps repo, then run npm run limiter:bug -- counter-race and the matching limiter:fix, or all five with npm run limiter:all. The lessons are on the site, starting with the counter race and the design lesson.
If your SaaS has login codes, public APIs or paid third-party calls behind a limiter and you want these failure shapes checked before they show up in production, that is the focus of my production readiness upgrade. Bring a list of your limited endpoints and where each count is stored, and we can work out where the gaps are.
Originally published at somanathkhadanga.com.



