0
0
Redisquery~10 mins

Rate limiting with sliding window in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Rate limiting with sliding window
Request arrives
Get current timestamp
Calculate window start time
Remove timestamps older than window start
Count remaining timestamps
If count < limit?
NoReject request
Yes
Add current timestamp
Allow request
Each request is checked against recent requests in a time window. Old requests are removed, and if the count is below the limit, the request is allowed and timestamp recorded.
Execution Sample
Redis
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local window = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local window_start = now - window
redis.call('ZREMRANGEBYSCORE', key, 0, window_start)
local count = redis.call('ZCARD', key)
if count < limit then
  redis.call('ZADD', key, now, tostring(now))
  return 1
else
  return 0
end
This Lua script in Redis implements sliding window rate limiting by removing old timestamps, counting current ones, and deciding to allow or reject the request.
Execution Table
StepActionTimestampWindow StartOld Timestamps RemovedCount After RemovalDecisionNew Timestamp AddedResult
1Request arrives1000940Remove timestamps <= 94033 < 5? YesAdd 1000Allow (1)
2Request arrives1010950Remove timestamps <= 95044 < 5? YesAdd 1010Allow (1)
3Request arrives1020960Remove timestamps <= 96055 < 5? NoNo addReject (0)
4Request arrives1030970Remove timestamps <= 97044 < 5? YesAdd 1030Allow (1)
5Request arrives1040980Remove timestamps <= 98055 < 5? NoNo addReject (0)
💡 Execution stops after each request is processed with decision to allow or reject based on count vs limit.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
now (timestamp)N/A10001010102010301040
window_startN/A940950960970980
countN/A34545
timestamps in sorted set[][960,970,980][970,980,990,1000][980,990,1000,1010,1020][990,1000,1010,1030][1000,1010,1030,1040]
Key Moments - 3 Insights
Why do we remove timestamps older than the window start before counting?
Because only requests within the current sliding window count towards the limit. Removing old timestamps ensures we count only recent requests, as shown in execution_table rows where old timestamps are removed before counting.
What happens if the count equals the limit exactly?
The request is rejected because the limit is reached. In execution_table row 3 and 5, count is 5 which equals limit 5, so the decision is No and request is rejected.
Why do we add the current timestamp only if the request is allowed?
Adding the timestamp represents recording the allowed request. If rejected, we don't add it to avoid counting it in future windows. This is shown in execution_table where 'New Timestamp Added' is only Yes when decision is allow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What is the count after removing old timestamps?
A5
B4
C3
D6
💡 Hint
Check the 'Count After Removal' column in execution_table row 3.
At which step does the request get rejected because the count equals the limit?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look for 'Decision' column where count < limit is No and result is Reject.
If the window size was increased, how would the 'Old Timestamps Removed' column change?
AMore timestamps would be removed
BFewer timestamps would be removed
CNo timestamps would be removed
DAll timestamps would be removed
💡 Hint
Increasing window means window_start is smaller, so fewer timestamps fall outside the window.
Concept Snapshot
Rate limiting with sliding window in Redis:
- Use sorted set to store request timestamps
- Remove timestamps older than (now - window)
- Count remaining timestamps
- If count < limit, allow and add current timestamp
- Else reject request
This ensures smooth rate limiting over time.
Full Transcript
This visual execution shows how sliding window rate limiting works in Redis. Each request gets the current time and calculates the window start by subtracting the window size. Old timestamps outside the window are removed from the sorted set. Then the count of timestamps inside the window is checked. If the count is less than the limit, the request is allowed and the current timestamp is added to the sorted set. Otherwise, the request is rejected. The execution table traces five requests with timestamps, showing removal of old timestamps, counts, decisions, and additions. The variable tracker shows how timestamps and counts change after each request. Key moments clarify why old timestamps are removed, what happens when count equals limit, and why timestamps are added only on allowed requests. The quiz tests understanding of counts, rejection steps, and window size effects. The snapshot summarizes the sliding window rate limiting steps in Redis.