0
0
Redisquery~10 mins

Key expiry for memory management in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Key expiry for memory management
Set Key with Expiry
Key Stored in Memory
Time Passes
Check Expiry Time
Key Accessible
Back to Time Passes
This flow shows how Redis stores a key with an expiry time, checks if the key has expired over time, and deletes it to free memory if expired.
Execution Sample
Redis
SET session:user123 "data" EX 5
GET session:user123
(wait 6 seconds)
GET session:user123
Set a key with 5 seconds expiry, get it before and after expiry to see memory management in action.
Execution Table
StepCommandActionResultMemory State
1SET session:user123 "data" EX 5Store key with 5s expiryOKsession:user123 stored with expiry=5s
2GET session:user123Check key existence and expiry"data"Key exists, not expired
3(wait 6 seconds)Time passes beyond expiryN/AExpiry time reached
4GET session:user123Check key existence and expiry(nil)Key deleted from memory due to expiry
💡 Key expired after 5 seconds, so GET returns nil and key is removed from memory
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
session:user123does not existexists with expiry=5sexists with expiry=5sexpiry reacheddeleted
Key Moments - 2 Insights
Why does the GET command return nil after waiting 6 seconds?
Because the key's expiry time of 5 seconds has passed (see execution_table step 3), Redis automatically deletes the key to free memory.
Does the key get deleted exactly at 5 seconds?
Redis checks expiry lazily on access or via periodic cleanup, so the key is deleted when accessed after expiry or during cleanup (step 4 shows deletion on GET).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value returned by GET?
A"data"
Bnil
CError
D"" (empty string)
💡 Hint
Check the 'Result' column at step 2 in execution_table
At which step does the key get deleted from memory?
AStep 1
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Memory State' column in execution_table for when the key is deleted
If the expiry was set to 10 seconds instead of 5, what would happen at step 4?
AGET returns nil because key is expired
BGET returns "data" because key is not expired
CGET returns error
DKey is deleted immediately at step 1
💡 Hint
Refer to variable_tracker and execution_table timing of expiry
Concept Snapshot
SET key with EX seconds sets expiry time
Key remains accessible until expiry
After expiry, key is deleted automatically
GET returns nil if key expired
Expiry helps free memory by removing unused keys
Full Transcript
This visual execution shows how Redis manages memory by expiring keys. First, a key is set with a 5-second expiry. The key is stored in memory and accessible immediately. After waiting 6 seconds, the expiry time is reached. When the key is accessed again, Redis detects the expiry and deletes the key from memory, returning nil. This automatic deletion frees memory by removing unused keys. Redis expiry is checked lazily on access or during periodic cleanup.