PERSIST to remove expiry in Redis - Time & Space Complexity
We want to understand how long it takes to remove the expiration from a key in Redis using the PERSIST command.
How does the time needed change as the database grows?
Analyze the time complexity of the following Redis command.
PERSIST mykey
This command removes the expiration time from the key named "mykey", making it persistent.
Look for any repeated steps or loops inside the command.
- Primary operation: Checking if the key has an expiration and removing it.
- How many times: This happens once per command call, no loops or repeated scans.
The time to remove expiration does not depend on the number of keys or database size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation takes about the same time no matter how many keys exist.
Time Complexity: O(1)
This means removing the expiration from a key takes a constant amount of time regardless of database size.
[X] Wrong: "Removing expiration takes longer if the database has many keys."
[OK] Correct: The PERSIST command works directly on the specified key without scanning others, so its time does not grow with database size.
Understanding that some Redis commands run in constant time helps you reason about performance and choose the right commands for your needs.
"What if we used EXPIRE to set a new expiration instead of PERSIST? How would the time complexity compare?"
Practice
PERSIST key do?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 DQuick Check:
PERSIST removes expiry = Removes the expiration time from the key, making it permanent [OK]
- Confusing PERSIST with EXPIRE
- Thinking PERSIST deletes the key
- Assuming PERSIST renames the key
session123?Solution
Step 1: Recall the correct command for removing expiry
The Redis command to remove expiry isPERSIST key. So for key 'session123', it isPERSIST session123.Step 2: Check other options
REMOVEEXPIRY is not a Redis command. EXPIRE with 0 does not remove expiry but sets it to expire immediately. TTL only checks expiry time.Final Answer:
PERSIST session123 -> Option AQuick Check:
Correct syntax for removing expiry = PERSIST session123 [OK]
- Using EXPIRE with 0 to remove expiry
- Trying non-existent commands like REMOVEEXPIRY
- Confusing TTL with expiry removal
SET mykey "hello" EXPIRE mykey 10 TTL mykey PERSIST mykey TTL mykey
What will be the output of the two
TTL commands?Solution
Step 1: Analyze the first TTL after EXPIRE
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 AQuick Check:
TTL before persist = 10, after persist = -1 [OK]
- Expecting TTL to stay 10 after PERSIST
- Confusing TTL -1 meaning
- Thinking PERSIST sets TTL to 0
PERSIST mykey but it returns 0. What is the most likely reason?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 BQuick Check:
PERSIST returns 0 if no expiry to remove [OK]
- Assuming 0 means syntax error
- Confusing no expiry with key missing
- Thinking 0 means key deleted
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?Solution
Step 1: Check current TTL of the key
UseTTL cache:itemto get remaining expiry time in seconds.Step 2: Conditionally remove expiry
If TTL is more than 10, runPERSIST cache:itemto 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 CQuick Check:
Check TTL before PERSIST to keep key only if TTL > 10 [OK]
- Running PERSIST blindly without TTL check
- Using EXPIRE 0 to remove expiry
- Thinking renaming removes expiry
Ready to prove it?
Pass the test and this pattern moves unknown
