📖 Scenario: You are managing a Redis database that stores temporary session data for users. Sometimes, you want to keep a session permanently by removing its expiry time.
🎯 Goal: Learn how to create a Redis key with an expiry time and then remove that expiry using the PERSIST command.
📋 What You'll Learn
Create a Redis key named session:user123 with the value active and set it to expire in 60 seconds.
Create a variable to hold the key name key_name with the value session:user123.
Use the PERSIST command on the key stored in key_name to remove its expiry.
Verify that the key no longer has an expiry time.
💡 Why This Matters
🌍 Real World
Managing user sessions or temporary data in Redis often requires setting expiry times and sometimes removing them to keep data permanently.
💼 Career
Understanding how to control key expiry in Redis is important for backend developers and system administrators working with caching and session management.
Progress0 / 4 steps
1
Create a Redis key with expiry
Create a Redis key named session:user123 with the value active and set it to expire in 60 seconds using the SET command with the EX option.
Redis
Hint
Use SET key value EX seconds to set a key with expiry.
2
Create a variable for the key name
Create a variable called key_name and set it to the string session:user123.
Redis
Hint
Assign the string 'session:user123' to key_name.
3
Remove expiry using PERSIST
Use the PERSIST command on the key stored in the variable key_name to remove its expiry time.
Redis
Hint
Use PERSIST key to remove expiry from a key.
4
Verify the key has no expiry
Use the TTL command on the key stored in key_name to check that it no longer has an expiry time.
Redis
Hint
Use TTL key to check the remaining time to live of a key. It returns -1 if the key has no expiry.
Practice
(1/5)
1. What does the Redis command PERSIST key do?
easy
A. Renames the key to 'persisted_key'
B. Sets a new expiration time on the key
C. Deletes the key immediately
D. Removes the expiration time from the key, making it permanent
Solution
Step 1: Understand the purpose of PERSIST
The PERSIST command removes any expiration time set on a Redis key, so the key no longer expires.
Step 2: Compare with other commands
Unlike EXPIRE which sets a timeout, PERSIST cancels it. It does not delete or rename the key.
Final Answer:
Removes the expiration time from the key, making it permanent -> Option D
Quick Check:
PERSIST removes expiry = Removes the expiration time from the key, making it permanent [OK]
After setting key 'mykey' and applying EXPIRE 10, TTL returns the remaining seconds, which is 10.
Step 2: Analyze the second TTL after PERSIST
PERSIST removes expiry, so TTL returns -1 indicating no expiration.
Final Answer:
10 and -1 -> Option A
Quick Check:
TTL before persist = 10, after persist = -1 [OK]
Hint: TTL shows seconds left; PERSIST makes TTL -1 [OK]
Common Mistakes:
Expecting TTL to stay 10 after PERSIST
Confusing TTL -1 meaning
Thinking PERSIST sets TTL to 0
4. You run the command PERSIST mykey but it returns 0. What is the most likely reason?
medium
A. The key does not exist
B. The key has no expiry set
C. The key is already persistent
D. The command syntax is incorrect
Solution
Step 1: Understand PERSIST return values
PERSIST returns 1 if expiry was removed, 0 if key had no expiry or key does not exist.
Step 2: Differentiate between no expiry and missing key
If key does not exist, PERSIST returns 0, but usually you check key existence first. If key exists but no expiry, PERSIST returns 0 because nothing to remove.
Final Answer:
The key has no expiry set -> Option B
Quick Check:
PERSIST returns 0 if no expiry to remove [OK]
Hint: PERSIST returns 0 if no expiry on key [OK]
Common Mistakes:
Assuming 0 means syntax error
Confusing no expiry with key missing
Thinking 0 means key deleted
5. You have a Redis key cache:item with an expiry of 30 seconds. You want to keep it permanently only if its current TTL is more than 10 seconds. Which sequence of commands achieves this safely?
hard
A. Run EXPIRE cache:item 0 to remove expiry
B. Run PERSIST cache:item directly without checking TTL
C. Use TTL cache:item, then if result > 10, run PERSIST cache:item
D. Rename the key to a new key without expiry
Solution
Step 1: Check current TTL of the key
Use TTL cache:item to get remaining expiry time in seconds.
Step 2: Conditionally remove expiry
If TTL is more than 10, run PERSIST cache:item to remove expiry and keep key permanently.
Step 3: Avoid wrong commands
Running PERSIST without checking TTL removes expiry regardless. EXPIRE with 0 does not remove expiry but sets immediate expiry. Renaming does not affect expiry.
Final Answer:
Use TTL, then if > 10, run PERSIST -> Option C
Quick Check:
Check TTL before PERSIST to keep key only if TTL > 10 [OK]
Hint: Check TTL first, then PERSIST if TTL > 10 [OK]
Common Mistakes:
Running PERSIST blindly without TTL check
Using EXPIRE 0 to remove expiry
Thinking renaming removes expiry
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.