0
0
Redisquery~10 mins

Rate limiter with INCR and EXPIRE 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 increment the request count for a user key.

Redis
redis.call('INCR', [1])
Drag options to blanks, or click blank then click option'
Auser_key
Bexpire_time
Climit
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Using expire_time instead of the key to increment
Using limit which is a number, not a key
2fill in blank
medium

Complete the code to set the expiration time for the user key.

Redis
redis.call('EXPIRE', [1], 60)
Drag options to blanks, or click blank then click option'
Acount
Btimestamp
Cuser_key
Dlimit
Attempts:
3 left
💡 Hint
Common Mistakes
Setting expiration on a number or limit instead of the key
Using wrong key name
3fill in blank
hard

Fix the error in the code to check if the key exists before setting expiration.

Redis
if redis.call('EXISTS', [1]) == 1 then redis.call('EXPIRE', [1], 60) end
Drag options to blanks, or click blank then click option'
Alimit
Buser_key
Ccount
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Checking existence of wrong key
Using count or limit instead of key
4fill in blank
hard

Fill both blanks to create a Lua script that increments the user key and sets expiration if it's a new key.

Redis
local current = redis.call('INCR', [1])
if current == 1 then redis.call('EXPIRE', [2], 60) end
return current
Drag options to blanks, or click blank then click option'
Auser_key
Blimit
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for increment and expire
Using limit or timestamp instead of user_key
5fill in blank
hard

Fill all three blanks to complete the Lua script that increments the user key, sets expiration, and checks if the limit is exceeded.

Redis
local current = redis.call('INCR', [1])
if current == 1 then redis.call('EXPIRE', [2], 60) end
if current > [3] then return 0 else return 1 end
Drag options to blanks, or click blank then click option'
Auser_key
Climit
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Using timestamp instead of user_key for increment
Mixing keys and limit values
Not comparing count to limit