0
0
Redisquery~10 mins

SET with expiry (EX, PX) in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SET with expiry (EX, PX)
Start SET command
Check if expiry option given?
NoSet key with value
Yes
Parse expiry time (seconds or ms)
Set key with value and expiry
Return OK
When using SET with expiry, Redis checks if expiry time is given, then sets the key with value and expiry time, so it auto-expires.
Execution Sample
Redis
SET mykey "hello" EX 5
GET mykey
(wait 6 seconds)
GET mykey
Sets 'mykey' with value 'hello' that expires in 5 seconds, then checks value before and after expiry.
Execution Table
StepCommandActionResultKey State
1SET mykey "hello" EX 5Set key 'mykey' with value 'hello' and expiry 5 secondsOK{"mykey": {"value": "hello", "expires_in": "5s"}}
2GET mykeyRetrieve value of 'mykey'"hello"{"mykey": {"value": "hello", "expires_in": "4s"}}
3(wait 6 seconds)Wait for expiry time to passN/A{}
4GET mykeyTry to get expired key 'mykey'nil{}
💡 Key 'mykey' expires after 5 seconds, so GET after 6 seconds returns nil.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
mykeydoes not exist{"value": "hello", "expires_in": "5s"}{"value": "hello", "expires_in": "4s"}expired (removed)expired (removed)
Key Moments - 2 Insights
Why does GET return 'hello' at step 2 but nil at step 4?
At step 2, the key 'mykey' still exists and has not expired (see execution_table row 2). By step 4, after waiting 6 seconds (row 3), the key expired and was removed, so GET returns nil.
What is the difference between EX and PX options in SET?
EX sets expiry time in seconds, PX sets expiry time in milliseconds. Both cause the key to expire after the given time (concept_flow step 'Parse expiry time').
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'mykey' immediately after step 1?
A"hello"
Bnil
C{"value": "hello", "expires_in": 5s}
D{}
💡 Hint
Check the 'Key State' column in row 1 of execution_table.
At which step does the key 'mykey' expire and get removed?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Key State' column and the wait time in execution_table row 3.
If we change EX 5 to PX 5000 in the SET command, what changes in the execution?
AExpiry time changes to 5 milliseconds
BExpiry time changes to 5000 milliseconds (5 seconds)
CExpiry time changes to 5000 seconds
DNo change in expiry time
💡 Hint
EX means seconds, PX means milliseconds (see key_moments explanation).
Concept Snapshot
SET key value [EX seconds | PX milliseconds]
- Sets key with value
- EX sets expiry in seconds
- PX sets expiry in milliseconds
- Key auto-expires after expiry time
- GET returns nil after expiry
Full Transcript
This visual execution shows how Redis SET command works with expiry options EX and PX. When SET is called with EX or PX, Redis sets the key with the given value and an expiry time. The key will automatically be removed after the expiry time passes. The execution table traces setting the key 'mykey' with value 'hello' and expiry 5 seconds, then getting it before and after expiry. The variable tracker shows the key state changing from existing to expired. Key moments clarify why GET returns the value before expiry and nil after. The quiz tests understanding of expiry timing and options. This helps beginners see how expiry works step-by-step.