What if your Redis server freezes just because you tried to find some keys?
Why KEYS pattern matching (avoid in production) in Redis? - Purpose & Use Cases
Imagine you have a huge collection of keys in your Redis database, like thousands or even millions of items. You want to find all keys that start with "user:" to see user data.
You try to look through each key one by one manually or by using a command that scans all keys.
Manually checking each key is very slow and can freeze your Redis server because it blocks other operations. It's like searching for a book in a huge library by opening every single book instead of using the index.
This causes delays and can crash your app if many users try to access Redis at the same time.
The KEYS command with pattern matching lets you quickly find keys matching a pattern like "user:*". It's like having a quick search tool that finds all books with titles starting with "user:".
But because it scans all keys at once, it can still slow down your server if used on large datasets.
KEYS *
KEYS user:*
It enables quick discovery of keys matching a pattern, making it easier to manage and debug Redis data.
A developer wants to delete all cache keys related to a specific user. Using KEYS "user:1234:*" helps find all those keys fast instead of guessing or deleting blindly.
Manual key searching is slow and blocks Redis.
KEYS command finds keys by pattern but can cause performance issues.
Use KEYS carefully and avoid in production on large datasets.
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
