0
0
Redisquery~20 mins

EXPIRE and TTL for time-to-live in Redis - Practice Problems & Coding Challenges

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

SET session:user123 "active"
EXPIRE session:user123 120

What will TTL session:user123 return immediately after?
Redis
SET session:user123 "active"
EXPIRE session:user123 120
TTL session:user123
A120
B-1
C-2
D0
Attempts:
2 left
💡 Hint
TTL returns the remaining time in seconds before the key expires.
query_result
intermediate
2:00remaining
What does TTL return for a key without expiration?
If you run:

SET config:theme "dark"

and then:

TTL config:theme

What is the expected output?
Redis
SET config:theme "dark"
TTL config:theme
A-1
B0
C-2
Dnull
Attempts:
2 left
💡 Hint
TTL returns -1 if the key exists but has no expiration.
📝 Syntax
advanced
2:00remaining
Which EXPIRE command syntax is correct?
Which of the following Redis commands correctly sets a key to expire in 60 seconds?
AEXPIRE 60 mykey
BEXPIRE mykey TO 60
CEXPIRE mykey 60
DEXPIRE mykey EX 60
Attempts:
2 left
💡 Hint
The EXPIRE command syntax is: EXPIRE key seconds
query_result
advanced
2:00remaining
What happens to TTL after key deletion?
You run:

SET temp:data "123"
EXPIRE temp:data 30
DEL temp:data

What will TTL temp:data return after deletion?
Redis
SET temp:data "123"
EXPIRE temp:data 30
DEL temp:data
TTL temp:data
A-1
B-2
C0
D30
Attempts:
2 left
💡 Hint
TTL returns -2 if the key does not exist.
🧠 Conceptual
expert
2:00remaining
Why use EXPIRE instead of manual deletion?
Which is the main advantage of using EXPIRE to set a time-to-live on keys instead of manually deleting keys later?
AEXPIRE makes keys immutable until expiration.
BEXPIRE duplicates keys to backup storage before deletion.
CEXPIRE encrypts keys for security until they expire.
DEXPIRE automatically removes keys after a set time, reducing memory usage without manual intervention.
Attempts:
2 left
💡 Hint
Think about automation and resource management.