System design fundamentals learning path Level 2 This level · 2 of 20
Medium system-design fundamentals

How does rate limiting work, and which algorithm would you choose?

The 30 second answer

A rate limiter caps how many requests a client can make in a period and rejects the excess with 429 Too Many Requests, ideally with a Retry-After header. Token bucket is my default: tokens refill at a steady rate, each request takes one, and the bucket size allows short bursts. Fixed windows are simplest but allow double bursts at the window edges, and a sliding window fixes that at a little more cost.

Could you say this in an interview?

Why you need it. Stripe describes rate limiters as protecting an API from one user overwhelming it, whether by bug or abuse, and distinguishes them from load shedders, which decide based on the state of the whole system rather than one user, so critical traffic gets through during an incident.

The algorithms.

  • Token bucket. A bucket holds up to N tokens and refills at a steady rate; each request takes a token and is rejected when the bucket is empty. Bucket size controls bursts, refill rate controls the sustained rate. Stripe uses token bucket for its request rate limiter, and AWS API Gateway throttles with the token bucket algorithm, with a steady-state rate and a burst limit.
  • Leaky bucket. Requests enter a queue that drains at a fixed rate; it smooths traffic to a constant output but adds queueing delay.
  • Fixed window counter. Count requests per client per minute and reset at the minute boundary. Simple, but a client can send a full limit at the end of one window and again at the start of the next.
  • Sliding window. Cloudflare describes approximating the rate from the previous window's count weighted by how much of it overlaps, plus the current window's count. With their example limit of 50 per minute, 42 requests last minute and 18 in the first 15 seconds of this one, the estimate is 42 × (45/60) + 18 = 49.5. It smooths the edge burst while storing only two counters.
  • Concurrency limiter. Stripe also limits how many requests a user has in flight at once, which protects expensive endpoints better than a per-second rate.

What the client sees. RFC 6585 defines 429 Too Many Requests and says the response may include Retry-After; RFC 9110 defines Retry-After as either a date or a number of seconds. The IETF HTTPAPI working group's draft adds RateLimit-Policy and RateLimit headers so clients can see their quota before they hit it.

Where it runs. At the edge or API gateway for per-client limits, and with a shared store (often Redis with atomic increments) when many gateway instances must share one count. Per-instance local limits are cheaper but only approximate a global limit.

Common mistakes interviewers listen for.

  • Returning 500 or 503 instead of 429, so clients can't tell "slow down" from "broken".
  • Limiting by IP only, which punishes users behind one NAT; limit by API key or user where possible.
  • No plan for when the rate-limit store is down: decide whether to fail open or closed.

Gateway-level strategy for auth, routing and tenants is on the senior Java and Spring Boot microservices page.

Sources (6)
  1. Stripe: Scaling your API with rate limitersstripe.com
  2. Cloudflare: How we built rate limiting capable of scaling to millions of domainsblog.cloudflare.com
  3. AWS: Throttle requests to your REST APIs in API Gatewaydocs.aws.amazon.com
  4. RFC 6585: Additional HTTP Status Codes (429 Too Many Requests)rfc-editor.org
  5. RFC 9110: HTTP Semantics (Retry-After)rfc-editor.org
  6. IETF draft: RateLimit header fields for HTTPdatatracker.ietf.org
Previous
Next