📖 Scenario: You are building a web service that needs to limit how many requests a user can make in a short time to prevent overload and abuse.We will use Redis to implement a sliding window rate limiter that tracks requests per user in real-time.
🎯 Goal: Create a Redis data structure and commands to track user requests and enforce a sliding window rate limit of 5 requests per 10 seconds.
📋 What You'll Learn
Create a Redis sorted set to store timestamps of user requests
Add a configuration variable for the time window size (10 seconds)
Write a Redis command sequence to remove old requests outside the window
Add a command to count current requests and allow or deny new requests based on the limit
💡 Why This Matters
🌍 Real World
Rate limiting protects web services from too many requests in a short time, preventing overload and abuse.
💼 Career
Understanding Redis data structures and commands for rate limiting is valuable for backend developers and system engineers.
Progress0 / 4 steps
1
Create a Redis sorted set key for user requests
Create a Redis sorted set key named user:123:requests to store timestamps of requests for user ID 123. Use the current Unix timestamp in seconds as the score and member. Add one request with timestamp 1000.
Redis
Hint
Use the ZADD command with the key user:123:requests, score 1000, and member 1000.
2
Set the sliding window size configuration
Create a Redis string key named rate_limit:window and set its value to 10 to represent the sliding window size in seconds.
Redis
Hint
Use the SET command with key rate_limit:window and value 10.
3
Remove old requests outside the sliding window
Write a Redis command to remove all entries from user:123:requests with scores less than 990 (current timestamp 1000 minus window size 10). Use the ZREMRANGEBYSCORE command.
Redis
Hint
Use ZREMRANGEBYSCORE with -inf as the minimum and 989 as the maximum score.
4
Check current request count and add new request if allowed
Use ZCARD user:123:requests to get the current number of requests in the window. If the count is less than 5, add a new request with timestamp 1001 using ZADD. Otherwise, do not add the request.
Redis
Hint
Use ZCARD to count requests and ZADD to add the new request if count < 5.
Practice
(1/5)
1. What is the main purpose of using a sliding window in rate limiting with Redis?
easy
A. To count actions within a moving time frame for fair limits
B. To store user passwords securely
C. To permanently block users after one action
D. To sort data alphabetically
Solution
Step 1: Understand sliding window concept
Sliding window rate limiting counts actions in a recent time frame that moves forward as time passes.
Step 2: Identify purpose in Redis context
Redis uses sorted sets to track timestamps of actions to enforce limits fairly over time.
Final Answer:
To count actions within a moving time frame for fair limits -> Option A
Quick Check:
Sliding window = moving time frame count [OK]
Hint: Sliding window means counting recent actions, not all time [OK]
Common Mistakes:
Thinking sliding window blocks permanently
Confusing with password storage
Assuming it sorts data alphabetically
2. Which Redis command is essential to add a timestamp to a sorted set for sliding window rate limiting?
easy
A. ZADD
B. GET
C. HSET
D. LPUSH
Solution
Step 1: Identify command to add to sorted set
ZADD adds members with scores (timestamps) to a sorted set in Redis.
Step 2: Confirm other commands' purpose
GET retrieves string values, HSET sets hash fields, LPUSH adds to lists, none add to sorted sets.
Final Answer:
ZADD -> Option A
Quick Check:
ZADD adds to sorted set [OK]
Hint: Use ZADD to add timestamps to sorted sets [OK]
Common Mistakes:
Using GET which only reads strings
Confusing HSET (hashes) with sorted sets
Using LPUSH which is for lists
3. Given this Redis Lua script snippet for sliding window rate limiting:
local now = tonumber(redis.call('TIME')[1])
redis.call('ZREMRANGEBYSCORE', key, 0, now - window)
local count = redis.call('ZCARD', key)
if count < limit then
redis.call('ZADD', key, now, now)
return 1
else
return 0
end
What will be the return value if the current count is equal to the limit?
medium
A. 1
B. nil
C. 0
D. An error
Solution
Step 1: Analyze the condition count < limit
If count is equal to limit, the condition count < limit is false.
Step 2: Determine return value when condition is false
When false, the script returns 0, indicating rate limit reached.
Final Answer:
0 -> Option C
Quick Check:
count == limit returns 0 [OK]
Hint: If count equals limit, return 0 (limit reached) [OK]
Common Mistakes:
Assuming count == limit returns 1
Expecting an error instead of 0
Thinking nil is returned
4. You have this Redis Lua script for sliding window rate limiting:
local now = tonumber(redis.call('TIME')[1])
redis.call('ZREMRANGEBYSCORE', key, 0, now - window)
local count = redis.call('ZCARD', key)
if count <= limit then
redis.call('ZADD', key, now, now)
return 1
else
return 0
end
What is the bug in this script?
medium
A. ZREMRANGEBYSCORE is used incorrectly
B. Using <= allows one extra request beyond limit
C. ZADD should use a string member, not number
D. TIME command returns a string, not number
Solution
Step 1: Check condition count <= limit
This condition allows adding a request when count equals limit, exceeding limit by one.
Step 2: Verify other commands usage
ZREMRANGEBYSCORE usage is correct to remove old timestamps; ZADD accepts numbers as members; TIME returns array with string seconds converted to number.
Final Answer:
Using <= allows one extra request beyond limit -> Option B
Quick Check:
count <= limit allows limit+1 requests [OK]
Hint: Use < not <= to enforce strict limit [OK]
Common Mistakes:
Thinking ZREMRANGEBYSCORE is wrong here
Believing ZADD can't use numbers as members
Confusing TIME return type
5. You want to implement a sliding window rate limiter in Redis that allows 100 requests per 60 seconds. Which sequence of Redis commands best enforces this limit efficiently?
hard
A. HSET to store count, GET to read count, INCR to increment count
B. LPUSH to add timestamp, LLEN to count, LTRIM to remove old timestamps
C. SETNX to set a flag, EXPIRE to reset after 60 seconds
D. ZREMRANGEBYSCORE to remove old timestamps, ZCARD to count, ZADD to add current timestamp
Solution
Step 1: Understand sliding window enforcement
Use sorted sets to store timestamps, remove old ones, count current requests, and add new timestamp.
Step 2: Evaluate each option's commands
ZREMRANGEBYSCORE to remove old timestamps, ZCARD to count, ZADD to add current timestamp uses ZREMRANGEBYSCORE, ZCARD, ZADD correctly for sliding window. LPUSH to add timestamp, LLEN to count, LTRIM to remove old timestamps uses lists which are less efficient for range removals. HSET to store count, GET to read count, INCR to increment count uses hashes and counters, not suitable for sliding window. SETNX to set a flag, EXPIRE to reset after 60 seconds uses simple flags, not counting requests.
Final Answer:
ZREMRANGEBYSCORE, ZCARD, ZADD sequence -> Option D