EXPIRE and TTL for time-to-live in Redis - Time & Space Complexity
When using Redis commands like EXPIRE and TTL, it's important to know how their speed changes as your data grows.
We want to understand how long these commands take when you have many keys or set many expirations.
Analyze the time complexity of the following Redis commands.
SET session:user123 "data"
EXPIRE session:user123 3600
TTL session:user123
This code sets a key, assigns it a time-to-live of one hour, and then checks how much time is left before it expires.
Look for any repeated steps or loops inside these commands.
- Primary operation: Checking or setting expiration on a single key.
- How many times: Each command works on one key at a time, no loops over multiple keys.
These commands handle one key at a time, so their work does not increase with the total number of keys.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 keys | Same small number of steps per command |
| 100 keys | Still same small number of steps per command |
| 1000 keys | No increase in steps per command |
Pattern observation: The time to run EXPIRE or TTL stays about the same no matter how many keys exist.
Time Complexity: O(1)
This means setting or checking expiration takes the same short time regardless of how many keys you have.
[X] Wrong: "Checking TTL gets slower as I add more keys because Redis must look through all keys."
[OK] Correct: Redis stores expiration info efficiently, so TTL only checks one key directly without scanning others.
Knowing that EXPIRE and TTL run in constant time helps you explain how Redis handles time-based data efficiently, a useful skill in many real projects.
"What if we wanted to set expiration on many keys at once? How would that affect the time complexity?"
Practice
EXPIRE key 60 do?Solution
Step 1: Understand the EXPIRE command
The EXPIRE command sets a countdown timer on a key in seconds.Step 2: Apply the command to the example
Here,EXPIRE key 60sets the key to expire after 60 seconds.Final Answer:
Sets the key to expire and be deleted after 60 seconds. -> Option CQuick Check:
EXPIRE sets countdown = A [OK]
- Thinking EXPIRE deletes immediately
- Confusing EXPIRE with TTL command
- Assuming EXPIRE renames keys
session in Redis?Solution
Step 1: Identify the command to check TTL
The Redis command to check remaining time isTTL key.Step 2: Match the syntax with the key name
For keysession, the correct command isTTL session.Final Answer:
TTL session -> Option AQuick Check:
TTL command syntax = D [OK]
- Using EXPIRE to check TTL
- Assuming GETTTL is a Redis command
- Using TIMELEFT which does not exist
SET temp "data" EXPIRE temp 10 TTL temp
What will the
TTL temp command return immediately after setting the expire?Solution
Step 1: Set key and expire time
The keytempis set with value "data" and an expire time of 10 seconds.Step 2: Check TTL immediately after EXPIRE
Immediately after setting expire, TTL returns the remaining seconds, which is 10.Final Answer:
10 -> Option DQuick Check:
TTL after EXPIRE = 10 [OK]
- Expecting TTL to return -1 (no expire)
- Thinking TTL returns 0 immediately
- Assuming TTL causes an error if key exists
SET user "Alice" EXPIRE user -5 TTL user
What is the result of
TTL user and why?Solution
Step 1: Understand EXPIRE with negative time
Setting EXPIRE with a negative value deletes the key immediately.Step 2: Check TTL after immediate deletion
TTL returns -2 when the key does not exist.Final Answer:
-2 because key is deleted immediately -> Option AQuick Check:
Negative EXPIRE deletes key, TTL = -2 [OK]
- Thinking negative expire removes expiration but keeps key
- Expecting TTL to return -1 for no expire
- Assuming negative expire causes error
cache with a value and ensure it expires in 30 seconds only if it does not already have an expiration. Which Redis command sequence achieves this safely?Solution
Step 1: Understand the requirement
We want to set the key and only add expiration if none exists (TTL = -1).Step 2: Analyze options
SET cache "value" TTL cache EXPIRE cache 30 if TTL is -1 checks TTL first, then sets EXPIRE only if TTL is -1, ensuring no overwrite of existing expiration.Final Answer:
SET cache "value" TTL cache EXPIRE cache 30 if TTL is -1 -> Option BQuick Check:
Check TTL before EXPIRE to avoid overwriting [OK]
- Using NX option which only affects key existence
- Setting EXPIRE twice unnecessarily
- Not checking TTL before setting EXPIRE
Ready to prove it?
Pass the test and this pattern moves unknown
