Rate limiting algorithms for system design interviews, explained
Design any public API — a login endpoint, a payments gateway, a tweet feed — and the interviewer will eventually ask the same thing: a single client is hammering you with ten thousand requests a second, so how do you cap them without taking the service down for everyone else? That is a rate limiting question, and the answer is never "add a limit" — it is which algorithm, and why. The rate limiting algorithms worth knowing are a short list — the token bucket algorithm, the leaky bucket algorithm, the fixed window counter, and the two sliding-window variants — and each makes a different trade between allowing short bursts, smoothing traffic, memory cost, and accuracy at the window edge. Pick the wrong one and a client sends double your limit in a single second; pick the right one and you can say exactly why.
The reason it comes up is that every real system needs to protect itself from traffic it did not ask for — a buggy client in a retry loop, a scraper, a denial-of-service attack — while still being fair to well-behaved users and keeping infrastructure costs bounded. A limiter is a tiny component with outsized design consequences: where it sits, what it counts, and how it behaves at a burst all show up in latency and correctness. This guide walks what a strong candidate names out loud: why you rate limit at all, the four classic algorithms and their tradeoffs, where the limiter lives, and distributed rate limiting — a shared counter across many servers, where the race conditions and the sliding window rate limiting math are the follow-ups the interview is really about.
Why rate limiting comes up in system design interviews
Rate limiting rarely gets its own prompt. It appears inside a larger design — "design Twitter," "design a URL shortener," "design a payment API" — the moment the interviewer asks how you stop one client from overwhelming the service or running up your bill. You say "I'll add a rate limiter," and the follow-up is the trap: with what algorithm, counted per what key, and stored where when you have fifty API servers behind a load balancer? Rate limiting is a favourite probe because "add a limit" is the easy half, and choosing an algorithm whose burst and accuracy behaviour you can defend is where real understanding shows.
The interviewer rarely lets "add a rate limiter" stand on its own. "Your fixed-window limiter allows 100 requests per minute. A client sends 100 at 11:59:59 and another 100 at 12:00:00 — that's 200 requests in one second, and both windows are technically under the limit. What broke, and which algorithm fixes it?" That question — the boundary burst a naive counter lets through — is the one a memorised "use a token bucket" answer can never survive.
The rounds on Zynter are modelled on how the loop runs, not scraped from any candidate's recall. What matters for prep is the shape: you name an algorithm, and the next sentence attacks a burst at the window edge, the memory cost of storing every timestamp, or a race between two servers incrementing the same counter.
What the interviewer is really testing
A rate-limiting answer is read on the same four dimensions as any design decision. Problem solving — did you pick an algorithm because its burst behaviour fits the endpoint, or just name the first one you remember? Communication — can you say in one sentence why token bucket allows bursts and leaky bucket does not? Technical execution — do you handle the distributed case, the boundary burst, and the per-user key? Time management — did you spend your words on the tradeoffs that matter, not on the limiter's config syntax? Reading this guide teaches the concepts; it does not teach you to defend an algorithm choice while someone pushes a burst through its weakest edge.
The problem: why you rate limit, and where the limiter lives
Start with why, because the algorithm choice only makes sense against the goal. A rate limiter caps how many requests a client can make in a window, and you reach for one for four reasons, all worth naming:
- Protect from abuse and DoS. A single client — malicious, or just stuck in a retry loop — can flood an endpoint and exhaust CPU, connections, or a downstream database. The limiter sheds that load at the edge before it reaches anything expensive.
- Fairness. Shared capacity means one noisy tenant should not starve everyone else. A per-user or per-API-key limit keeps one client from consuming the whole pool.
- Cost control. Every request you serve costs compute, bandwidth, and often third-party API calls you pay for. A limit puts a ceiling on spend you did not intend.
- Backpressure. Limiting at the front is a controlled way to tell clients to slow down — returning HTTP 429 Too Many Requests — instead of letting an overloaded system fail unpredictably deeper in.
The second half of the problem is where the limiter lives. It usually sits at the API gateway or a middleware layer, in front of the application logic, so rejected traffic never touches the services it would overload. And it almost always counts against a key — typically a user ID or API key, sometimes a client IP for anonymous traffic — so the limit is per-client, not global. The sentence that scores: I'd rate limit at the gateway, keyed by API key, to protect the service from abuse, keep it fair across tenants, and bound cost — rejecting over-limit requests with a 429 before they reach anything expensive. Naming the key and the placement, unprompted, frames every algorithm choice that follows.
Token bucket vs leaky bucket
The first two algorithms are the ones interviewers expect you to compare, because they handle bursts in opposite ways.
- Token bucket. Picture a bucket that holds up to B tokens and refills at a steady rate R tokens per second. Each request removes one token; if the bucket is empty, the request is rejected (or waits). Because the bucket can be full, a client that has been quiet can spend up to B requests in a burst, then is throttled to the steady rate R. This is the common choice — it is memory-cheap (you store a token count and a last-refill timestamp per key), and the burst allowance matches how real clients behave. It is what AWS, Stripe, and most API gateways use.
- Leaky bucket. Picture requests entering a queue (the bucket) and draining out at a fixed rate — like water leaking from a hole at the bottom. Requests are processed at a constant, smooth rate no matter how they arrive; if the queue is full, new requests overflow and are dropped. Leaky bucket smooths bursts into a steady stream and gives no burst allowance at all, which is exactly what you want when a downstream system can only handle a fixed throughput.
The design decision is burst versus smoothness. The sentence that scores: token bucket allows short bursts up to the bucket size and then throttles to the refill rate — cheap to store and friendly to bursty clients, so it's my default; leaky bucket queues requests and drains them at a constant rate, giving no burst but a perfectly smooth output, which I'd pick when the thing behind the limiter can only take a fixed rate. Knowing when to pick each — token bucket for most public APIs, leaky bucket when you must protect a fixed-throughput downstream — is the judgment signal.
System Design
HLD · Core concept
Defend token bucket vs leaky bucket out loud
Fixed window vs sliding window counters
The other family counts requests inside a time window, and the whole point of the comparison is the boundary burst the naive version lets through.
- Fixed window counter. Divide time into fixed intervals — say one-minute buckets aligned to the clock — and keep a counter per key per interval. Each request increments the counter; when it exceeds the limit, reject. It is dead simple and memory-cheap (one integer per key per window), but it has a sharp flaw: because the window resets at a fixed boundary, a client can send the full limit at the end of one window and the full limit at the start of the next — up to 2x the limit in a short span straddling the boundary. That is the exact failure the opening blockquote describes.
- Sliding window log. Store a timestamp for every request in a sorted structure; to check the limit, count how many timestamps fall within the last window (and drop older ones). This is exact — no boundary burst, the limit is enforced over any rolling window — but it is memory-heavy: you store one entry per request per key, which gets expensive at scale.
- Sliding window counter. The practical middle ground. Keep a counter for the current and previous fixed windows, then weight the previous window by how much of it still overlaps the rolling window. If you are 25% into the current minute, you count the current window's requests plus 75% of the previous window's. It is an approximation, but it kills the boundary burst while using roughly the memory of a fixed window — which is why it is the sliding option people actually ship.
The sentence that scores: fixed window is one counter per window — simple but it allows up to double the limit at the boundary; sliding window log stores every timestamp so it's exact but memory-heavy; sliding window counter weights the previous window by its overlap, approximating the exact answer at fixed-window cost — that's the one I'd use when the boundary burst matters and I can't afford to store every timestamp.
System Design
HLD · Core concept
Walk the window-boundary burst and its fix out loud
Distributed rate limiting: a shared counter in Redis
Everything above assumes one process holding the counter in memory. The interview turns the moment you have many API servers behind a load balancer: a per-server in-memory counter means a client's requests get spread across servers, each sees only a fraction of the traffic, and the real limit becomes N times what you set. The fix is a shared counter in a central store, and the standard answer is Redis.
- The simple version is Redis
INCRplusEXPIRE: for a fixed-window limit, increment a key likerate:{user}:{minute}and set it to expire after the window; if the returned count exceeds the limit, reject. One shared counter, every server reads and writes it. - The race condition is the follow-up.
INCRis atomic, so the increment itself is safe — but a naive read-then-check-then-increment across two commands lets two servers both read a count under the limit and both proceed, admitting too many. You close the gap by doing the check-and-increment atomically: a Lua script that reads, decides, and updates in one round trip (the usual way to implement a distributed token bucket), or Redis's atomic primitives so the decision never splits across commands. - The real tradeoff is synchronisation versus accuracy. A single shared counter is accurate but adds a network hop to every request and makes Redis a hot dependency. To cut that latency teams let each server hold a local allowance and sync with the central store periodically — which is faster but lets the global count drift slightly over the limit between syncs. Naming that accuracy-versus-latency trade, unprompted, is the tell that you have actually built one of these.
The sentence that scores: across many servers an in-memory counter multiplies the limit, so I'd keep the counter in Redis — INCR with EXPIRE for a fixed window, or a Lua script for an atomic token-bucket check — and the real decision is whether to hit the shared store on every request for accuracy or sync local counters periodically for latency, accepting a little overshoot.
The tradeoffs that decide the round
No algorithm is free, and naming the sharp edges is what separates a real answer from a list:
- Burst behaviour. Token bucket allows a burst up to the bucket size; leaky bucket allows none; fixed window allows an accidental 2x burst at the boundary. Match the burst behaviour to the endpoint — a login endpoint wants tight smoothing, a read API can tolerate bursts.
- Memory cost. Sliding window log stores a timestamp per request and gets expensive at scale; token bucket and fixed/sliding window counters store a couple of numbers per key. At millions of keys this difference is the design.
- Accuracy at the boundary. Fixed window is approximate and leaky at the edge; sliding window log is exact; sliding window counter is a close, cheap approximation. Decide how much boundary error you can tolerate.
- Distributed race conditions. A shared counter must be incremented atomically or two servers overshoot the limit;
INCRis atomic, a read-modify-write is not — so push the decision into a single atomic operation or Lua script. - The accuracy-versus-latency trade in the distributed case. A central counter is accurate but adds a hop; local counters synced periodically are fast but drift over the limit. There is no free lunch; say which side you are choosing and why.
The point to state plainly: pick the algorithm by its burst and accuracy behaviour, size it by memory at your key count, and in the distributed case choose deliberately between a shared counter for accuracy and local counters for latency. Those are the follow-ups that decide the round.
How to talk about rate limiting in an interview
The probes from every section rhyme. Master these five and you can defend a rate limiter in any design, not just this checklist.
- "Which algorithm, and why?" → Token bucket by default — cheap and burst-friendly; leaky bucket to force a smooth rate into a fixed-throughput downstream; sliding window counter when the window boundary burst matters. (problem solving)
- "Fixed window lets 2x through at the boundary — fix it." → Switch to a sliding window counter that weights the previous window by its overlap, or a sliding window log if you need exactness and can afford the memory. (technical execution)
- "Token bucket or leaky bucket?" → Token bucket allows bursts up to the bucket size then throttles to the refill rate; leaky bucket queues and drains at a constant rate with no burst — pick by whether the endpoint should tolerate bursts. (communication)
- "You have fifty servers — where's the counter?" → In a shared store like Redis (INCR + EXPIRE, or a Lua token-bucket script), because a per-server in-memory counter multiplies the real limit by the server count. (technical execution)
- "Two servers increment at once — isn't that a race?" → Make the check-and-increment atomic — Redis INCR or a single Lua script — so the limit decision never splits across commands; and name the accuracy-versus-latency trade if you sync local counters. (problem solving)
Each maps to a debrief dimension. Reading them teaches the answers; it does not teach you to deliver them while someone pushes a burst through your weakest edge — that only comes from saying them out loud.
How to prepare: a short plan
You do not need a new topic for every design. You need the rate-limiting vocabulary automatic, so when a design needs to cap traffic you reach for the right algorithm without thinking.
- Day 1: Learn the four algorithms and their burst behaviour — token bucket, leaky bucket, fixed window, and the two sliding windows — until you can draw each from memory and say its one tradeoff.
- Day 2: Drill the boundary-burst problem and its fix, and the distributed case — a shared Redis counter, the atomic increment, and the accuracy-versus-latency sync trade. These are where the follow-ups live.
- Day 3: Place a limiter inside a real design (a login endpoint, a public API, a payments gateway): choose an algorithm, pick the key, put the counter in Redis, and name the race condition. Then do it out loud, timed, with someone pushing a burst through the edge.
This explainer feeds the broader Top 25 system design interview questions — rate limiting shows up in almost every API design, so getting the algorithms automatic pays off across the whole list.
Frequently asked questions
What are the main rate limiting algorithms?
There are four you are expected to know. The token bucket algorithm holds up to B tokens and refills at a steady rate R; each request takes a token, an empty bucket rejects, and because the bucket can be full it allows a short burst before throttling to R — it is the common default. The leaky bucket algorithm queues requests and drains them at a constant fixed rate, smoothing bursts into a steady stream with no burst allowance. The fixed window counter keeps one count per key per fixed interval — simple and cheap but it allows roughly double the limit at the window boundary. Sliding window rate limiting comes in two forms: a sliding window log stores a timestamp per request for an exact count at higher memory cost, and a sliding window counter weights the previous window by its overlap to approximate the exact answer cheaply. You pick by burst behaviour, memory, and how much boundary error you can tolerate.
Token bucket vs leaky bucket — which should I use?
They differ in how they treat bursts. The token bucket algorithm lets a client spend accumulated tokens up to the bucket size in a burst, then throttles them to the refill rate — it is memory-cheap (a count and a timestamp per key) and matches how real clients behave, so it is the default for most public APIs, and what gateways like AWS and Stripe use. The leaky bucket algorithm processes requests out of a queue at a constant rate no matter how they arrive, giving a perfectly smooth output and no burst at all, with overflow dropped when the queue is full. Choose token bucket when short bursts are fine and you want simplicity; choose leaky bucket when the system behind the limiter can only handle a fixed, steady throughput and you must smooth traffic into it.
Why does a fixed window rate limiter allow bursts at the boundary?
Because the window resets at a fixed point in time and the counter has no memory across that reset. If your limit is 100 requests per minute and a client sends 100 in the last second of one minute and 100 in the first second of the next, each window saw only 100 — under the limit — but the system actually took 200 requests in about two seconds straddling the boundary. The fix is a sliding window approach: a sliding window log counts requests over any rolling window exactly by storing each timestamp, and a sliding window counter approximates that by weighting the previous fixed window by how much of it still overlaps the current rolling window. The sliding window counter is the practical choice because it removes the boundary burst while using about the same memory as a fixed window.
How does distributed rate limiting work across many servers?
When many API servers sit behind a load balancer, a per-server in-memory counter fails: a client's requests are spread across servers, each sees a fraction of the traffic, and the effective limit becomes the server count times what you intended. The standard fix is a shared counter in a central store like Redis — for a fixed window you INCR a key such as rate:{user}:{minute} and set an EXPIRE on it, and reject when the count exceeds the limit; for a token bucket you run a small Lua script so the check-and-update is atomic. The subtlety is that a read-then-increment across two commands can race, so two servers both see a value under the limit and both proceed — you avoid it by doing the decision atomically. The real tradeoff is accuracy versus latency: hitting the shared store on every request is accurate but adds a hop, while syncing local counters periodically is faster but drifts slightly over the limit.
How do I practise a system design interview out loud?
Reading about rate limiting algorithms is not the same as defending an algorithm choice while an interviewer pushes a burst through its weakest edge. On Zynter, the HLD track runs live voice rounds modelled on how real system design loops run — you talk through a design (like a public API or a payments gateway) on a shared canvas, and a voice interviewer follows up one question at a time ("which algorithm, and why?", "fixed window lets 2x through — fix it", "you have fifty servers, where's the counter?"), then gives you a debrief on problem solving, communication, technical execution and time management. The score is a practice signal, not a hiring decision.
Practise it on Zynter
You can say "add a rate limiter." The round is won on the algorithm, the boundary burst, and the shared counter.
Pick an HLD system design round with a public API, place the limiter on the canvas, and defend it out loud to a voice interviewer that keeps asking "token bucket or leaky bucket?" and "fifty servers — where does the count live?" — then read the debrief. Zynter.ai is on-demand mock interviews modelled on real company loops, with 150+ rounds across 20 companies including the HLD system design track (Sep 2026).
Practise an HLD system design round →
Browse all interview tracks





