DEL and UNLINK for deletion in Redis - Time & Space Complexity
When deleting keys in Redis, it's important to know how the time it takes grows as you delete more keys.
We want to understand how the commands DEL and UNLINK behave as the number of keys changes.
Analyze the time complexity of these Redis commands:
DEL key1 key2 key3 ... keyN
UNLINK key1 key2 key3 ... keyN
These commands delete one or more keys from the database, but they work differently internally.
Look at what repeats when deleting multiple keys:
- Primary operation: Deleting each key one by one.
- How many times: Once for each key given to DEL or UNLINK.
As you increase the number of keys to delete, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 delete actions |
| 100 | About 100 delete actions |
| 1000 | About 1000 delete actions |
Pattern observation: The time grows linearly as you add more keys to delete.
Time Complexity: O(n)
This means the time to delete keys grows directly with the number of keys you delete.
[X] Wrong: "UNLINK deletes keys instantly with no cost regardless of how many keys."
[OK] Correct: UNLINK frees memory asynchronously but still must process each key, so its time grows with the number of keys.
Understanding how deletion commands scale helps you write efficient Redis code and explain your choices clearly in real projects or interviews.
"What if we delete keys one by one in a loop instead of passing them all at once? How would the time complexity change?"
Practice
DEL and UNLINK commands in Redis?Solution
Step 1: Understand
DELbehaviorDELremoves keys immediately and blocks Redis until deletion finishes.Step 2: Understand
UNLINKbehaviorUNLINKremoves keys asynchronously in the background without blocking Redis.Final Answer:
DELdeletes keys immediately and blocks Redis, whileUNLINKdeletes keys asynchronously in the background. -> Option AQuick Check:
Deletion blocking =DEL[OK]
- Thinking UNLINK blocks Redis like DEL
- Believing DEL deletes keys asynchronously
- Confusing number of keys each command can delete
key1 and key2 using the UNLINK command in Redis?Solution
Step 1: Recall Redis command syntax
Redis commands take keys as space-separated arguments without brackets or quotes.Step 2: Match syntax for multiple keys
UNLINK key1 key2correctly deletes both keys at once.Final Answer:
UNLINK key1 key2 -> Option BQuick Check:
Space-separated keys = correct syntax [OK]
- Adding parentheses or brackets around keys
- Using quotes around keys unnecessarily
- Separating keys with commas
SET key1 "value1"SET key2 "value2"DEL key1UNLINK key2What keys remain in the database after these commands?
Solution
Step 1: Analyze
DEL key1DEL key1deleteskey1immediately.Step 2: Analyze
UNLINK key2UNLINK key2deleteskey2asynchronously, but it will be removed eventually.Final Answer:
No keys remain -> Option AQuick Check:
Both keys deleted = no keys left [OK]
- Assuming UNLINK does not delete keys
- Thinking UNLINK delays deletion indefinitely
- Believing DEL only marks keys for deletion
DEL key1 key2 but get an error. Which of the following is the most likely cause?Solution
Step 1: Check syntax for multiple keys
Redis commands require keys separated by spaces, not commas.Step 2: Identify error cause
Using commas likeDEL key1, key2causes syntax error.Final Answer:
You used commas between keys like DEL key1, key2 -> Option DQuick Check:
Keys must be space-separated [OK]
- Using commas between keys
- Thinking DEL can't delete multiple keys
- Confusing missing keys with syntax errors
Solution
Step 1: Understand blocking behavior of
DELDELblocks Redis and deleting 1000 large keys at once can cause latency spikes.Step 2: Use
UNLINKin batchesUNLINKdeletes keys asynchronously; batching avoids large memory spikes and keeps Redis responsive.Final Answer:
Use UNLINK on keys in small batches asynchronously -> Option CQuick Check:
Batch UNLINK = safe large deletion [OK]
- Using DEL for large bulk deletes causing blocking
- Deleting keys one by one causing slow performance
- Using UNLINK without batching causing memory spikes
Ready to prove it?
Pass the test and this pattern moves unknown
