📖 Scenario: You are managing a Redis database that stores temporary session data for an online store. To keep the database clean and efficient, you want to set expiration times on keys so that old sessions are automatically removed.
🎯 Goal: Build a Redis setup where you create keys for user sessions, configure expiration times, and verify that keys expire as expected to manage memory effectively.
📋 What You'll Learn
Create keys representing user sessions with specific values
Set expiration time on these keys using Redis commands
Check the remaining time to live (TTL) for keys
Demonstrate automatic key expiry after the set time
💡 Why This Matters
🌍 Real World
Setting key expiry helps automatically remove old or unused data, keeping the Redis database efficient and saving memory.
💼 Career
Many jobs require managing cache or session data with automatic expiry to ensure performance and resource management.
Progress0 / 4 steps
1
Create session keys in Redis
Use the Redis SET command to create three keys named session:user1, session:user2, and session:user3 with values data1, data2, and data3 respectively.
Redis
Hint
Use the SET command followed by the key name and value.
2
Set expiration time for session keys
Use the Redis EXPIRE command to set an expiration time of 60 seconds on the keys session:user1 and session:user2.
Redis
Hint
The EXPIRE command takes the key name and the time in seconds.
3
Check the time to live (TTL) for session keys
Use the Redis TTL command to check the remaining expiration time for the keys session:user1, session:user2, and session:user3.
Redis
Hint
The TTL command shows how many seconds remain before the key expires. If the key has no expiry, it returns -1.
4
Verify automatic key expiry
Wait for 60 seconds and then use the Redis EXISTS command to check if the keys session:user1, session:user2, and session:user3 still exist.
Redis
Hint
The EXISTS command returns 1 if the key exists, 0 if it does not.
Practice
(1/5)
1. What does the EXPIRE command do in Redis?
easy
A. Sets a time limit after which a key will be automatically deleted
B. Immediately deletes a key from the database
C. Shows the remaining time before a key expires
D. Removes the expiration time from a key
Solution
Step 1: Understand the purpose of EXPIRE
The EXPIRE command sets a countdown timer on a key, after which Redis deletes it automatically.
Step 2: Differentiate from other commands
Unlike DEL which deletes immediately, EXPIRE schedules deletion after a set time.
Final Answer:
Sets a time limit after which a key will be automatically deleted -> Option A
Quick Check:
EXPIRE = set auto-delete timer [OK]
Hint: EXPIRE sets timer; key deletes after time [OK]
Common Mistakes:
Confusing EXPIRE with DEL command
Thinking EXPIRE shows time left instead of setting it
Assuming EXPIRE removes expiration instead of adding it
2. Which of the following is the correct syntax to set a key session to expire in 120 seconds?
easy
A. SETEX session 120
B. EXPIRE 120 session
C. TTL session 120
D. EXPIRE session 120
Solution
Step 1: Recall EXPIRE command syntax
The correct syntax is EXPIRE key seconds, so EXPIRE session 120 sets expiry to 120 seconds.
Step 2: Check other options
EXPIRE 120 session reverses arguments, A is incomplete syntax for SETEX, D is invalid usage of TTL.
Final Answer:
EXPIRE session 120 -> Option D
Quick Check:
EXPIRE key seconds = EXPIRE session 120 [OK]
Hint: EXPIRE syntax: EXPIRE key seconds [OK]
Common Mistakes:
Swapping key and seconds in EXPIRE
Using TTL to set expiry instead of check
Confusing SETEX syntax
3. Given the commands:
SET user:1 "Alice"
EXPIRE user:1 10
TTL user:1
What will the TTL user:1 command return immediately after setting expiry?
medium
A. -1
B. Error
C. 10
D. 0
Solution
Step 1: Understand TTL after EXPIRE
After setting EXPIRE to 10 seconds, TTL returns the remaining time in seconds, which is 10 immediately.
Step 2: Interpret other options
-1 means no expiry, 0 means expired, Error is invalid here.
Final Answer:
10 -> Option C
Quick Check:
TTL after EXPIRE = expiry seconds [OK]
Hint: TTL shows seconds left after EXPIRE set [OK]
Common Mistakes:
Expecting TTL to return -1 immediately
Thinking TTL returns 0 right after EXPIRE
Confusing TTL with DEL command
4. You run the command EXPIRE mykey 30 but TTL mykey returns -2. What is the likely problem?
medium
A. The key mykey has no expiry set
B. The key mykey does not exist
C. The EXPIRE command syntax is incorrect
D. The Redis server is down
Solution
Step 1: Understand TTL return values
TTL returns -2 if the key does not exist in Redis.
Step 2: Analyze the scenario
Since TTL returns -2, it means mykey is missing, so EXPIRE had no effect.
Final Answer:
The key mykey does not exist -> Option B
Quick Check:
TTL = -2 means key missing [OK]
Hint: TTL -2 means key missing, not syntax error [OK]
Common Mistakes:
Assuming -2 means no expiry set
Thinking EXPIRE syntax is wrong
Ignoring that key might be deleted
5. You want to keep a Redis key cache:data but remove its expiry to prevent automatic deletion. Which command should you use?
hard
A. PERSIST cache:data
B. EXPIRE cache:data 0
C. DEL cache:data
D. TTL cache:data
Solution
Step 1: Identify command to remove expiry
The PERSIST command removes the expiry time from a key, making it permanent.
Step 2: Check other options
EXPIRE with 0 deletes immediately, DEL deletes key, TTL only checks expiry time.
Final Answer:
PERSIST cache:data -> Option A
Quick Check:
PERSIST removes expiry timer [OK]
Hint: Use PERSIST to cancel expiry, keep key [OK]
Common Mistakes:
Using EXPIRE 0 which deletes key immediately
Confusing DEL with removing expiry
Using TTL expecting it to remove expiry
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.