Key expiry for memory management in Redis - Time & Space Complexity
When Redis removes keys that have expired, it helps manage memory efficiently. We want to understand how the time it takes to check and delete expired keys changes as the number of keys grows.
How does Redis handle key expiry without slowing down as more keys exist?
Analyze the time complexity of this Redis key expiry process.
// Redis periodically runs this:
for each key in a small random sample of keys with expiry:
if key is expired:
delete key
// Repeat this process every few milliseconds
This code shows Redis checking a small random set of keys with expiry and deleting those that are expired, repeating this often to keep memory clean.
Look at what repeats in this process.
- Primary operation: Checking a small fixed number of keys for expiry.
- How many times: This check runs repeatedly over time, but each time only a small sample is checked.
Even if the total number of keys grows, Redis only checks a small fixed sample each time.
| Input Size (n) | Approx. Operations per check |
|---|---|
| 10 | 5 checks |
| 100 | 5 checks |
| 1000 | 5 checks |
Pattern observation: The number of operations per check stays the same, no matter how many keys exist.
Time Complexity: O(1)
This means the time to check and delete expired keys does not grow as the number of keys grows; it stays constant.
[X] Wrong: "Checking all keys for expiry takes longer as more keys are added."
[OK] Correct: Redis does not check all keys at once. It samples a small number repeatedly, keeping the work steady regardless of total keys.
Understanding how Redis manages key expiry efficiently shows your grasp of practical system design. It highlights how smart sampling keeps performance steady even with lots of data.
"What if Redis checked all keys for expiry every time instead of a small sample? How would the time complexity change?"
Practice
EXPIRE command do in Redis?Solution
Step 1: Understand the purpose of EXPIRE
The EXPIRE command sets a countdown timer on a key, after which Redis deletes it automatically.Step 2: Differentiate from other commands
Unlike DEL which deletes immediately, EXPIRE schedules deletion after a set time.Final Answer:
Sets a time limit after which a key will be automatically deleted -> Option AQuick Check:
EXPIRE = set auto-delete timer [OK]
- Confusing EXPIRE with DEL command
- Thinking EXPIRE shows time left instead of setting it
- Assuming EXPIRE removes expiration instead of adding it
session to expire in 120 seconds?Solution
Step 1: Recall EXPIRE command syntax
The correct syntax isEXPIRE key seconds, soEXPIRE session 120sets expiry to 120 seconds.Step 2: Check other options
EXPIRE 120 session reverses arguments, A is incomplete syntax for SETEX, D is invalid usage of TTL.Final Answer:
EXPIRE session 120 -> Option DQuick Check:
EXPIRE key seconds = EXPIRE session 120 [OK]
- Swapping key and seconds in EXPIRE
- Using TTL to set expiry instead of check
- Confusing SETEX syntax
SET user:1 "Alice" EXPIRE user:1 10 TTL user:1
What will the
TTL user:1 command return immediately after setting expiry?Solution
Step 1: Understand TTL after EXPIRE
After setting EXPIRE to 10 seconds, TTL returns the remaining time in seconds, which is 10 immediately.Step 2: Interpret other options
-1 means no expiry, 0 means expired, Error is invalid here.Final Answer:
10 -> Option CQuick Check:
TTL after EXPIRE = expiry seconds [OK]
- Expecting TTL to return -1 immediately
- Thinking TTL returns 0 right after EXPIRE
- Confusing TTL with DEL command
EXPIRE mykey 30 but TTL mykey returns -2. What is the likely problem?Solution
Step 1: Understand TTL return values
TTL returns -2 if the key does not exist in Redis.Step 2: Analyze the scenario
Since TTL returns -2, it meansmykeyis missing, so EXPIRE had no effect.Final Answer:
The keymykeydoes not exist -> Option BQuick Check:
TTL = -2 means key missing [OK]
- Assuming -2 means no expiry set
- Thinking EXPIRE syntax is wrong
- Ignoring that key might be deleted
cache:data but remove its expiry to prevent automatic deletion. Which command should you use?Solution
Step 1: Identify command to remove expiry
The PERSIST command removes the expiry time from a key, making it permanent.Step 2: Check other options
EXPIRE with 0 deletes immediately, DEL deletes key, TTL only checks expiry time.Final Answer:
PERSIST cache:data -> Option AQuick Check:
PERSIST removes expiry timer [OK]
- Using EXPIRE 0 which deletes key immediately
- Confusing DEL with removing expiry
- Using TTL expecting it to remove expiry
Ready to prove it?
Pass the test and this pattern moves unknown
