Design a Rate Limiter: System Design Interview Walkthrough
- Andrew C. Lewis
- 12 Nov, 2026
- 14 Mins read
“Design a rate limiter” is the question interviewers pick when they want depth, not breadth. A rate limiter is the part of a system that counts how many requests each user or API key sends and rejects the extra ones. There is no feed to rank and no video to encode. The whole design fits on one board in ten minutes, which leaves the interviewer 30 minutes to probe the algorithm, the race condition (two servers updating the same counter at the same moment) and what happens when the store that holds the counters goes down.
This walkthrough follows the 45-minute clock: requirements, the one number that matters, the high-level design with one request walked through it, two deep dives, then trade-offs. The real-world references are Stripe’s write-up of its four production limiters (Stripe, 2017) and Cloudflare’s account of the counting method behind its rate limiting product (Cloudflare, 2017). Both are short and worth reading before the round.
Requirements and scope (0 to 5 minutes)
Do not draw yet. The prompt is vague on purpose. Ask these, in this order, and write the answers on the board:
- What are we limiting by? A user id, an API key, an IP address, or an endpoint. Each one changes the key you count on.
- Where does it sit? In the API gateway (the one front door that every request passes through), as middleware inside each service, or at the edge in front of the whole site.
- What do we return? The standard answer is 429 Too Many Requests (MDN) with a Retry-After header that tells the client how many seconds to wait (MDN).
- Hard or soft limits? A hard limit rejects every request over the line. A soft limit allows short bursts above it, or only logs and alerts.
- How exact? If a key may get 1% more than its limit during a traffic spike, the design gets much cheaper. Say that out loud.
- One region or several? Counters that must agree across continents are a different problem.
What to say when the interviewer says “you decide”: “I will assume we limit by API key, that the limiter lives in the API gateway so every service gets it for free, and that we return 429 with Retry-After. Limits are hard, but a short burst above the line is fine. A small amount of over-counting is acceptable, and I will point out where that matters. One region for now. Tell me if any of that is wrong.”
Write the scope as two lists. In scope: per-key request rate, a reusable check that services can call, correct behaviour when many gateway servers share one key. Out of scope: monthly billing quotas, network-level flood protection, per-region sync. You will point at this list later when you cut something.
One more thing to say in this step, because it frames everything after it: a rate limiter exists to protect the service from one badly behaved client while everyone else keeps working. Stripe’s post frames it the same way, and separates limits that protect against one user (answered with 429) from load shedding that protects the whole fleet during an incident (answered with 503, Service Unavailable) (Stripe). That distinction comes back in the second deep dive.
The number that matters (5 to 8 minutes)
Two numbers pick components here, and you should calculate both. The first is the check rate across the fleet, which decides whether one counter store is enough. The second is memory per counter, which decides the algorithm.
Assume 10 million active API keys and a fleet peak of 100,000 requests a second. Those are assumptions, not Stripe’s numbers; say so.
- Check rate. Every request needs one counter read and one write. Done as separate commands, that is 200,000 operations a second. Done as one atomic script, 100,000. Redis handles about 180,000 SET commands a second on one instance without pipelining (sending commands in batches), and about 1.5 million with it (Redis docs). So one Redis node is at its edge, and two or three shards split by key are comfortable.
- Memory, counter designs. A token bucket needs two numbers per key: the token count and the last refill time. With the key string and the store’s own overhead, call it about 100 bytes a key as a rule of thumb. For 10 million keys that is about 1 GB. Cloudflare describes its sliding window method as needing “only two numbers per counter” (Cloudflare), so the same order of size.
- Memory, exact log. A design that stores the timestamp of every request needs 8 bytes per timestamp. At a limit of 1,000 requests a minute, that is up to 8 KB per key. For 10 million keys, 80 GB. Cloudflare rejected this approach for its “huge processing and memory requirements” (Cloudflare).
What to say: “A hundred thousand checks a second fits in a small Redis cluster if each check is one round trip. Memory is the real constraint. Anything that stores every request timestamp is around 80 GB for 10 million keys, while a two-number counter is about a gigabyte. So I will pick a counter design unless you need exact counts, and I will come back to the atomic check when we talk about the race condition.”
If the arithmetic habit is new to you, the back-of-the-envelope estimation guide covers the constants and the “calculate only what picks a component” rule.
High-level design, then walk one request (8 to 20 minutes)
The parts, in the order you draw them:
- API gateway with the limiter as middleware. Every request passes through it. The limiter runs before authentication is finished, if the API key is readable from the header, so rejected requests cost as little as possible.
- Limit rules store. Which limit applies to which key or tier (for example 100 requests a second for a free key, 1,000 for a paid one). It changes rarely, so each gateway node keeps a copy in memory and refreshes it every minute.
- Counter store. Redis, sharded by key, holding one small record per API key with an expiry so idle keys disappear on their own.
- The response. 429 with Retry-After on rejection. On success, headers such as X-RateLimit-Remaining so a well-behaved client can slow down before it hits the wall.
Then place the limiter, out loud, because the interviewer will ask. In the gateway: one place to run it, consistent across services, one extra round trip per request. Inside each service as a library: no extra hop and per-service limits, but every team has to keep it correct. At the edge, in front of the whole site: good for abuse by IP address, blind to API keys. Pick the gateway for API keys and say the edge handles floods by IP, which you have put out of scope.
Now walk one request through it, pointing at each box.
What to say: “A client sends GET /v1/charges with its API key. The gateway reads the key from the header, finds the key’s limit in its local copy of the rules, and sends one atomic command to the Redis shard that owns this key: refill, check, take a token, set the expiry. Redis answers in well under a millisecond, since a round trip inside one data centre is about 500 microseconds (Jeff Dean’s latency table). If the answer is ‘allowed’, the gateway forwards the request and adds the remaining count to the response headers. If not, it returns 429 with Retry-After set to the number of seconds until the next token.”
Before the deep dives, pick the algorithm. This is the part of the design the interviewer will test hardest, so state the four options with their real behaviour and cost. For a wider menu of these building blocks, see the system design cheat sheet.
Fixed window counter
Count requests per key per clock minute: one integer, incremented on each request, expiring at the end of the minute. Cheapest possible. The flaw is the boundary. With a limit of 100 a minute, a client can send 100 requests at 0:59 and 100 more at 1:00, which is 200 requests in two seconds while the limiter sees nothing wrong. That is the “2x burst at the boundary”, and the interviewer expects you to name it without prompting.
Sliding log
Store the timestamp of every request in a sorted set per key, drop the ones older than the window, count the rest. Exact, always. The cost is the 80 GB from the estimate above, plus the work of trimming the set on every request. Worth naming as the exact option and then rejecting with the number.
Sliding window counter
Cloudflare’s approximation keeps two counters per key, the previous minute and the current one, and weights the previous minute by how much of it still falls inside a sliding 60-second window. Their worked example: 42 requests in the previous minute, 18 so far in the current minute, 15 seconds into it. The estimated rate is 42 x (45 / 60) + 18 = 49.5, just under a limit of 50 (Cloudflare).
The method assumes requests in the previous minute were spread evenly, so it is an estimate. Cloudflare measured the error on 400 million requests from 270,000 sources: 0.003% of requests were “wrongly allowed or rate limited”, and the average gap between the estimated and real rate was 6% (Cloudflare). Two numbers per counter, no boundary burst, and an error you can quote. This is what you pick when the requirement is a smooth cap per minute.
Token bucket
Each key owns a bucket with capacity b. Tokens drip in at r per second up to b. Every request takes one token; an empty bucket means reject (token bucket). The average rate is r, and a client that has been quiet can burst up to b requests at once, which is a feature for things like a flash sale. Stripe: “We use the token bucket algorithm to do rate limiting”, with the buckets in Redis (Stripe).
Implementation detail worth saying: you do not run a timer that adds tokens. You store the token count and the last refill time, and on each request you compute how many tokens were earned since then, add them, cap at b, then try to take one. Two numbers per key, lazy refill.
What to say to close the choice: “I will use a token bucket, because bursts are something an API should allow and the cost is two numbers per key. If the requirement were a smooth per-minute cap with a known error, I would use the sliding window counter instead. The fixed window is out because of the boundary burst, and the log is out on memory.”
Now stop and ask: “That is the basic design. Do you want me to go into the distributed counter, or somewhere else?” You are at the midpoint with a complete picture, which is the pacing rule from the framework.
Deep dive one: counters across many gateway servers (20 to 29 minutes)
The gateway is not one server. It is dozens, behind a load balancer, and the same API key’s requests land on all of them. Three questions follow.
Where does the counter live?
- Local, in each gateway’s memory. No network hop, fastest possible. The problem: with 20 gateway nodes, each one sees about a twentieth of a key’s traffic, so a key can get 20 times its limit before any single node objects. Two fixes. Pin each key to one node by hashing it at the load balancer, so one node sees all of a key’s traffic (and takes it all down when it fails). Or keep local counts and sync them to a central store every 100 milliseconds or so, accepting a short window of over-admission. Cloudflare counts per data centre and increments its counters asynchronously, trading a little precision for speed (Cloudflare).
- Central, in Redis. Every node checks the same counter, so the limit is exact across the fleet, at the cost of one round trip per request. This is the Stripe shape (Stripe).
Pick central for hard limits and local-with-sync for soft ones, and say which your requirements call for.
The race between read and write.
The naive check is: read the counter, compare, write the new value. Two gateway nodes can both read 99, both decide 99 is under 100, and both write 100. The key got 101. Under load this happens constantly.
The fix is to make the check one atomic operation (a step that either completes entirely or not at all, with nothing else interleaving). Redis gives you two ways. INCR increments and returns the new value in one command, and the Redis docs show a rate limiter built on INCR plus an expiry as a worked pattern (Redis INCR). For a token bucket, which needs read, refill, compare and write together, you use a short Lua script run with EVAL; Redis runs a script to completion with no other command in between (Redis scripting). Cloudflare’s design needs only GET, SET and the atomic INCR, which is part of why they chose a memcached-style store (Cloudflare).
What to say: “The check is one Lua script: load the two numbers, add the tokens earned since the last refill, take one if there is one, write both numbers back, set the expiry to the time it takes the bucket to refill completely. Redis runs it atomically, so two gateway nodes can never both see the same last token.”
What happens when Redis is down?
This is the question that separates candidates who have run a limiter from candidates who have read about one. Two answers:
- Fail open. If Redis times out or errors, treat the request as allowed, log it, and alert. Stripe wraps its limiter so that “any coding or operational errors would fail open”, because a bug in the limiter must not take the API down (Stripe).
- Fail closed. Reject everything until Redis is back. Safer for the backend, terrible for every customer.
Pick fail open for the per-key limiter, because its job is fairness between customers and a few minutes without it is survivable. Then add the operational detail: a short timeout on the Redis call, a few milliseconds, so a slow Redis cannot slow every request in the fleet, and a circuit breaker that stops calling Redis for a while after repeated failures (Martin Fowler).
Two smaller points, if there is time. Sharding: hash the API key to pick the Redis shard, so each key always lands on the same one and a bucket is never split. Hot keys: one huge customer sends all its checks to one shard, which at these rates is fine, and if it were not, that customer gets its own shard.
Deep dive two: why Stripe runs four limiters, not one (29 to 38 minutes)
A single request-rate limit answers the interview question. Explaining why a real API needs more than one answers the senior version of it. Stripe describes four, and each exists because the previous one leaves a gap (Stripe).
- Request rate limiter. N requests a second per user. Stripe calls this the most important of the four and the one that triggers most. It allows brief bursts above the cap for spikes such as flash sales, which is the token bucket’s capacity b at work.
- Concurrent requests limiter. Not requests per second but requests in flight at the same moment, for example “You can only have 20 API requests in progress at the same time”. It protects CPU-heavy endpoints from a client that opens many slow requests at once, which a per-second limit never notices. Stripe says it triggers far less often. Implementation: increment a counter when a request starts, decrement when it ends, with an expiry in case the gateway crashes between the two.
- Fleet usage load shedder. The first two protect against one user. This one protects the fleet. Traffic is split into critical methods (creating a charge) and non-critical (listing charges), counted in Redis. In Stripe’s example, with “a reservation number of 20%”, non-critical traffic above its 80% share is rejected with 503, so a flood of reads cannot stop payments from going through.
- Worker utilization load shedder. The last line, for major incidents. Traffic is split into critical methods, POSTs, GETs and test-mode traffic, and when workers are nearly all busy, the shedder drops the least important category first, “starting with test mode traffic”, and escalates if that is not enough. Stripe notes that shedding and restoring must happen slowly so the system does not flap between the two states.
What to say: “One limiter per threat. The rate limiter stops a noisy customer, the concurrency limiter stops a slow one, the fleet shedder keeps payments working when reads flood in, and the worker shedder keeps the lights on in an outage. The first two answer 429, so the client knows it should slow down. The last two answer 503, so the client knows the service is the problem and should retry later with backoff.”
The client side matters too. A client that retries a 429 immediately makes the problem worse. The right behaviour is exponential backoff with jitter: wait longer after each failure, plus a random offset so a thousand clients do not retry in the same second (AWS). Saying that your Retry-After header is what makes good client behaviour possible closes the loop.
If you reach for a cache here, notice that the only cache in this design is the local copy of the limit rules. An interviewer on Hacker News describes candidates whose answer to every bottleneck is “add a cache” (Hacker News, March 2025); this question is a good one for showing you know when not to.
Trade-offs and what you cut (38 to 43 minutes)
Give the one-minute summary: choice, cost, what was cut, what comes next.
What to say: “I chose a token bucket in Redis, checked by one atomic script from the API gateway, because it allows bursts, costs two numbers a key, and is exact across the fleet. The cost is one extra round trip per request and a dependency on Redis, which I handle by failing open with a short timeout. What I cut: monthly quotas, which belong in billing; flood protection by IP, which belongs at the edge; and multi-region, where I would run a counter per region and accept that a key can get up to one extra region’s worth of limit. With more time I would add a shadow mode that counts and logs without rejecting, so a new limit can be watched for a week before it is enforced.”
Then stop talking. Interviewers grade the trade-offs section hardest, and candidates most often run out of time for it. For the other feedback phrases that show up in rejections, with each weak answer rewritten, see why candidates fail system design interviews.
What the interviewer probes at each level
The same question is asked at three levels. The design is the same; the follow-ups are not.
- Mid-level. Can you explain the fixed window and name the boundary burst? Can you describe a token bucket and where its two numbers live? Do you know what 429 and Retry-After mean and who reads them? Can you draw the gateway, Redis and the response path without help? A working single-node answer with INCR and an expiry passes.
- Senior. The race between read and write, and the atomic script that fixes it. The memory math that rules out the sliding log. Fail open versus fail closed, with the timeout and the circuit breaker. Sharding by key and the hot key. What the client should do with the headers. A Rippling interviewer failed a senior candidate for naming a technique without its patterns and trade-offs (Blind, September 2025); at this level, every component needs its failure mode.
- Staff. Why one limiter is not enough, and how load shedding by priority protects the business. Multi-region counters and what consistency you give up. Rolling out a new limit without breaking existing customers. Hello Interview names the biggest staff-level mistake as listing options without picking one (Hello Interview), so at this level, pick, and say what you would need to see in production to change your mind.
For a list of other questions in this shape, with what each one tests, see the 20 system design interview questions with answer outlines. The rate limiter pairs well with design a payment system, because both are about what a server owes a client that retries: here the answer is a 429 and a wait, there it is the same result without a second charge.
How InterviewStorm helps
Run this question as a mock call and InterviewStorm acts as your practice partner for the round. It hands back the requirements to confirm, the two numbers that pick the components, an architecture diagram it draws itself, the request walked through the gateway and the counter store, the schema and the API shape, the scaling moves, and the trade-offs to name, so you can rehearse the whole 45 minutes out loud and compare your board with a complete answer. See the system design feature.
Most companies do not allow AI help during the design round itself, and interviewers say they look for it. Use it live only where the employer allows it, and to practise everywhere else. Follow each employer’s interview rules. Do not use InterviewStorm in proctored tests, exams, or anywhere AI help is not allowed.
The free trial is 20 minutes in total, up to 15 minutes per session, with no credit card. Try it free.
For the rest of the loop, read the guides on coding interviews and the STAR method.
Share Articles:
Similar Posts