0
0
Redisquery~20 mins

Key expiry for memory management in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Expiry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this Redis command sequence?
Consider the following Redis commands executed in order:

SET session:12345 user1
EXPIRE session:12345 10
TTL session:12345

What will the TTL command return immediately after setting the expiry?
Redis
SET session:12345 user1
EXPIRE session:12345 10
TTL session:12345
A0
B-1
C-2
D10
Attempts:
2 left
💡 Hint
TTL returns the remaining time to live in seconds for a key with expiry.
🧠 Conceptual
intermediate
2:00remaining
Which Redis key expiry behavior is correct?
In Redis, what happens when a key with an expiry time reaches zero?
AThe key remains until a client tries to access it, then it is deleted.
BThe key is deleted only during periodic eviction cycles.
CThe key is immediately deleted from memory.
DThe key's expiry time resets automatically to prevent deletion.
Attempts:
2 left
💡 Hint
Redis uses lazy expiration to save CPU resources.
📝 Syntax
advanced
2:00remaining
Which command correctly sets a key to expire in 5 minutes?
Choose the correct Redis command to set the key cache:item to expire in 5 minutes.
ASETEX cache:item 5 value
BEXPIRE cache:item 300
CSETEX cache:item 300 value
DEXPIRE cache:item 5m
Attempts:
2 left
💡 Hint
SETEX sets a key with a value and expiry in seconds.
optimization
advanced
2:00remaining
How to efficiently manage memory with many expiring keys?
You have millions of keys with expiry in Redis. Which approach optimizes memory and CPU usage best?
ASet expiry and use Redis active expiration cycle to remove expired keys regularly.
BSet expiry on all keys and rely on Redis lazy expiration only.
CManually delete keys with a background script instead of using expiry.
DNever set expiry and let keys accumulate to avoid CPU overhead.
Attempts:
2 left
💡 Hint
Redis combines lazy and active expiration for efficiency.
🔧 Debug
expert
2:00remaining
Why does this Redis key never expire?
A developer runs these commands:

SET temp:data 100
EXPIRE temp:data 60
TTL temp:data

But the TTL command returns -1. What is the most likely cause?
ARedis does not support expiry on string keys.
BThe key was overwritten by another SET command without expiry after EXPIRE was set.
CThe EXPIRE command syntax is incorrect and was ignored.
DThe TTL command was run on a different key by mistake.
Attempts:
2 left
💡 Hint
Setting a key again removes its expiry unless set with SETEX or EXPIRE after.