SET with expiry (EX, PX) in Redis - Time & Space Complexity
We want to understand how the time it takes to run a Redis SET command with an expiry changes as we add more data.
Specifically, we ask: does setting a key with expiration take longer when the database grows?
Analyze the time complexity of the following Redis commands.
SET mykey "Hello" EX 10
SET anotherkey "World" PX 5000
These commands set keys with expiration times in seconds (EX) and milliseconds (PX).
Look for any repeated work inside the command execution.
- Primary operation: Storing the key and value, and scheduling the expiration.
- How many times: Each command runs once per key set; no loops or repeated traversals inside the command.
The time to set a key with expiry stays about the same no matter how many keys are in the database.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant time to set and schedule expiry |
| 100 | Still constant time per command |
| 1000 | Still constant time per command |
Pattern observation: The work does not grow with the number of keys; it stays steady.
Time Complexity: O(1)
This means setting a key with an expiry takes the same amount of time regardless of how many keys are stored.
[X] Wrong: "Setting a key with expiry takes longer as the database grows because Redis must check all keys for expiration."
[OK] Correct: Redis handles expirations efficiently using internal data structures, so setting a key with expiry is a quick, direct operation.
Knowing that setting keys with expiry is fast and does not slow down as data grows helps you design efficient caching and session systems.
"What if we used a command that scans all keys to set expiry instead? How would the time complexity change?"
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
