Protect services with rate limiting
Compare limiter algorithms and run one across many servers.
- Compare fixed window, sliding window, and token bucket limiters.
- Explain how burst size and sustained rate map to a token bucket.
- Design a rate limiter shared by many API servers.
A rate limiter caps how many requests a client (by user, API key, or IP) can make in a period. It protects services from abuse and overload, enforces fair use and pricing tiers, and limits the blast radius of a buggy client. Rejected requests get HTTP 429 Too Many Requests, ideally with a Retry-After header.
Limiter algorithms
- Fixed window counter: count requests per clock window (for example per minute). Simple and cheap, but a client can send a full limit at the end of one window and another at the start of the next - about 2× in a short span.
- Sliding window log: store each request’s timestamp and count those within the last window. Accurate, but memory grows with traffic.
- Sliding window counter: blend the current and previous window counts. A cheap, good approximation.
- Token bucket: a bucket holds up to capacity tokens and refills at a steady rate. Each request spends a token. Capacity sets the allowed burst; the refill rate sets the sustained throughput.
A worked example: with a limit of 3 per minute, requests at seconds 1, 2, 2, 3 and 3 give three allowed and two rejected. A request at second 61 falls in the next window, so it’s allowed.
With many API servers, each server’s local counter only sees part of the traffic. A distributed limiter keeps counters in a shared, fast store such as Redis, using atomic operations (INCR with an expiry, or a Lua script for a token bucket) so concurrent requests cannot race. Limiters usually live in an API gateway or middleware, and should fail open or closed deliberately if the store is unavailable.
Practice: compare limiters
Try it
Up to 15 XPWhich limiter holds the line?
The limit is 5 requests per 60 seconds. Green dots pass, and red dots get a 429.
- In Boundary burst, compare the worst window for a fixed window and a sliding log.
- What does the sliding log pay for that accuracy?
- Try Steady with a token bucket. How does it differ from the fixed window?
- Limit
- 5 per 60 seconds
- Most allowed in any window
- 10
- Stored per client
- One counter
Try it
Up to 15 XPFind the flaw in this design
A candidate describes their limiter. Click what you’d challenge as the interviewer.
Click every suspicious section. There are 3.
Key takeaways
Token bucket: capacity is the burst, refill rate is the sustained limit.
Fixed windows are cheap but allow boundary bursts.
Distributed limiters need a shared store and atomic updates.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…