Back to results for “Performance”
Redis › Redis CLI & Basic Commands

How do you search for keys matching a specific pattern in Redis, and why is the KEYS command generally discouraged in production?

Intermediate

Answer

You can use the KEYS command with a wildcard pattern to find matching key names, but it is generally discouraged in production because it scans the entire keyspace in a single blocking operation, which can significantly slow down the server on a large dataset, and the SCAN command is recommended instead since it iterates through the keyspace incrementally without blocking.
-- Avoid in production on a large dataset
KEYS user:*

-- Preferred alternative
SCAN 0 MATCH user:* COUNT 100
Real-world example A team investigating a brief production slowdown discovers a monitoring script had been calling KEYS on a large dataset, and switches it to use SCAN instead, eliminating the blocking behavior that was causing the issue.

Common follow-ups: How does SCAN's cursor based iteration actually work?;Are there any guarantees SCAN provides about not missing or duplicating keys?