0
0
Redisquery~10 mins

Temporary data with TTL in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Temporary data with TTL
Set key with value
Assign TTL (time to live)
Wait for TTL countdown
TTL expires?
NoKey still exists
Yes
Key auto-deleted
Key no longer accessible
This flow shows how a key is set with a value and a time limit (TTL). When the TTL ends, the key is automatically removed.
Execution Sample
Redis
SET session123 "user_data"
EXPIRE session123 5
GET session123
(wait 6 seconds)
GET session123
Set a key with a value and TTL 5 seconds, then get it before and after TTL expires.
Execution Table
StepCommandActionKey Exists?Output
1SET session123 "user_data"Store key 'session123' with value 'user_data'YesOK
2EXPIRE session123 5Set TTL 5 seconds on 'session123'Yes1
3GET session123 (immediately)Retrieve value of 'session123'Yes"user_data"
4(Wait 6 seconds)TTL countdown ends, key expiresNonil
5GET session123 (after wait)Try to get expired keyNonil
💡 After 5 seconds, TTL expires and Redis deletes the key automatically.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4Final
session123does not exist"user_data""user_data" (TTL 5s)deleteddoes not exist
Key Moments - 2 Insights
Why does GET return nil after waiting even though we set the key before?
Because the TTL expired after 5 seconds (see execution_table step 4), Redis automatically deleted the key.
What does the EXPIRE command return and why?
It returns 1 to confirm TTL was set successfully (see execution_table step 2). If key did not exist, it would return 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of GET session123 immediately after setting TTL?
A"user_data"
Bnil
CError
D1
💡 Hint
Check execution_table row 3 under Output column.
At which step does the key 'session123' get deleted automatically?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at execution_table step 4 Action and Key Exists? columns.
If we set TTL to 10 seconds instead of 5, what changes in the execution_table?
AEXPIRE returns 0
BGET returns nil immediately
CKey expires at step 4 after 10 seconds instead of 5
DKey never expires
💡 Hint
TTL controls when the key expires, see execution_table step 4.
Concept Snapshot
SET key value - stores data
EXPIRE key seconds - sets TTL
GET key - retrieves data
After TTL ends, key auto-deletes
GET returns nil if key expired
Full Transcript
This visual trace shows how Redis handles temporary data using TTL (time to live). First, a key 'session123' is set with a value 'user_data'. Then, a TTL of 5 seconds is assigned. Immediately after, getting the key returns the stored value. After waiting 6 seconds, the TTL expires and Redis deletes the key automatically. Trying to get the key now returns nil because it no longer exists. This demonstrates how TTL helps manage temporary data that expires automatically.