Temporary data with TTL in Redis - Time & Space Complexity
We want to understand how the time to set temporary data with expiration changes as we add more data.
How does Redis handle the time cost when we add keys that expire automatically?
Analyze the time complexity of the following Redis commands.
SET user:1000 "Alice" EX 60
SET user:1001 "Bob" EX 60
SET user:1002 "Carol" EX 60
GET user:1000
DEL user:1001
This code sets keys with a time-to-live (TTL) of 60 seconds, then reads and deletes keys.
Look for repeated actions that affect time.
- Primary operation: Setting keys with expiration (SET with EX option)
- How many times: Once per key added, each key has its own expiration timer
As you add more keys with TTL, each SET command takes about the same time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 SET commands, each quick |
| 100 | 100 SET commands, each still quick |
| 1000 | 1000 SET commands, each similar speed |
Pattern observation: Time per SET stays about the same no matter how many keys exist.
Time Complexity: O(1)
This means setting a key with TTL takes about the same time no matter how many keys are stored.
[X] Wrong: "Setting a key with expiration gets slower as more keys with TTL exist."
[OK] Correct: Redis manages expirations efficiently, so each SET with TTL runs in constant time regardless of total keys.
Knowing that setting temporary data with TTL is fast helps you design systems that rely on quick expiration without slowing down as data grows.
"What if we used a Lua script to set multiple keys with TTL in one call? How would the time complexity change?"
Practice
EX option do when used with the Redis SET command?Solution
Step 1: Understand the SET command with EX option
TheSETcommand stores a key-value pair. TheEXoption specifies the expiration time in seconds.Step 2: Effect of EX on the key
WhenEXis used, the key will automatically be deleted after the given time passes.Final Answer:
Sets a time-to-live (TTL) in seconds for the key, after which it expires -> Option BQuick Check:
EX = TTL in seconds [OK]
- Thinking EX makes the key permanent
- Confusing EX with immediate deletion
- Assuming EX renames the key
session with value abc123 that expires in 120 seconds in Redis?Solution
Step 1: Recall Redis SET command syntax with EX
The correct syntax isSET key value EX seconds.Step 2: Match the syntax with options
SET session abc123 EX 120 matches the correct order: key, value, EX, seconds.Final Answer:
SET session abc123 EX 120 -> Option DQuick Check:
SET key value EX seconds [OK]
- Placing EX before the key or value
- Using PX instead of EX for seconds
- Mixing order of arguments
TTL user:123 immediately after execution?
SET user:123 "John" EX 60 TTL user:123
Solution
Step 1: Understand the SET with EX 60
The keyuser:123is set with a TTL of 60 seconds.Step 2: Check TTL command output
TheTTLcommand returns the remaining time to live in seconds. Immediately after setting, it should be 60.Final Answer:
60 -> Option AQuick Check:
TTL after SET with EX 60 = 60 [OK]
- Expecting TTL to return -1 (no expiration)
- Confusing -2 (key does not exist) with TTL
- Thinking TTL returns 0 immediately
SET temp_key "data" EX 30 EXPIRE temp_key 60 TTL temp_keyWhat will
TTL temp_key return?Solution
Step 1: SET with EX 30 sets TTL to 30 seconds
The keytemp_keyinitially has 30 seconds TTL.Step 2: EXPIRE command updates TTL to 60 seconds
TheEXPIRE temp_key 60resets the TTL to 60 seconds.Step 3: TTL command returns updated TTL
So,TTL temp_keyreturns 60 seconds remaining.Final Answer:
60 -> Option AQuick Check:
EXPIRE updates TTL, TTL shows new value [OK]
- Assuming TTL stays at original 30 seconds
- Confusing TTL -1 (no expiration) with updated TTL
- Thinking EXPIRE does not change TTL
Solution
Step 1: Use SET with EX 300 for 5 minutes expiration
The commandSET login_token abc EX 300sets the token with 300 seconds TTL.Step 2: Wait 180 seconds (3 minutes) and check TTL
UsingSLEEP 180simulates waiting. ThenTTL login_tokenshows remaining time, confirming token still exists.Step 3: EXISTS only returns 1 or 0, TTL shows time left
Checking TTL is better to confirm token is still valid and how long it lasts.Final Answer:
SET login_token abc EX 300 SLEEP 180 TTL login_token -> Option CQuick Check:
SET with EX + wait + TTL check = correct flow [OK]
- Using PX instead of EX for seconds
- Using WAIT instead of SLEEP (WAIT waits for replicas, not sleep)
- Using EXISTS which only returns existence, not TTL
Ready to prove it?
Pass the test and this pattern moves unknown
