Challenge - 5 Problems
TTL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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?
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
Attempts:
2 left
💡 Hint
TTL returns the remaining time to live in seconds.
✗ Incorrect
After setting EXPIRE with 10 seconds, TTL returns 10 immediately.
🧠 Conceptual
intermediate2:00remaining
What does a TTL of -2 mean?
In Redis, what does a TTL command returning -2 indicate about the key?
Attempts:
2 left
💡 Hint
Think about what happens when a key is missing.
✗ Incorrect
TTL returns -2 when the key does not exist in Redis.
📝 Syntax
advanced2: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.
Attempts:
2 left
💡 Hint
SETEX sets value and expiry in one command.
✗ Incorrect
SETEX key seconds value sets the key with expiry atomically.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
PTTL returns milliseconds remaining.
✗ Incorrect
PTTL returns time to live in milliseconds, so checking if it's between 0 and 3000 is precise.
🔧 Debug
expert2: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?
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?
Attempts:
2 left
💡 Hint
Consider concurrent clients and key recreation.
✗ Incorrect
If another client recreated the key after deletion, TTL returns -2 because the original key was deleted, but the new key exists without expiry.