Bird
Raised Fist0
Redisquery~20 mins

KEYS pattern matching (avoid in production) in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis KEYS Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
What does the KEYS command return?
Given these keys in Redis: user:1, user:2, user:admin, session:1, session:2, what is the output of KEYS user:*?
Redis
KEYS user:*
A["user:1", "user:2"]
B["session:1", "session:2"]
C["user:1", "user:2", "user:admin"]
D[]
Attempts:
2 left
💡 Hint
The * wildcard matches any characters after 'user:'.
🧠 Conceptual
intermediate
1:30remaining
Why should KEYS be avoided in production?
Which of the following is the main reason to avoid using the KEYS command in a production Redis environment?
AIt requires special permissions that are unsafe to grant.
BIt deletes keys permanently without confirmation.
CIt only works with string keys, not hashes or lists.
DIt blocks the server and can cause latency spikes because it scans all keys.
Attempts:
2 left
💡 Hint
Think about what happens when KEYS scans a large database.
📝 Syntax
advanced
1:30remaining
Which KEYS command pattern matches keys ending with ':id'?
You want to find all keys that end with ':id'. Which KEYS command pattern is correct?
AKEYS *id:
BKEYS *:id
CKEYS :id*
DKEYS id:*
Attempts:
2 left
💡 Hint
The * wildcard matches any characters before ':id'.
❓ optimization
advanced
1:30remaining
How to safely find keys matching a pattern in production?
Since KEYS is unsafe in production, which Redis command is recommended to find keys matching a pattern without blocking the server?
ASCAN with MATCH option
BDEL with pattern
CGET with pattern
DEXPIRE with pattern
Attempts:
2 left
💡 Hint
This command iterates keys incrementally without blocking.
🔧 Debug
expert
1:30remaining
What error occurs with invalid KEYS pattern?
What happens if you run KEYS user[ in Redis?
Redis
KEYS user[
ASyntax error due to incomplete pattern
BReturns an empty list because no keys match
CReturns all keys starting with 'user['
DRuntime error: invalid argument
Attempts:
2 left
💡 Hint
Check if the pattern syntax is correct.

Practice

(1/5)
1. What does the Redis command KEYS * do?
easy
A. Returns all keys in the current database
B. Deletes all keys in the current database
C. Returns the number of keys in the current database
D. Sets a new key with a wildcard name

Solution

  1. Step 1: Understand the KEYS command

    The KEYS command searches for keys matching a given pattern.
  2. Step 2: Analyze the pattern *

    The pattern * matches all keys in the current database.
  3. Final Answer:

    Returns all keys in the current database -> Option A
  4. Quick Check:

    KEYS * lists all keys [OK]
Hint: Remember * matches everything in KEYS command [OK]
Common Mistakes:
  • Thinking KEYS deletes keys
  • Confusing KEYS with COUNT commands
  • Assuming KEYS sets keys
2. Which of the following is the correct unquoted syntax to find keys starting with 'user:' using KEYS?
easy
A. KEYS user:*
B. KEYS user*
C. KEYS 'user:*'
D. KEYS 'user*'

Solution

  1. Step 1: Understand pattern syntax in KEYS

    Patterns do not require quotes; wildcards like * match any characters.
  2. Step 2: Match keys starting with 'user:'

    The pattern should be user:* without quotes to match keys starting with 'user:'.
  3. Final Answer:

    KEYS user:* -> Option A
  4. Quick Check:

    Use unquoted patterns with * wildcard [OK]
Hint: Use unquoted patterns with * for KEYS [OK]
Common Mistakes:
  • Adding quotes around pattern
  • Using colon without wildcard
  • Using wrong wildcard placement
3. Given keys: user:1, user:2, admin:1, what is the output of KEYS user:*?
medium
A. ["admin:1"]
B. ["user:1", "user:2", "admin:1"]
C. ["user:1", "user:2"]
D. []

Solution

  1. Step 1: Identify keys matching pattern user:*

    Keys starting with 'user:' are user:1 and user:2.
  2. Step 2: Exclude keys not matching pattern

    admin:1 does not start with 'user:', so it is excluded.
  3. Final Answer:

    ["user:1", "user:2"] -> Option C
  4. Quick Check:

    Pattern matches keys starting with 'user:' [OK]
Hint: Match only keys starting with pattern prefix [OK]
Common Mistakes:
  • Including keys with different prefixes
  • Returning empty list incorrectly
  • Confusing pattern syntax
4. What is wrong with this command: KEYS user?*?
medium
A. The question mark is not a valid wildcard in KEYS
B. The pattern should be quoted
C. The command will delete keys instead of listing
D. The pattern matches only keys with exactly one character after 'user' followed by any characters

Solution

  1. Step 1: Understand the question mark wildcard in KEYS

    The question mark ? matches exactly one character in Redis patterns.
  2. Step 2: Analyze the pattern user?*

    This pattern matches keys starting with 'user', followed by exactly one character, then any characters.
  3. Final Answer:

    The pattern matches only keys with exactly one character after 'user' -> Option D
  4. Quick Check:

    ? matches exactly one character [OK]
Hint: Remember ? matches exactly one character in KEYS [OK]
Common Mistakes:
  • Thinking ? is invalid wildcard
  • Assuming pattern deletes keys
  • Forgetting ? matches exactly one character
5. You want to find all keys containing the word 'session' anywhere in their name without blocking Redis in production. Which approach is best?
hard
A. Use KEYS *session* command directly
B. Use SCAN 0 MATCH *session* iteratively
C. Use GET *session* command
D. Use DEL *session* to remove keys first

Solution

  1. Step 1: Understand why KEYS is discouraged in production

    KEYS blocks Redis and can cause performance issues on large datasets.
  2. Step 2: Use SCAN with MATCH for safe iteration

    SCAN iterates keys incrementally without blocking, using MATCH *session* to filter keys containing 'session'.
  3. Final Answer:

    Use SCAN 0 MATCH *session* iteratively -> Option B
  4. Quick Check:

    SCAN is safe for production key matching [OK]
Hint: Use SCAN with MATCH to avoid blocking Redis [OK]
Common Mistakes:
  • Using KEYS in production causing blocking
  • Using GET with wildcards (invalid)
  • Deleting keys without checking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard