What if your data could vanish exactly when you want it to, all by itself?
Why SET with expiry (EX, PX) in Redis? - Purpose & Use Cases
Imagine you have a to-do list on paper that you want to keep only for today. Every day, you erase the old list and write a new one manually.
Manually tracking when to erase or update your list is tiring and easy to forget. You might keep old tasks too long or lose track of what's current.
Using SET with expiry in Redis automatically removes data after a set time, so you never have to remember to delete it yourself.
SET task "buy milk"
// manually delete laterSET task "buy milk" EX 86400
You can store temporary data that cleans itself up, keeping your system fresh and efficient without extra work.
Storing a user's login session that expires after 30 minutes to keep their account secure without manual cleanup.
Manual data cleanup is slow and error-prone.
SET with expiry automates data removal after a set time.
This keeps your data accurate and your system efficient.
Practice
EX option do when used with the Redis SET command?Solution
Step 1: Understand the EX option in Redis SET
The EX option sets the expiration time in seconds for the key.Step 2: Compare EX with other expiry options
PX sets expiry in milliseconds, so EX is specifically for seconds.Final Answer:
Sets the key to expire after a specified number of seconds -> Option AQuick Check:
EX = seconds expiry [OK]
- Confusing EX with PX and thinking EX is milliseconds
- Assuming EX sets no expiry
- Thinking EX deletes the key immediately
session with value abc123 that expires in 10 seconds using Redis?Solution
Step 1: Recall Redis SET command syntax with expiry
The correct syntax is: SET key value EX seconds.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.Final Answer:
SET session abc123 EX 10 -> Option DQuick Check:
SET key value EX seconds = correct syntax [OK]
- Placing EX before the value
- Using PX instead of EX for seconds
- Using EXPIRE keyword inside SET command
SET temp 100 PX 5000 TTL temp
Solution
Step 1: Understand PX expiry in SET command
PX 5000 sets the key to expire in 5000 milliseconds (5 seconds).Step 2: Interpret TTL command output
TTL returns time to live in seconds. Since expiry was set in milliseconds, TTL rounds to nearest second.Step 3: Predict TTL value
TTL will return an integer close to 5 seconds, depending on execution time.Final Answer:
An integer close to 5 -> Option AQuick Check:
PX 5000 ms = ~5 seconds TTL [OK]
- Expecting TTL to return milliseconds
- Thinking TTL returns 0 immediately
- Assuming TTL returns -1 for keys with expiry
SET user123 "John" EXPIRE 20
Solution
Step 1: Check Redis SET command syntax
SET command uses EX or PX for expiry, not EXPIRE keyword.Step 2: Identify misuse of EXPIRE keyword
EXPIRE is a separate command, not an option inside SET.Final Answer:
EXPIRE is not valid inside SET command -> Option BQuick Check:
Use EX or PX inside SET, not EXPIRE [OK]
- Using EXPIRE inside SET command
- Confusing EXPIRE command with expiry option
- Misplacing quotes around key or value
api_token with value xyz789 and the required expiry?Solution
Step 1: Determine correct expiry option for milliseconds
PX sets expiry in milliseconds, EX uses seconds only.Step 2: Check value format for PX
PX requires integer milliseconds, so 1500 is valid, decimals like 1.5 are invalid.Step 3: Validate each option
SET api_token xyz789 PX 1500 uses PX 1500 correctly; others use wrong units or decimals.Final Answer:
SET api_token xyz789 PX 1500 -> Option CQuick Check:
Milliseconds expiry = PX integer value [OK]
- Using EX with decimal seconds
- Using PX with decimal instead of integer
- Confusing EX and PX units
Ready to prove it?
Pass the test and this pattern moves unknown
