JobVora

System Design Interview Guide: How to Design a Rate Limiter

System Design Interview Guide: How to Design a Rate Limiter

Maruf Mehar · System Design · 5 min read · Updated Nov 26, 2026

Short answer: Use a token bucket per client stored in Redis, execute check-and-decrement via an atomic Lua script, shard by client ID, and define explicit fail-open vs fail-closed rules.

Short answer: Use a token bucket per client, stored in Redis, with an atomic Lua script for the check, and shard keys by client ID.

A candidate recently reported getting this exact question in our Microsoft 3+ YOE interview experience — designing an API gateway rate limiter handling 250,000 requests per second. You can also compare similar backend rounds in our Amazon interview breakdown and full Software Engineering Interviews hub.

1. Clarify Requirements

  • Limit per tenant, per user, or per API key?
  • Should short traffic bursts be allowed?
  • When exceeded, do we reject immediately with HTTP 429 Too Many Requests or queue?

2. Do Quick Capacity Estimates

If you track roughly 1 million active keys at around 100 bytes per key, total state is only ~100 MB — which fits comfortably in a Redis cluster. Say these numbers out loud early so the interviewer sees strong systems intuition.

3. Pick an Algorithm

AlgorithmGood forWeakness
Fixed windowSimplicityTraffic spikes at window boundaries
Sliding windowHigh boundary accuracyHigher memory per key
Token bucketControlled bursts, industry defaultRequires accurate refill calculation

4. Design the Data Flow

Store tokens and last_refill_ts per client key in Redis. Execute refill calculation, limit check, and decrement inside a single atomic Redis Lua script so concurrent requests from the same client cannot both claim the final token.

5. Scale Across Shards

Shard keys by tenant or user ID using consistent hashing. Add read replicas for availability and failover, and keep a short-TTL local in-memory cache for extreme hot keys if approximate global synchronization is acceptable.

6. Handle Failure Gracefully

If a Redis shard becomes unreachable, choose explicitly between fail open (let traffic pass, risk downstream overload) or fail closed (reject requests, protect backend, risk temporary unavailability) and tie that decision directly to CAP theorem trade-offs.

Common Mistakes to Avoid

Skipping requirement clarification, avoiding back-of-the-envelope estimates, and ignoring datastore failover scenarios. In the reported Microsoft round, geo-distributed database failover behavior was a major follow-up probe, so prepare replication lag and split-brain edge cases too.

Frequently Asked Questions

What is the best algorithm for a rate limiter? Token bucket is the most common production choice because it supports controlled bursts with minimal per-key state.

Why Redis? Redis offers sub-millisecond in-memory reads/writes and atomic Lua execution to eliminate race conditions under high QPS.

Fail open or fail closed? Choose based on risk: security-critical endpoints typically fail closed, while user-facing product feeds usually fail open.

Recommended Career Guides