Why key management matters in Redis - Performance Analysis
When working with Redis, how you manage keys affects how fast your commands run.
We want to see how the number of keys impacts the time it takes to do operations.
Analyze the time complexity of deleting multiple keys using the DEL command.
# Delete multiple keys at once
DEL key1 key2 key3 ... keyN
# Or delete keys matching a pattern
EVAL "return redis.call('DEL', unpack(redis.call('KEYS', ARGV[1])))" 0 pattern*
This code deletes a list of keys or all keys matching a pattern.
Look for repeated steps that take time as input grows.
- Primary operation: Deleting each key one by one.
- How many times: Once for each key to delete.
As you delete more keys, the time grows with the number of keys.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 delete steps |
| 100 | 100 delete steps |
| 1000 | 1000 delete steps |
Pattern observation: The time grows directly with how many keys you delete.
Time Complexity: O(n)
This means if you delete twice as many keys, it takes about twice as long.
[X] Wrong: "Deleting many keys is always instant because Redis is fast."
[OK] Correct: Even though Redis is fast, deleting more keys takes more time because each key must be removed.
Understanding how key count affects command time helps you write better Redis commands and avoid slowdowns.
"What if we used SCAN instead of KEYS to find keys before deleting? How would the time complexity change?"
Practice
Solution
Step 1: Understand key naming purpose
Clear and consistent key names help keep data organized in Redis.Step 2: Identify benefits of good key names
Organized keys make it easier to find and manage data quickly.Final Answer:
It helps organize data and makes it easier to find keys. -> Option AQuick Check:
Good key names = easier data management [OK]
- Assuming key names affect database size
- Believing key names control memory limits
- Confusing key names with connection settings
Solution
Step 1: Recognize common prefix pattern
Using a colon ':' is the standard way to separate prefixes in Redis keys.Step 2: Check each option's syntax
Only SET user:1001 "John" uses the colon prefix format correctly.Final Answer:
SET user:1001 "John" -> Option BQuick Check:
Prefix with ':' is correct Redis key style [OK]
- Using dashes or slashes instead of colons
- Not using any separator between prefix and key
- Confusing key naming with command syntax
user:1, user:2, order:1, what will the command KEYS user:* return?Solution
Step 1: Understand the KEYS command with pattern
KEYS user:* matches all keys starting with 'user:' prefix.Step 2: Identify matching keys
Only 'user:1' and 'user:2' match the pattern; 'order:1' does not.Final Answer:
["user:1", "user:2"] -> Option DQuick Check:
KEYS user:* returns keys with 'user:' prefix [OK]
- Expecting keys with different prefixes to appear
- Thinking KEYS returns empty if any key exists
- Confusing pattern matching syntax
session: but your command DEL session:* returns an error. What is the problem?Solution
Step 1: Understand DEL command behavior
DEL command deletes specific keys; it does not support wildcards.Step 2: Identify correct deletion method
To delete keys by pattern, you must first find keys (e.g., with KEYS) then delete them individually.Final Answer:
DEL does not accept wildcards; you must delete keys individually. -> Option CQuick Check:
DEL needs exact keys, no wildcards [OK]
- Trying to use wildcards directly with DEL
- Thinking prefixes are invalid keys
- Believing Redis needs restart to delete keys
cache:item:1, cache:item:2, and cache:user:1. You want to delete only keys related to items efficiently. Which approach is best?Solution
Step 1: Identify keys to delete
Only keys with prefix 'cache:item:' should be deleted, not 'cache:user:'.Step 2: Choose efficient deletion method
SCAN with MATCH finds keys without blocking Redis; then delete keys individually.Step 3: Avoid unsafe commands
DEL with wildcards or FLUSHDB deletes too much; renaming keys is unnecessary.Final Answer:
Use SCAN 0 MATCH cache:item:* to find keys, then delete them one by one. -> Option AQuick Check:
SCAN + MATCH + DEL = safe selective deletion [OK]
- Using DEL with wildcards causing errors
- Flushing entire DB losing all data
- Renaming keys unnecessarily before deletion
Ready to prove it?
Pass the test and this pattern moves unknown
