0
0
Redisquery~10 mins

PERSIST to remove expiry in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PERSIST to remove expiry
Start with a key
Check if key has expiry?
NoNothing changes
Yes
Run PERSIST command
Expiry removed
Key becomes persistent
End
This flow shows how the PERSIST command removes the expiry time from a Redis key, making it permanent.
Execution Sample
Redis
SET session123 user_data
EXPIRE session123 300
TTL session123
PERSIST session123
TTL session123
This code sets a key with expiry, checks its TTL, removes expiry with PERSIST, then checks TTL again.
Execution Table
StepCommandKey TTL BeforeActionKey TTL AfterResult
1SET session123 user_dataN/ACreate key without expiry-1Key created
2EXPIRE session123 300-1Set expiry 300 seconds300Expiry set
3TTL session123300Check TTL300TTL is 300 seconds
4PERSIST session123300Remove expiry-1Expiry removed, key persistent
5TTL session123-1Check TTL-1TTL is -1 (no expiry)
💡 After PERSIST, TTL is -1 meaning no expiry remains on the key.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
TTL(session123)N/A-1300300-1-1
Key Moments - 3 Insights
Why does TTL show -1 after SET but before EXPIRE?
Because the key was created without expiry, TTL returns -1 indicating no expiration (see execution_table step 1).
What does PERSIST do to the key's TTL?
PERSIST removes the expiry, changing TTL from a positive number to -1, meaning the key will not expire (see execution_table step 4).
Why check TTL after PERSIST?
To confirm the expiry was removed and the key is now persistent (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the TTL of 'session123' right after the EXPIRE command?
A-1
B0
C300
DN/A
💡 Hint
Check the 'Key TTL After' column at step 2 in the execution_table.
At which step does the key become persistent with no expiry?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for when TTL changes to -1 after being positive in the execution_table.
If we skip the PERSIST command, what would TTL show at step 5?
A300
B-1
C0
DN/A
💡 Hint
Without PERSIST, expiry remains so TTL stays positive (see step 3 and 5 comparison).
Concept Snapshot
PERSIST key
- Removes expiry from a Redis key
- Makes key persistent (TTL = -1)
- Use after EXPIRE to keep key forever
- TTL command shows -1 if no expiry
- Useful to cancel automatic deletion
Full Transcript
This visual execution shows how the Redis PERSIST command removes the expiry time from a key. First, a key 'session123' is created without expiry (TTL is -1). Then expiry is set to 300 seconds. Checking TTL confirms 300 seconds remain. Running PERSIST removes the expiry, changing TTL back to -1, meaning the key will not expire. Checking TTL again confirms no expiry remains. This helps understand how PERSIST cancels expiry on keys.