System Design Interview Questions: Rate Limiting Patterns

Reviewed by Mark Dickie · Last updated

7 questions on this page you can answer and see marked on the spot. Go to the first one

Rate limiting is a technique that controls how many requests a client can send to a service within a fixed time window, protecting backend resources from overload and abuse. For system design interviews, you should be able to identify the right rate-limiting algorithm for a given scenario, explain the trade-offs between accuracy and memory, and discuss how to enforce limits in a distributed environment where multiple servers share state. Expect to justify where the limiter sits (client, API gateway, application server) and how it behaves under partial failures.

The most common algorithms and their core properties:

AlgorithmHow it worksStrengthsWeaknesses
Token bucketTokens refill at a fixed rate; each request consumes one tokenAllows bursts up to bucket capacity; memory-efficientHard to tune burst size precisely
Leaky bucketRequests enter a queue and drain at a constant rateSmooths traffic to a steady output rateQueue can grow unbounded under sustained load
Fixed windowCounter resets at each time boundary (e.g. every minute)Simple to implement and reason aboutBurst at window edges can double traffic
Sliding window logStores timestamps of each request, removes expired onesMost accurate limit enforcementHigh memory and compute per request
Sliding window counterSplits the window into sub-windows and weights the current positionGood accuracy with low memorySlight over- or under-counting at boundaries

What does a rate-limiting interview question typically ask you to design?

You are usually given a service (a URL shortener, an API gateway, a login endpoint) and asked to add rate limiting to it. The interviewer wants to see you work through the following decisions in order:

  1. Choose an algorithm that fits the traffic pattern — token bucket for bursty APIs, leaky bucket for smoothing, sliding window for strict per-user caps.
  2. Decide where to enforce the limit — at the load balancer, in an API gateway, or inside the application logic — and explain the trade-off of each placement.
  3. Pick a data store for distributed counting — Redis with INCR and EXPIRE is the default choice; explain why atomicity matters when multiple server nodes share the counter.
  4. Handle edge cases: what happens when Redis is temporarily unavailable, whether to fail open or fail closed, and how to return a 429 Too Many Requests response with a Retry-After header.
  5. Discuss observability — logging rejected requests, exposing metrics, and setting alerts so you can tune limits based on real traffic.

How do you implement distributed rate limiting across multiple servers?

In a single-server setup an in-memory counter is enough. Once you scale horizontally, every server instance needs to agree on the current count, so the limiter must read from and write to a shared store. Redis is the go-to because its single-threaded model makes operations like INCR atomic without explicit locking. For stricter guarantees you can use a Lua script that checks the count and increments it in one server-side round trip, avoiding race conditions between a read and a write. The cost is added latency per request — usually one to two milliseconds for a Redis round trip — which is acceptable for most API gateways but may matter for ultra-low-latency paths.

Key facts

  • Tarmac has 28 System Design interview questions on this topic, 25 of them on this page, at difficulty 1–4 of 5.
  • Tarmac tracked 2,173 job postings asking for System Design in November 2026.
  • Roles asking for System Design advertise a median base salary of £86,000, across 642 job postings as of November 2026.
  • Tarmac last reviewed these System Design interview questions on 13 October 2026.

At a glance

Questions25 shown · 28 in the bank
Difficulty1–4 of 5
FormatsTrue / false, Short answer, Fill in the blank, Flashcard, Multiple choice, Multiple answer, Design exercise

What you'll review

  1. rate limiting

Practice questions

Try one before you open the answer. Pick an option and press Check; it's marked on the spot.

A token bucket rate limiter allows short bursts of traffic that exceed the steady-state refill rate, as long as the bucket has accumulated tokens.#

Options

Show answer

A token bucket rate limiter does allow short bursts above its refill rate. Unused tokens accumulate up to the bucket's capacity during idle periods, and a burst can consume them, so throughput briefly exceeds the steady refill rate until the bucket empties.

Why:

The token bucket's defining feature is burst tolerance. Tokens refill at a fixed rate, and unused tokens accumulate up to the bucket's capacity during idle periods. A burst can then consume those accumulated tokens, so the client's throughput briefly exceeds the steady refill rate until the bucket is depleted. The statement is true because it describes exactly this behavior.

What is the main purpose of rate limiting in a web application or API?#

Show answer

Rate limiting caps how many requests a single client or source can make within a given time window. Its purpose is to protect the server or API from being overwhelmed by excessive traffic, which prevents abuse, denial-of-service conditions, and resource exhaustion while keeping the service available for legitimate users.

Why:

Rate limiting enforces a maximum number of requests a client may make in a fixed time window, so that no single source can monopolize or exhaust server resources. This defends against abuse, accidental flooding, and denial-of-service attacks, and it keeps capacity available for legitimate traffic. The core idea is protecting the service from excessive request volume from any one source.

In a token bucket rate limiter, what condition must be met for an incoming request to be allowed through?#

Show answer

At least one token must be available in the bucket at the time of the request. If the bucket is empty, the request is denied or queued until a token is refilled.

Why:

A token bucket rate limiter admits a request only when the bucket holds at least one token at the moment the request arrives. The admitted request consumes one token, and the bucket is refilled over time at a fixed rate. When the bucket is empty, no token can be consumed, so the request cannot be admitted immediately and must be rejected or delayed until a new token is added.

Which rate limiting algorithm divides time into fixed intervals and counts all requests arriving within each interval, resetting the counter to zero at the start of every new interval?#

Show answer

Fixed window counter

Why:

The fixed window counter algorithm partitions time into fixed intervals (e.g., one second, one minute) and counts requests per interval. The counter resets at each boundary, which can cause bursts at window edges — a limitation that the sliding window algorithm addresses.

The _____ _____ algorithm maintains a bucket that holds up to a maximum number of tokens. Tokens are added at a fixed refill rate, and each incoming request consumes one token; if the bucket is empty, the request is rejected or delayed.#

Show answer

The token bucket algorithm maintains a bucket that holds up to a maximum number of tokens. Tokens are added at a fixed refill rate, and each incoming request consumes one token; if the bucket is empty, the request is rejected or delayed.

Why:

The algorithm described is the token bucket. The bucket capacity caps the burst size a client can send at once, while the fixed refill rate sets the long-term average throughput. Because tokens accumulate while traffic is idle, short bursts are allowed up to the capacity, and sustained traffic is held to the refill rate.

Explain the token bucket rate limiting algorithm: how it works, what the bucket capacity and refill rate each control, and why it allows bursts.#

Show answer

The token bucket algorithm holds up to N tokens, refilled at R tokens per second. Each request consumes one token. Empty bucket → reject/delay. Capacity N = max burst; refill rate R = steady throughput. It permits bursts but averages to R over time.

Why:

The token bucket decouples burst capacity (bucket size) from long-term rate (refill rate). Tokens accumulate during idle periods up to the capacity, enabling bursts; the steady refill rate limits average throughput.

Compare fixed window counter vs sliding window log vs sliding window counter: what does each store, and what are the key trade-offs?#

Show answer

Fixed window counter: stores a single counter per key per window; simple and O(1) memory per key, but suffers from burst-at-boundary — a single client can spend its full quota at the end of one window and again at the start of the next, roughly doubling the rate across the boundary. Sliding window log: stores timestamps of every request; most accurate, but higher memory (O(N) per key) and cleanup is amortized O(1) per request with a deque or O(log N) with a sorted set. Sliding window counter: stores counts for the current and previous fixed windows; approximates a sliding window using a weighted blend of those counts, balancing accuracy and memory.

Why:

These three algorithms form a spectrum from simple-but-coarse to accurate-but-memory-intensive to a balanced hybrid. Fixed window stores one counter per key per window (O(1) memory) but allows a boundary burst: a single client exhausting its quota at the end of one window and again at the start of the next roughly doubles the instantaneous rate. Sliding window log stores per-request timestamps (O(N) memory per key) for the most accurate limit, with cleanup that is amortized O(1) per request using a deque or O(log N) using a sorted set. Sliding window counter stores counts for the current and previous windows and blends them, trading some accuracy for far less memory than the log approach.

Which rate-limiting algorithm uses a fixed-capacity pool of tokens that refill at a constant rate, allowing short bursts of requests up to the pool's capacity while bounding the average rate over time?#

Show answer

The token bucket algorithm. It maintains a fixed-capacity pool of tokens that refill at a constant rate; each request consumes one token, so short bursts up to the bucket's capacity are allowed while the average rate stays bounded.

Why:

The token bucket algorithm holds up to N tokens (the bucket capacity) and adds tokens at a steady refill rate R. Each incoming request removes one token; if the bucket is empty the request is rejected or delayed. This design permits bursts up to the bucket capacity while maintaining an average rate of R requests per unit time. The leaky bucket algorithm, modeled as a queue, smooths all traffic into a steady stream and does not allow bursts. Fixed-window and sliding-window counters track request counts within time windows but do not model a refillable capacity pool.

What is the Token Bucket algorithm and how does it work?#

Show answer

Token Bucket is a rate-limiting algorithm where tokens are added to a bucket at a fixed refill rate. Each request consumes one token; if the bucket is empty, the request is rejected. The bucket has a maximum capacity, so tokens can accumulate up to that cap during idle periods, allowing short bursts of traffic up to the bucket size. This makes it well-suited for APIs that need to allow bursts while enforcing an average rate over time. Alternative approaches include fixed-window counters and sliding-window logs.

Why:

Token Bucket is the most widely taught rate-limiting pattern in system design interviews. Its key property is that it separates average throughput (governed by the refill rate) from burst tolerance (governed by the bucket capacity). This distinguishes it from a simple fixed-window counter, which rejects any request beyond a fixed count per window regardless of prior idle time. At difficulty 1, the goal is to recall the core mechanism: tokens refill continuously, requests consume tokens, and an empty bucket means the request is denied.

Name the rate-limiting algorithm in which a bucket accumulates tokens at a constant refill rate, each request consumes one token, and the bucket's capacity sets the largest allowed burst while the refill rate sets the long-term average rate.#

Show answer

Token Bucket

Why:

The algorithm described is the Token Bucket. Tokens are added at a constant refill rate up to the bucket's capacity, and each request consumes one token, so a burst of requests can be served as long as tokens remain, up to the capacity. Over the long term, the average admitted rate equals the refill rate, which enforces the steady rate. The prompt names the mechanism by its tokens, which pins the answer to this algorithm: a Leaky Bucket run as a meter behaves equivalently and also permits bursts up to its capacity, but it is described in terms of a leak rather than tokens. A Leaky Bucket used as a queue or shaper smooths output to a constant rate and does not permit bursts. A Fixed Window Counter has no bucket, though its count can allow up to twice the limit across a window edge. A Sliding Window Log stores individual request timestamps for precise control, at higher memory cost.

In the Fixed Window Counter rate-limiting algorithm, time is divided into equal-length intervals and a counter tracks requests within each interval. A known drawback is that a burst can occur at the boundary of two consecutive windows. Explain this drawback: how many times the configured limit can be sent in a brief period, and which algorithm family mitigates the issue by not fully resetting the count at each window boundary?#

Show answer

A burst can occur at the boundary of two consecutive windows, allowing up to 2 times (double) the configured limit in a brief period. The Sliding Window approaches (Sliding Window Counter and Sliding Window Log) mitigate this by considering the previous window's count (weighted) or by logging individual timestamps, so the limit is never fully reset at a hard boundary.

Why:

The Fixed Window Counter resets its counter at each window boundary. If the limit is N requests per window, a client can send N requests just before a boundary and another N just after, yielding up to 2N requests in a very short interval, roughly double the configured rate. The Sliding Window Counter and Sliding Window Log algorithms address this by considering the previous window's count (weighted) or by logging individual timestamps, so the limit is never fully reset at a hard boundary.

True or false: With a fixed-window counter rate limiter that allows N requests per window, a burst of requests straddling the boundary between two consecutive windows can cause up to 2N requests to be served within a short interval.#

Options

Show answer

A fixed-window counter rate limiter can serve up to twice its per-window limit in a short interval. Clients can send N requests just before a window ends and N more right after the counter resets at the next window's start. Sliding-window algorithms are a common fix for this boundary burst.

Why:

The fixed-window counter resets its count to zero at the start of each window. A client can send N requests just before a window ends, all of which are allowed because the count stays within the limit. Immediately after the reset at the start of the next window, the client can send N more, all allowed. That yields up to 2N served requests across a short interval that straddles the boundary. This is a well-known drawback of the fixed-window approach, which sliding-window algorithms mitigate.

In rate limiting, the _____ algorithm smooths incoming traffic by enqueuing requests and releasing them at a constant output rate, in contrast to the token bucket algorithm, which allows bursts up to the bucket's capacity.#

Show answer

In rate limiting, the leaky bucket algorithm smooths incoming traffic by enqueuing requests and releasing them at a constant output rate, in contrast to the token bucket algorithm, which allows bursts up to the bucket's capacity.

Why:

The leaky bucket algorithm, in its queuing or metering variant, enqueues incoming requests and processes them at a fixed output rate, which smooths bursts regardless of how requests arrive. The token bucket, by contrast, accumulates tokens and lets a burst through up to the bucket's capacity before throttling. The sentence describes constant-rate smoothing, so the blank is the leaky bucket algorithm.

In the sliding-window log rate-limiting algorithm, what does the limiter store per client, and how does it decide whether to allow or reject a new request?#

Show answer

The limiter stores a log of request timestamps, one per recent request, for each client, typically in a sorted list or deque. When a new request arrives, it first removes all timestamps older than the current window start. If the remaining count is below the configured limit, it appends the new request's timestamp and allows the request; otherwise it rejects it.

Why:

The sliding-window log keeps a per-client record of individual request timestamps within a rolling time window. On each incoming request, it evicts timestamps that fall outside the window, then checks whether the remaining count is under the limit. If so, it records the new timestamp and allows the request; if not, it rejects it. This approach avoids the fixed-window boundary problem but requires storing and pruning a timestamp per request, which costs more memory than counter-based schemes.

Which rate limiting algorithm is specifically designed to permit short traffic bursts up to a configured maximum, while still enforcing a configurable long-term average rate?#

Options

Show answer

Token bucket is the rate limiting algorithm built to allow short bursts up to a configured bucket capacity while enforcing a configurable long-term average rate set by its refill rate. Leaky bucket queues traffic to a constant output rate, and fixed window and sliding window log have no dedicated burst-capacity parameter.

Why:

The token bucket holds up to a fixed number of tokens and refills them at a fixed rate. Each request consumes a token, so a burst of requests passes as long as tokens remain, up to the bucket capacity. The refill rate fixes the long-term average throughput. The leaky bucket implemented as a queue drains at a constant output rate, so it smooths traffic and does not permit bursts by design. The fixed window counter resets at window boundaries and can double-count at edges, but it has no burst-size parameter. The sliding window log stores individual request timestamps for precise counting, but it has no explicit burst-capacity concept.

In the token bucket rate limiting algorithm, the _____ caps how many tokens can accumulate, which sets the maximum burst size, while the _____ sets the steady-state average rate at which new tokens are added.#

Show answer

In the token bucket rate limiting algorithm, the bucket capacity caps how many tokens can accumulate, which sets the maximum burst size, while the refill rate sets the steady-state average rate at which new tokens are added.

Why:

In the token bucket algorithm, each request consumes one or more tokens from a bucket. The bucket's capacity (also called its size or depth) is the maximum number of tokens it can hold, so it caps how many unused tokens can accumulate and therefore bounds the burst a client can send at once. Tokens are added at a fixed refill rate (also called the token generation rate), which determines the long-term average throughput. If the bucket is full, newly generated tokens are discarded; if it is empty, requests are rate-limited until a token becomes available. Every common name for each concept is accepted, so either the capacity-related or rate-related wording is marked correct.

An API must allow clients a sustained rate of 10 req/s but also tolerate short bursts (e.g. a client firing 50 requests after being idle). Which rate-limiting algorithm naturally supports this?#

Options

Show answer

Use a token bucket. It refills tokens at the sustained rate and lets unused tokens accumulate up to the bucket size, so an idle client banks capacity and can spend it in a burst — exactly the behaviour required. A leaky bucket deliberately flattens bursts to a fixed drain rate, and a fixed-window counter is bursty across boundaries in the wrong way.

Why:

Token bucket refills tokens at the sustained rate and lets unused tokens accumulate up to the bucket size, so an idle client banks capacity and can spend it in a burst — exactly the behaviour required. A leaky bucket smooths output to a fixed drain rate, deliberately flattening bursts, which is the opposite of what we want. A fixed window counter is bursty in the wrong way: it permits double the limit across a window boundary while still rejecting legitimate bursts inside a window. A concurrency semaphore of 1 limits parallelism, not request rate, and would serialise the client rather than rate-limit it.

Design a distributed rate-limiting service in front of a fleet of API servers. It must enforce per-API-key quotas (for example, 1000 requests per minute per key) across many application instances, at a peak of 50,000 RPS total. In your answer, address: (1) which rate-limiting algorithm you would choose, naming its main trade-off against one alternative; (2) how you store and coordinate shared counters across instances, including how you keep the check atomic; (3) how you handle one extreme hot key that generates a large share of the traffic, stating the accuracy trade-off (overshoot or undercount) of your approach; and (4) what the system does when the rate-limiting backend is unavailable, and which endpoints would justify a different policy.#

Show answer

Algorithm: I would use a sliding window counter. The current and previous fixed windows are combined with a weighted estimate, so the count approximates a rolling minute while storing only two integers per key. A sliding window log gives exact counts but stores a timestamp per request, which is far too much memory at 50K RPS across millions of keys. A plain fixed window can admit up to twice the limit across a window boundary. A token bucket is a reasonable alternative if the contract explicitly allows short bursts; I would only add it if product requirements call for burst allowance.

Shared state: Per-instance counters undercount, because the load balancer spreads one key's requests across instances. I would store counters in Redis, hash-partitioned by API key across several shards so the total 50K RPS is spread over nodes. Each check runs as one Lua script via EVALSHA that reads the window counts, checks the limit, and increments only if under quota. That check-and-increment is atomic, so concurrent instances cannot lose updates the way separate GET and INCR calls can. Keys carry a TTL so old windows expire.

Hot keys: A single key at very high RPS would still concentrate load on one shard, so I would not split its quota into per-shard sub-counters, since random routing with a budget of limit/K rejects requests on a sub-counter that is full while others have spare budget. Instead, each application instance leases a batch of quota (for example a few percent of the remaining window budget) from the central counter with one atomic Redis call, and consumes it locally. Overshoot is bounded by the total outstanding leases across instances, and capacity held in unused leases is temporarily undercounted until the lease expires or is returned. Once the key is denied centrally, instances cache the denial briefly, so the hot key stops generating Redis traffic.

Degradation: If Redis is unreachable, I would fail open for general read endpoints so a non-critical dependency does not block legitimate traffic. I would pair this with a circuit breaker to stop hammering Redis during the outage, alerting that pages on-call, and a per-instance local fallback that enforces the global limit divided by the expected instance count. For security- or cost-sensitive endpoints such as login, password reset, or expensive exports, I would instead fail closed, because an unlimited window there is a larger risk than rejecting some legitimate requests.

Why:

This is an open design question, so it is graded by keywords and the sample answer rather than a single key. The sample answer names a sliding window counter with a stated trade-off against a sliding log (memory versus exactness) and notes a token bucket as the alternative if bursts are allowed. It stores counters in Redis hash-partitioned by key across shards, which is the real reason for sharding: to spread the 50K RPS load across nodes. Atomicity comes from a single Lua script that checks and increments in one round trip. For the hot key, the answer avoids splitting the quota into K sub-counters with limit/K budgets, which causes false rejections from uneven random routing, and instead uses leases whose overshoot and temporary undercount are explicitly bounded. The state stays two integers per key because leases draw from a single central counter. For degradation, fail-open suits general reads and fail-closed suits security- or cost-sensitive endpoints, so the policy follows the product risk.

An API team needs to limit each client to no more than 1000 requests within any rolling 60-second window, and needs an exact count of requests in that window rather than an approximation. Which rate-limiting algorithm stores individual request timestamps and gets this exact count by evicting entries older than the window?#

Options

Show answer

Sliding window log is the rate-limiting algorithm that stores each request's timestamp and evicts entries older than the window, so the remaining count is the exact number of requests in a rolling window. Sliding window counter approximates this with weighted buckets, while token bucket and fixed window counter do not give an exact rolling-window count.

Why:

Sliding window log keeps each request's timestamp and evicts entries older than the 60-second boundary, so the number of remaining entries is the exact count of requests in the current rolling window, with no estimation or interpolation. The sliding window counter uses fixed sub-window buckets with weighted interpolation between adjacent buckets, which yields an approximate count. The token bucket enforces a steady refill rate with burst capacity rather than counting requests within a window, so it does not produce a windowed count. The fixed window counter resets at fixed boundaries, so it does not track a rolling window and can allow up to twice the limit across adjacent windows.

You are implementing rate limiting at the API gateway layer to protect backend services. Which statements are accurate about common rate-limiting algorithms and their trade-offs?#

Options

Pick every one that applies.

Show answer

Four statements are accurate: a token bucket allows short-term bursts up to the bucket capacity while enforcing an average refill rate, a sliding window log tracks per-request timestamps and prevents the burst-at-boundary problem, a fixed window counter can allow up to 2× the limit at a window boundary, and distributed rate limiting requires a shared counter (e.g. in Redis) to enforce limits accurately. Token buckets do not guarantee zero burstiness — that is the leaky bucket.

Why:

Token buckets are the canonical burst-tolerant algorithm: tokens accumulate at the refill rate up to bucket capacity, so a burst of capacity tokens is permitted before the rate kicks in. The sliding window log solves the boundary problem by tracking exact timestamps, so a burst at the end of one period plus the start of the next can't exceed the limit. The fixed window boundary exploit is a real and well-documented flaw: if the limit is 100/minute and 100 requests arrive in the last second of window 1 and 100 in the first second of window 2, you've accepted 200 in two seconds. Distributed coordination is essential in multi-node gateways — without a shared counter each node enforces the limit independently, effectively multiplying it by the number of nodes. The claim that token bucket guarantees zero burstiness is wrong: token bucket explicitly allows bursts; the algorithm that spaces requests exactly 1/rate apart is the leaky bucket (or its fixed-rate output variant), not the token bucket.

The _____ rate-limiting algorithm refills capacity at a steady rate while permitting short bursts up to its capacity, whereas the _____ algorithm smooths output to a strictly constant rate by draining queued requests at a fixed pace.#

Show answer

The token bucket rate-limiting algorithm refills capacity at a steady rate while permitting short bursts up to its capacity, whereas the leaky bucket algorithm smooths output to a strictly constant rate by draining queued requests at a fixed pace.

Why:

Token bucket adds tokens at a fixed rate up to a maximum; each request consumes a token, so accumulated tokens allow bursts while the average stays bounded — this is the most common API limiter. Leaky bucket processes requests out of a queue at a constant rate regardless of arrival pattern, which shapes traffic into a smooth stream but offers no burst allowance. Sliding-window counters are a third common option that trade a little accuracy for cheap memory.

Design a distributed rate limiter for a public API gateway. Every incoming request must be checked against a per-API-key quota before it reaches a backend.#

Show answer

Requirements. The limit is per API key over a rolling window (1,000/min), checked on every request. I'd confirm: is a brief burst above the steady rate acceptable (token bucket says yes, up to the bucket size), and — most importantly — if the counter store is unavailable, do we fail open (let requests through, protect availability) or fail closed (reject, protect the backend)? For a public gateway I default to fail-open with alerting, since rate limiting is a guardrail, not the product.

Algorithm. I'd use a sliding-window counter (or token bucket). Fixed-window counters are simplest but allow ~2× the limit across a boundary: a client sends 1,000 in the last second of minute N and another 1,000 in the first second of N+1. The sliding-window counter weights the previous window's count by the overlap fraction, smoothing that out at a fraction of the cost of a full per-request log. Token bucket is the equivalent framing when you want to allow controlled bursts: refill at limit/60 per second, cap the bucket at the burst size.

Distributed counter. Counters live in a shared in-memory store (Redis) so all gateway nodes see the same count. The check must be atomic — a read-modify-write from two nodes races and under-counts — so I use INCR + EXPIRE (or a small Lua script that does increment, TTL, and the limit comparison in one round trip). The API key is the Redis key, so all operations for one key land on one shard.

Accuracy vs latency. A central store is exact but adds a network hop to every request. To hold <5 ms p99, I keep the store cell-local (one Redis per region/cell, no cross-region hop) and accept that limits are enforced per-region. If even one hop is too much, the alternative is local per-node token buckets that sync counts every few hundred ms — fast and highly available, but approximate (a key can briefly exceed the global limit by roughly the number of nodes). I'd start central-per-cell and only move to local+sync if the hop proves too costly.

Failure modes. Store down: fail open, serve requests un-limited, and alert. Hot key: a single key's counter is one Redis key on one shard, so a 200K-req/sec key hammers that shard — I'd detect it and shard that key's counter into N sub-counters (key:0..N) summed on read, or shed it at the edge. Clock skew: window algorithms key off time, so I rely on the store's clock (single source) rather than each node's wall clock.

Scaling. At ~500K ops/sec the counter store is sharded by key across a Redis cluster; because each key's ops stay on its shard, adding shards scales throughput linearly. Replicas give availability, and per-cell stores keep latency flat as we add regions. The bottleneck is always the hottest single key, handled by sub-sharding as above.

Why:

A distributed rate limiter is the canonical 'shared mutable counter in the hot path' problem. The two decisions interviewers probe are the algorithm (sliding-window counter or token bucket, because fixed-window allows a 2× burst across the window boundary) and where the counter lives (a shared atomic store is exact but adds a hop; local counters with sync are fast but approximate — the <5 ms budget forces you to pick a point on that spectrum). The atomic increment-and-check (INCR+EXPIRE or Lua) is what prevents concurrent nodes from racing and under-counting. The dominant failure modes are the fail-open/fail-closed choice when the store dies and the hot-key problem, where one key's counter overloads its single shard.

A per-API-key rate limiter stores two counters in Redis Cluster: a requests-per-second bucket and a requests-per-minute bucket. One Lua script checks and decrements both. Explain (1) how Redis Cluster assigns keys to shards, and what key naming you would use so both counters for one API key are guaranteed to live on the same shard (the script would otherwise fail with a CROSSSLOT error), and (2) why the check-and-decrement must run as a single atomic script rather than as separate GET and DECR calls.#

Show answer

Redis Cluster divides the keyspace into 16384 hash slots. A key's slot is CRC16(key) mod 16384, except that if the key contains a non-empty substring inside {}, only that substring is hashed. So I would name the keys rl:{apikey}:rps and rl:{apikey}:rpm, using the API key as the hash tag. Both keys then map to the same slot and shard, and the Lua script can touch both without a CROSSSLOT error. For atomicity, a naive GET, check, then DECR lets two concurrent requests both read one remaining token and both decrement, over-allowing. A single Lua script runs atomically on the Redis server, so the read, the check, and both decrements happen with no other command interleaved, and no distributed lock is needed.

Why:

The question asks for two precise Redis facts. Redis Cluster does not use consistent hashing; it assigns each key to one of 16384 slots using CRC16 of the key, and a hash tag (the substring inside braces) restricts hashing to that substring. Using the API key as a hash tag puts both the per-second and per-minute counters in the same slot, so a multi-key Lua script runs without CROSSSLOT errors. The second part is answered by the race condition: separate read and write commands let concurrent requests interleave, while a Lua script executes atomically on the server, so the check-and-decrement cannot be split.

In a horizontally-scaled API gateway with N instances behind a round-robin load balancer, each instance independently enforcing the full intended per-IP rate limit in local memory will over-allow requests. Explain the root cause, then describe one standard production approach that enforces the intended limit across all instances.#

Show answer

Round-robin distribution means each gateway instance sees only about 1/N of a given client's requests, so its local counter tracks only a fraction of the true traffic. Because every instance is configured with the full limit, a client can effectively make up to N times the intended number of requests. One standard fix is to move the counter into a shared, low-latency store such as Redis, so every instance reads and updates the same per-IP count, and to make each check-and-increment atomic so concurrent updates cannot race. An alternative is session affinity (for example, consistent-hash routing by client IP), which sends each client's requests to one instance so its local counter sees all of that client's traffic.

Why:

With round-robin load balancing, each instance receives roughly 1/N of any client's requests, so its local counter sees only a fraction of the real volume. Since each instance enforces the full configured limit, the aggregate allowance grows to about N times the intended value. A standard production fix is to keep rate-limit state in a shared store such as Redis so all instances count against the same per-IP counter, with each update performed atomically to avoid races between concurrent instances. Session affinity by client IP is an accepted alternative because it routes each client to one instance, giving that instance the full traffic picture.

Which of the following statements about common rate-limiting algorithms and their distributed implementations are true? Select all that apply.#

Options

Pick every one that applies.

Show answer

Token buckets allow bursts up to their capacity while still enforcing a long-term average rate. Fixed-window counters can admit up to twice the configured rate across a window boundary, and a Lua script makes a Redis token-bucket check-and-decrement atomic. The sliding-window counter is only an approximation; the sliding-window log is the exact variant.

Why:

The three correct statements are that a token bucket permits bursts up to its capacity while regulating the long-term refill rate, that wrapping the Redis token-bucket read and DECRBY in a Lua script makes the check-and-decrement atomic because Redis runs scripts in its single-threaded event loop, and that a fixed-window counter can admit up to 2x the nominal rate when a full quota is used at the end of one window and another at the start of the next. The statement that the sliding-window counter is an exact limiter that never exceeds the configured rate is false: the sliding-window counter is an approximation that weights the current and previous fixed-window counts, and only the sliding-window log (which stores every request timestamp) is exact.

Related interview questions

Job market

See system-design salaries and hiring demand from live job postings.

The other 3 questions

This page shows 25 and marks what you pick. That's as far as a page can go. A free account opens the other 3 and keeps every answer. What you miss comes back until it's right: after a day, then at longer gaps.

Start with this topic

Free · the whole bank · 100 answers per 30 days, each marked with written feedback

Interview booked? Practise the job ad instead

What moved, monthly

One email a month when the bulletin comes out: what moved in the markets we track, and the new question topics we published. Confirm your address to join. Unsubscribe any time.