0
0
Redisquery~20 mins

Rate limiting with sliding window in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sliding Window Rate Limiting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Sliding Window Rate Limit: Count Requests in Last Minute
Given a Redis sorted set where each member is a timestamp of a user request, which command returns the count of requests made in the last 60 seconds?
Redis
ZCOUNT user:123:requests -inf +inf
AZCOUNT user:123:requests (current_timestamp - 60) current_timestamp
BZCOUNT user:123:requests (current_timestamp - 60) +inf
CZCOUNT user:123:requests -inf (current_timestamp - 60)
DZCOUNT user:123:requests current_timestamp (current_timestamp + 60)
Attempts:
2 left
💡 Hint
Use ZCOUNT with a score range to count timestamps within the last 60 seconds.
📝 Syntax
intermediate
2:00remaining
Identify the Correct Redis Command Syntax for Sliding Window Cleanup
Which Redis command syntax correctly removes all request timestamps older than 60 seconds from the sorted set 'user:123:requests'?
AZREMRANGEBYSCORE user:123:requests (current_timestamp - 60)
BZREMRANGEBYSCORE user:123:requests (current_timestamp - 60) +inf
CZREMRANGEBYSCORE user:123:requests -inf (current_timestamp - 60)
DZREMRANGEBYSCORE user:123:requests -inf current_timestamp
Attempts:
2 left
💡 Hint
You want to remove all timestamps less than (current_timestamp - 60).
optimization
advanced
3:00remaining
Optimize Sliding Window Rate Limiting with Redis Lua Script
Which Lua script snippet atomically adds the current timestamp to a sorted set, removes timestamps older than 60 seconds, and returns the count of requests in the last 60 seconds?
Aredis.call('ZADD', KEYS[1], ARGV[1], ARGV[1]); redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1] + 60); return redis.call('ZCOUNT', KEYS[1], ARGV[1] - 60, ARGV[1])
Bredis.call('ZADD', KEYS[1], ARGV[1], ARGV[1]); redis.call('ZREMRANGEBYSCORE', KEYS[1], ARGV[1] - 60, '+inf'); return redis.call('ZCOUNT', KEYS[1], ARGV[1] - 60, ARGV[1])
Credis.call('ZADD', KEYS[1], ARGV[1], ARGV[1]); redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1] - 60); return redis.call('ZCOUNT', KEYS[1], '-inf', ARGV[1])
Dredis.call('ZADD', KEYS[1], ARGV[1], ARGV[1]); redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1] - 60); return redis.call('ZCOUNT', KEYS[1], ARGV[1] - 60, ARGV[1])
Attempts:
2 left
💡 Hint
Remove timestamps older than (current_timestamp - 60) and count timestamps in the last 60 seconds.
🔧 Debug
advanced
2:30remaining
Debug Sliding Window Rate Limiting: Unexpected High Count
A developer uses this Redis command to count requests in the last 60 seconds:

ZCOUNT user:123:requests -inf (current_timestamp - 60)

but notices the count is unexpectedly high. What is the cause?
AThe command syntax is invalid and causes a runtime error.
BThe command counts timestamps older than (current_timestamp - 60) instead of newer ones.
CThe command counts timestamps newer than (current_timestamp - 60) correctly.
DThe command counts timestamps between (current_timestamp - 60) and +inf correctly.
Attempts:
2 left
💡 Hint
Check the score range used in ZCOUNT and what it includes.
🧠 Conceptual
expert
3:00remaining
Why Use Sliding Window Rate Limiting Over Fixed Window?
Which statement best explains the advantage of sliding window rate limiting compared to fixed window rate limiting?
ASliding window provides a smoother and more accurate limit by counting requests in a moving time frame, avoiding bursts at window edges.
BFixed window rate limiting uses sorted sets, while sliding window uses simple counters.
CFixed window rate limiting counts requests continuously without resetting, unlike sliding window.
DSliding window is easier to implement and requires less memory than fixed window.
Attempts:
2 left
💡 Hint
Think about how requests are counted over time in both methods.