LLD Guide · Module 2: Problem prep · Lesson 02.04
Prep: Sliding Window Rate Limiter
Shared state in Redis, one atomic decision per request, and data that cleans itself up.
By Cruscible Engineering · Published
A rate limiter makes one decision, millions of times: allow or deny. The decision is only correct if counting and recording happen as one step.
This lesson prepares you for Build a Sliding Window Rate Limiter. It doesn't solve it.
1. What the rate limiter contract asks for
A limiter that allows at most maxRequests per user in any window of windowSizeSeconds, sliding, not fixed. All state lives in Redis, so several app instances would share the same limits.
| Method | Returns / does |
|---|---|
configure(maxRequests, windowSizeSeconds) | sets the limits; may be called again, and existing counts are judged by the new limits |
allowRequest(userId) | true if allowed (and it counts), false if over the limit |
getRemainingRequests(userId) | how many more are allowed now; never negative |
reset(userId) | clears that user's data |
Targets: up to 10,000 distinct users, 10,000 operations a second, P99 under 5 ms.
Figure: A sliding window: requests older than the window stop counting as it moves
- The limit is 3 requests in any 10 seconds; each allowed request is a timestamp in a sorted set.
- Requests at 1 s, 3 s and 4 s are allowed; one at 6 s is rejected because 3 are already in the window.
- By 12 s the entry from 1 s has left the window, so requests at 12 s, 13 s and 15 s are allowed.
- At 17 s the window holds 12 s, 13 s and 15 s: rejected.
2. Lessons to read before the rate limiter
- 01.03 Redis for LLD: sorted sets, Lua scripts and expiry. This problem uses all three.
- 01.02 Thread-safety: why a check followed by a write lets two requests through.
3. Rate limiter design decisions to make first
- What's in one user's key? What is the score, and what is the member, and how do two requests in the same millisecond stay two entries?
- Which steps must be one atomic unit? List the Redis commands
allowRequestneeds, and which of them theRedisClienthas directly. - What does a denied request write? Should a rejected request take a place in the window?
- How does data expire for users who stop sending? What TTL, set when?
- What does
getRemainingRequestscount? Only entries inside the current window, so it must drop old ones (or ignore them) too. - What does
configuremean for data already stored? The contract says existing counts are judged against the new limits.
4. Rate limiter pitfalls
⚠️ Trap: no in-process counters. A
ConcurrentHashMaplimiter might pass by hand, but it breaks the "shared across instances" requirement.
⚠️ Trap: a fixed window (one counter per minute) isn't a sliding window: it lets up to twice the limit through around a boundary.
⚠️ Trap:
ZREMRANGEBYSCORE,ZCARDandZADDas separate calls can let concurrent requests exceed the limit. TheRedisClienthas noZREMRANGEBYSCOREanyway: that's a hint to useeval.
⚠️ Trap:
userIdmay be an empty string. It's still a valid user, with its own key.
Before you submit
- All limiter state is in Redis, none in Java fields
- The decision and the insert happen in one atomic step
- Concurrent requests for one user never exceed
maxRequestsin any window - Old requests stop counting as the window slides
- Rejected requests don't consume a place
- Every user's key expires when the user goes idle
getRemainingRequestsis never negative and counts only the current windowresetclears exactly one user's data- A second
configurecall applies to the requests already stored - An empty
userIdworks like any other user
Further reading
After you've tried it
Submit first. The full walkthrough of one solution: Building a Sliding Window Rate Limiter with Redis.