0
0
Redisquery~5 mins

KEYS pattern matching (avoid in production) in Redis

Choose your learning style9 modes available
Introduction
The KEYS command helps find all keys in Redis that match a pattern. It is like searching for files by name on your computer.
When you want to see all keys that start with a certain word.
When you need to find keys containing a specific part in their name.
When you want to check what keys exist during development or debugging.
When you want to clean up keys matching a pattern in a test environment.
Syntax
Redis
KEYS pattern
The pattern can include * as a wildcard to match any characters.
Avoid using KEYS in production because it can slow down Redis if many keys exist.
Examples
Finds all keys that start with 'user:'
Redis
KEYS user:*
Finds all keys that contain 'session' anywhere in their name
Redis
KEYS *session*
Finds all keys in the database
Redis
KEYS *
Sample Program
This example sets three keys and then finds all keys starting with 'user:'.
Redis
SET user:1 Alice
SET user:2 Bob
SET session:123 active
KEYS user:*
OutputSuccess
Important Notes
The KEYS command scans all keys, so it can be slow if you have many keys.
Use SCAN command in production for safer, incremental key searching.
Common mistake: Using KEYS on a large database can cause Redis to freeze temporarily.
Summary
KEYS finds keys matching a pattern using wildcards.
It is useful for quick checks or debugging.
Avoid KEYS in production; prefer SCAN for large datasets.