0
0
Redisquery~20 mins

TTL-based expiry in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TTL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output after setting a TTL?
You run these Redis commands:

SET session:123 user1
EXPIRE session:123 10
TTL session:123

What will the TTL command return immediately after setting the expiry?
Redis
SET session:123 user1
EXPIRE session:123 10
TTL session:123
A10
B-1
C0
D-2
Attempts:
2 left
💡 Hint
TTL returns the remaining time to live in seconds.
🧠 Conceptual
intermediate
2:00remaining
What does a TTL of -2 mean?
In Redis, what does a TTL command returning -2 indicate about the key?
AThe key does not exist
BThe key exists but has no expiry
CThe key has expired but still exists
DThe key has a persistent expiry
Attempts:
2 left
💡 Hint
Think about what happens when a key is missing.
📝 Syntax
advanced
2:00remaining
Which command correctly sets a key with a 5-second expiry?
Choose the Redis command that sets key 'temp' with value 'data' and expires it after 5 seconds in one atomic operation.
ASET temp data EX 5
BSETEX temp 5 data
CEXPIRE temp 5 SET data
DSET temp data PX 5000
Attempts:
2 left
💡 Hint
SETEX sets value and expiry in one command.
optimization
advanced
2:00remaining
How to efficiently check if a key will expire soon?
You want to check if a Redis key 'cache:item' will expire within the next 3 seconds. Which approach is best?
AUse TTL cache:item and check if result is between 0 and 3
BUse GET cache:item and check if value is null
CUse EXISTS cache:item and check if it returns 1
DUse PTTL cache:item and check if result is between 0 and 3000
Attempts:
2 left
💡 Hint
PTTL returns milliseconds remaining.
🔧 Debug
expert
2:00remaining
Why does a key persist despite EXPIRE command?
You run these commands:

SET mykey value
EXPIRE mykey 10
DEL mykey
TTL mykey

The TTL command returns -2. But after 5 seconds, you check and the key still exists. Why?
ADEL command failed silently and key was never deleted
BTTL returns -2 only if key exists without expiry
CThe key was recreated by another client after deletion
DRedis does not support expiry on deleted keys
Attempts:
2 left
💡 Hint
Consider concurrent clients and key recreation.