Concept Simulators / Keeping a service up
Rate limiting
Fixed window, sliding window, token bucket and leaky bucket against bursts.
On the roadmap:Intermediate · step 6: Protecting services
Settings
Counts requests in each whole second and starts again at zero on the boundary. Cheap, but blind to what happens across a boundary.
What the traffic does
Let through
100%
Refused
0
Busiest second
20 (limit 10)
Average wait
0 ms
The busiest second let 20 requests through against a limit of 10. A fixed window resets at the boundary, so bursts either side of it both pass. Try a sliding window.
Fixed window over 20 seconds
In plain words
A rate limiter decides how many requests a client may make in a stretch of time and turns the rest away. The simple versions have blind spots, and the way a limiter counts changes what it lets through when traffic arrives in a burst.
Why it matters
Limits protect a service from one noisy client, a bug that loops, or an attack. Here the same stream of requests meets five limiters, so you can see what each lets through in its busiest second and what it does to the requests it holds back. Times run over 20 seconds.
How real systems do it
The simulation is a simplified model. Here is what production systems add or do.
- Stripe describes using token buckets for its request rate limiters, and returns HTTP 429 when a client goes over.
- Amazon API Gateway throttles with a token bucket model: a steady rate and a burst allowance.
- NGINX's limit_req module is described as using the leaky bucket method, with an optional burst of requests that are queued.
- A rejected request should say how long to wait. HTTP has the 429 status and a Retry-After header for exactly this, and good clients back off when they see it.
Study: Rate limiting
Once you have played with it, learn the same idea from people who build these systems. Watch first for the picture, then read for the detail.
35 minSystem Design Interview - Rate Limiting (local and distributed)
System Design Interview · 344K views
Then
36 minRate Limiting system design | TOKEN BUCKET, Leaky Bucket, Sliding LogsTech Dummies - Narendra Lakshmana Gowda · 326K views
56 minDesign a Distributed Rate Limiter w/ a Ex-Meta Staff Engineer: System Design BreakdownHello Interview · 308K views
23 minSystem Design Mock Interview: Design a Rate Limiter (with Meta Engineering Manager)Aced (formerly Exponent) · 244K views
8 minRate Limiter System Design: Token Bucket, Leaky Bucket, ScalingByteByteGo · 95K views