Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 modes available
Concept Flow - SET with expiry (EX, PX)
Start SET command
↓
Check if expiry option given?
No→Set 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.

Practice

(1/5)
1. What does the EX option do when used with the Redis SET command?
easy
A. Sets the key to expire after a specified number of seconds
B. Sets the key to expire after a specified number of milliseconds
C. Sets the key without any expiration
D. Deletes the key immediately after setting it

Solution

  1. Step 1: Understand the EX option in Redis SET

    The EX option sets the expiration time in seconds for the key.
  2. Step 2: Compare EX with other expiry options

    PX sets expiry in milliseconds, so EX is specifically for seconds.
  3. Final Answer:

    Sets the key to expire after a specified number of seconds -> Option A
  4. Quick Check:

    EX = seconds expiry [OK]
Hint: EX means seconds expiry, PX means milliseconds expiry [OK]
Common Mistakes:
  • Confusing EX with PX and thinking EX is milliseconds
  • Assuming EX sets no expiry
  • Thinking EX deletes the key immediately
2. Which of the following is the correct syntax to set a key session with value abc123 that expires in 10 seconds using Redis?
easy
A. SET session abc123 EXPIRE 10
B. SET session EX 10 abc123
C. SET session abc123 PX 10
D. SET session abc123 EX 10

Solution

  1. Step 1: Recall Redis SET command syntax with expiry

    The correct syntax is: SET key value EX seconds.
  2. Step 2: Check each option for correct order and keyword

    SET session abc123 EX 10 matches the correct order and uses EX for seconds expiry.
  3. Final Answer:

    SET session abc123 EX 10 -> Option D
  4. Quick Check:

    SET key value EX seconds = correct syntax [OK]
Hint: Remember: SET key value EX seconds [OK]
Common Mistakes:
  • Placing EX before the value
  • Using PX instead of EX for seconds
  • Using EXPIRE keyword inside SET command
3. What will be the output of the following Redis commands executed in order?
SET temp 100 PX 5000
TTL temp
medium
A. An integer close to 5
B. 5
C. 0
D. -1

Solution

  1. Step 1: Understand PX expiry in SET command

    PX 5000 sets the key to expire in 5000 milliseconds (5 seconds).
  2. Step 2: Interpret TTL command output

    TTL returns time to live in seconds. Since expiry was set in milliseconds, TTL rounds to nearest second.
  3. Step 3: Predict TTL value

    TTL will return an integer close to 5 seconds, depending on execution time.
  4. Final Answer:

    An integer close to 5 -> Option A
  5. Quick Check:

    PX 5000 ms = ~5 seconds TTL [OK]
Hint: TTL returns seconds even if PX used for expiry [OK]
Common Mistakes:
  • Expecting TTL to return milliseconds
  • Thinking TTL returns 0 immediately
  • Assuming TTL returns -1 for keys with expiry
4. Identify the error in this Redis command:
SET user123 "John" EXPIRE 20
medium
A. Value should not be in quotes
B. EXPIRE is not valid inside SET command
C. Missing quotes around key
D. EXPIRE should be PX for milliseconds

Solution

  1. Step 1: Check Redis SET command syntax

    SET command uses EX or PX for expiry, not EXPIRE keyword.
  2. Step 2: Identify misuse of EXPIRE keyword

    EXPIRE is a separate command, not an option inside SET.
  3. Final Answer:

    EXPIRE is not valid inside SET command -> Option B
  4. Quick Check:

    Use EX or PX inside SET, not EXPIRE [OK]
Hint: Use EX or PX with SET, EXPIRE is separate command [OK]
Common Mistakes:
  • Using EXPIRE inside SET command
  • Confusing EXPIRE command with expiry option
  • Misplacing quotes around key or value
5. You want to store a temporary API token in Redis that expires exactly after 1500 milliseconds. Which command correctly sets the key api_token with value xyz789 and the required expiry?
hard
A. SET api_token xyz789 EX 1.5
B. SET api_token xyz789 EX 1500
C. SET api_token xyz789 PX 1500
D. SET api_token xyz789 PX 1.5

Solution

  1. Step 1: Determine correct expiry option for milliseconds

    PX sets expiry in milliseconds, EX uses seconds only.
  2. Step 2: Check value format for PX

    PX requires integer milliseconds, so 1500 is valid, decimals like 1.5 are invalid.
  3. Step 3: Validate each option

    SET api_token xyz789 PX 1500 uses PX 1500 correctly; others use wrong units or decimals.
  4. Final Answer:

    SET api_token xyz789 PX 1500 -> Option C
  5. Quick Check:

    Milliseconds expiry = PX integer value [OK]
Hint: Use PX with integer milliseconds for precise expiry [OK]
Common Mistakes:
  • Using EX with decimal seconds
  • Using PX with decimal instead of integer
  • Confusing EX and PX units
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard