Skip to main content

Description

Returns all keys matching a glob-style pattern. This command should be used with caution in production environments as it can impact performance on large databases.
Consider using SCAN instead of KEYS for production use cases. KEYS blocks the server until it completes, while SCAN provides an incremental iteration approach.

Method Signature

Parameters

pattern
string
required
The glob-style pattern to match keys against. Supported glob patterns:
  • * matches any number of characters (including zero)
  • ? matches exactly one character
  • [abc] matches one character from the set
  • [^abc] matches one character not in the set
  • [a-z] matches one character from the range
  • \ escapes special characters

Return Value

result
string[]
Array of keys matching the pattern. Returns an empty array if no keys match.

Examples

Match all keys

Match with prefix

Match with wildcard in middle

Match single character

Match character range

No matches

Performance Considerations

KEYS is a blocking operation that scans the entire keyspace. For production use:
  • Use SCAN for incremental iteration
  • Avoid using KEYS on large databases
  • Consider using key naming schemes that allow targeted queries

See Also

  • scan - Incrementally iterate over keys
  • exists - Check if specific keys exist