📖 Scenario: You are managing a Redis cache for a web application that stores temporary session data. To keep the cache clean and efficient, you want to set expiration times on session keys so that old sessions are automatically removed.
🎯 Goal: Build a Redis setup where you create session keys with values, configure expiration times (TTL), and verify that keys expire as expected.
📋 What You'll Learn
Create a Redis key named session:user123 with the value active
Set a TTL (time to live) of 120 seconds on the session:user123 key
Retrieve the TTL of the session:user123 key to confirm it is set
Use the EXPIRE command to update the TTL to 60 seconds
💡 Why This Matters
🌍 Real World
Setting TTLs on cache or session keys helps automatically remove stale data, keeping Redis memory usage efficient.
💼 Career
Many backend developers and DevOps engineers use Redis TTL features to manage cache lifecycles and session expiration in real applications.
Progress0 / 4 steps
1
Create a session key with a value
Use the SET command to create a Redis key called session:user123 with the value active.
Redis
Hint
The SET command stores a key with a value in Redis.
2
Set a TTL of 120 seconds on the session key
Use the EXPIRE command to set a TTL of 120 seconds on the key session:user123.
Redis
Hint
The EXPIRE command sets the time to live for a key in seconds.
3
Check the TTL of the session key
Use the TTL command to retrieve the remaining time to live of the key session:user123.
Redis
Hint
The TTL command returns the remaining seconds before the key expires.
4
Update the TTL to 60 seconds
Use the EXPIRE command again to update the TTL of session:user123 to 60 seconds.
Redis
Hint
You can reset the TTL of a key anytime using the EXPIRE command.
Practice
(1/5)
1. What does the Redis command EXPIRE key 60 do?
easy
A. Renames the key to '60'
B. Deletes the key immediately
C. Sets the key to expire and be deleted after 60 seconds
D. Returns the current TTL of the key
Solution
Step 1: Understand the EXPIRE command
The EXPIRE command sets a time-to-live (TTL) in seconds for a key in Redis.
Step 2: Apply the command to the example
Using EXPIRE key 60 means the key will be deleted automatically after 60 seconds.
Final Answer:
Sets the key to expire and be deleted after 60 seconds -> Option C
Quick Check:
EXPIRE sets TTL = expire after time [OK]
Hint: EXPIRE sets countdown in seconds for key deletion [OK]
Common Mistakes:
Thinking EXPIRE deletes key immediately
Confusing EXPIRE with TTL command
Assuming EXPIRE renames the key
2. Which of the following is the correct syntax to check the remaining TTL of a key named session?
easy
A. TTL session
B. EXPIRE session
C. GETTTL session
D. PERSIST session
Solution
Step 1: Identify the command to check TTL
The Redis command to check remaining time-to-live is TTL followed by the key name.
Step 2: Match the syntax
TTL session correctly checks the remaining TTL of the key 'session'. Other options are either invalid or perform different actions.
Final Answer:
TTL session -> Option A
Quick Check:
TTL command returns remaining expiry time [OK]
Hint: Use TTL key to see remaining expiry time in seconds [OK]
Common Mistakes:
Using EXPIRE to check TTL instead of setting it
Using PERSIST which removes expiry, not checks it
Assuming GETTTL is a valid Redis command
3. Consider the following Redis commands executed in order: SET temp 100 EXPIRE temp 10 TTL temp What is the expected output of the last command?
medium
A. -1
B. 0
C. Error
D. 10
Solution
Step 1: Set key and expiry
The key 'temp' is set with value '100' and then given a TTL of 10 seconds.
Step 2: Check TTL immediately
Immediately after setting expiry, TTL temp returns the remaining time, which should be close to 10 seconds.
Final Answer:
10 -> Option D
Quick Check:
TTL after EXPIRE shows seconds left [OK]
Hint: TTL right after EXPIRE shows the set seconds [OK]
Common Mistakes:
Expecting TTL to return -1 (means no expiry)
Thinking TTL returns 0 immediately after EXPIRE
Assuming TTL command causes error
4. You run the following commands: SET user123 "data" EXPIRE user123 30 PERSIST user123 TTL user123 But the TTL command returns -1. What is the issue?
medium
A. Key user123 does not exist
B. PERSIST removed the expiry, so TTL returns -1
C. TTL command syntax is wrong
D. EXPIRE was not set correctly
Solution
Step 1: Understand PERSIST effect
PERSIST removes the expiry from a key, making it permanent.
Step 2: Check TTL after PERSIST
After PERSIST, TTL returns -1 indicating no expiry is set on the key.
Final Answer:
PERSIST removed the expiry, so TTL returns -1 -> Option B
Quick Check:
PERSIST removes TTL, TTL returns -1 [OK]
Hint: PERSIST removes expiry; TTL then returns -1 [OK]
Common Mistakes:
Thinking TTL returns error if expiry removed
Assuming EXPIRE failed without checking
Confusing TTL -1 with key missing
5. You want to set a key cache_item with a value and ensure it expires exactly after 5 minutes. Which sequence of commands correctly achieves this and verifies the expiry?
hard
A. SET cache_item "value"\nEXPIRE cache_item 300\nTTL cache_item
B. SET cache_item "value"\nTTL cache_item 300\nEXPIRE cache_item
C. EXPIRE cache_item 300\nSET cache_item "value"\nTTL cache_item
D. SET cache_item "value"\nPERSIST cache_item\nTTL cache_item
Solution
Step 1: Set the key with value
Use SET to create the key 'cache_item' with the desired value.
Step 2: Set expiry to 300 seconds (5 minutes)
Use EXPIRE cache_item 300 to set TTL to 5 minutes.
Step 3: Verify TTL
Use TTL cache_item to check remaining time, which should be close to 300 seconds.
Final Answer:
SET cache_item "value"\nEXPIRE cache_item 300\nTTL cache_item -> Option A
Quick Check:
SET then EXPIRE then TTL to verify expiry [OK]
Hint: SET key, then EXPIRE seconds, then TTL to check [OK]
Common Mistakes:
Using TTL to set expiry (wrong command)
Setting EXPIRE before SET (key must exist first)
Using PERSIST which removes expiry
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.