0
0
Redisquery~10 mins

Rate limiting with sliding window in Redis - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add the current timestamp to the sorted set for rate limiting.

Redis
redis.call('ZADD', 'user:[1]:requests', current_time, current_time)
Drag options to blanks, or click blank then click option'
A123
Blimit
Ctimestamp
Duser123
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic word like 'timestamp' instead of a user ID.
Using 'user123' which results in malformed key 'user:user123:requests'.
2fill in blank
medium

Complete the code to remove timestamps older than the sliding window duration.

Redis
redis.call('ZREMRANGEBYSCORE', 'user:123:requests', 0, current_time [1] window_size)
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction.
Using multiplication or division which are incorrect here.
3fill in blank
hard

Fix the error in the code to count requests within the sliding window.

Redis
local count = redis.call('ZCOUNT', 'user:123:requests', current_time [1] window_size, current_time)
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction in the range.
Using multiplication or division which are invalid here.
4fill in blank
hard

Fill both blanks to complete the Lua script that enforces rate limiting with sliding window.

Redis
if count [1] limit then
  return [2]
end
Drag options to blanks, or click blank then click option'
A>=
Btrue
C<=
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' for the condition.
Returning true to block the request instead of false.
5fill in blank
hard

Fill all three blanks to complete the Lua script that adds the request timestamp, cleans old entries, and returns true if allowed.

Redis
redis.call('ZREMRANGEBYSCORE', 'user:123:requests', 0, current_time [1] window_size)
local count = redis.call('ZCOUNT', 'user:123:requests', current_time [2] window_size, current_time)
if count [3] limit then
  return false
end
redis.call('ZADD', 'user:123:requests', current_time, current_time)
return true
Drag options to blanks, or click blank then click option'
A-
B+
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction for score ranges.
Using '<=' instead of '>=' in the if condition.