KEYS pattern matching (avoid in production) in Redis - Time & Space Complexity
When using Redis, the KEYS command helps find keys matching a pattern. But it can be slow if many keys exist.
We want to know how the time to run KEYS grows as the number of keys grows.
Analyze the time complexity of the following Redis command.
KEYS user:*
This command finds all keys starting with "user:" by checking every key in the database.
Look for repeated work inside the command.
- Primary operation: Scanning every key in the database to check if it matches the pattern.
- How many times: Once for each key stored in Redis.
As the number of keys grows, the work grows too because Redis checks each key one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 key checks |
| 100 | About 100 key checks |
| 1000 | About 1000 key checks |
Pattern observation: The time grows directly with the number of keys. Double the keys, double the work.
Time Complexity: O(n)
This means the time to run KEYS grows linearly with the number of keys in the database.
[X] Wrong: "KEYS is fast even with many keys because Redis is very quick."
[OK] Correct: Redis checks every key one by one, so more keys mean more work and slower response.
Understanding how commands like KEYS scale helps you write better Redis queries and avoid slowdowns in real projects.
"What if we used SCAN instead of KEYS? How would the time complexity change?"
Practice
KEYS * do?Solution
Step 1: Understand the
TheKEYScommandKEYScommand searches for keys matching a given pattern.Step 2: Analyze the pattern
The pattern**matches all keys in the current database.Final Answer:
Returns all keys in the current database -> Option AQuick Check:
KEYS *lists all keys [OK]
- Thinking KEYS deletes keys
- Confusing KEYS with COUNT commands
- Assuming KEYS sets keys
Solution
Step 1: Understand pattern syntax in KEYS
Patterns do not require quotes; wildcards like * match any characters.Step 2: Match keys starting with 'user:'
The pattern should beuser:*without quotes to match keys starting with 'user:'.Final Answer:
KEYS user:* -> Option AQuick Check:
Use unquoted patterns with * wildcard [OK]
- Adding quotes around pattern
- Using colon without wildcard
- Using wrong wildcard placement
user:1, user:2, admin:1, what is the output of KEYS user:*?Solution
Step 1: Identify keys matching pattern
Keys starting with 'user:' areuser:*user:1anduser:2.Step 2: Exclude keys not matching pattern
admin:1does not start with 'user:', so it is excluded.Final Answer:
["user:1", "user:2"] -> Option CQuick Check:
Pattern matches keys starting with 'user:' [OK]
- Including keys with different prefixes
- Returning empty list incorrectly
- Confusing pattern syntax
KEYS user?*?Solution
Step 1: Understand the question mark wildcard in KEYS
The question mark?matches exactly one character in Redis patterns.Step 2: Analyze the pattern
This pattern matches keys starting with 'user', followed by exactly one character, then any characters.user?*Final Answer:
The pattern matches only keys with exactly one character after 'user' -> Option DQuick Check:
?matches exactly one character [OK]
- Thinking ? is invalid wildcard
- Assuming pattern deletes keys
- Forgetting ? matches exactly one character
Solution
Step 1: Understand why
KEYSis discouraged in productionKEYSblocks Redis and can cause performance issues on large datasets.Step 2: Use
SCANwithMATCHfor safe iterationSCANiterates keys incrementally without blocking, usingMATCH *session*to filter keys containing 'session'.Final Answer:
UseSCAN 0 MATCH *session*iteratively -> Option BQuick Check:
SCANis safe for production key matching [OK]
- Using KEYS in production causing blocking
- Using GET with wildcards (invalid)
- Deleting keys without checking
Ready to prove it?
Pass the test and this pattern moves unknown
