Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Command

KEYS

Returns every key name matching a glob-style pattern. Because it can block the Redis server while producing the complete result, reserve it for debugging and exceptional administrative tasks.

How it works

KEYS scans the database for every key name matching a glob-style pattern and returns the matches as an array. Pattern characters can select exact names, prefixes, suffixes, or single-character variations, but the command runs synchronously and can block the server while collecting results.

Syntax

KEYS pattern

Parameters

NameTypeDescription
patternstringA glob-style pattern used to match key names.

Returns

An array reply containing all key names that match the pattern.

Key points

  • The command returns matching key names rather than their values or metadata.
  • Its sole required argument is a glob-style pattern applied to key names.
  • Redis classifies KEYS as a keyspace read command that is slow and potentially dangerous.
  • A match can produce multiple keys, including an empty result when no name fits.

Common mistakes

  • Avoid KEYS on large databases or in latency-sensitive application paths because it blocks the server until the complete result is available.
  • Use it for debugging or exceptional keyspace operations rather than normal application logic.
  • Cluster deployments may not provide the same keyspace behavior as standalone Redis; consult the cluster guidance before relying on the result.

Example: Find keys by prefix

redis-cli SET user:1 Alice
redis-cli SET user:2 Bob
redis-cli KEYS 'user:*'

Creates two sample keys, then lists keys whose names begin with user:.

When to use it

Debug key naming

Inspecting a development database

Special keyspace operations

Renaming or restructuring a keyspace during maintenance

Targeted inspection

Checking whether keys with a naming prefix exist

Interview questions

QWhat does the KEYS command do?

KEYS takes one glob-style pattern and returns every key name matching it. For example, KEYS user:* matches keys whose names begin with user:.

Follow-up: Why can KEYS harm latency? It blocks the Redis server while all matching keys are returned, so large databases can experience substantial pauses.

QHow is KEYS pattern matching specified?

The pattern is the required argument and uses glob-style matching. A literal pattern matches one name, while wildcard patterns can match groups of names.

Follow-up: What should replace KEYS for incremental traversal? SCAN is the documented alternative when iterating through a keyspace subset because it avoids requesting the entire result at once.

QWhen is KEYS appropriate?

KEYS is intended for debugging and special operations, such as changing keyspace layout. It should not be used in regular application code because it can block the server.

Follow-up: How should a production application find a subset of keys? Prefer SCAN or an appropriate set-based design instead of scanning the full keyspace with KEYS.

  • SCAN
  • SET
  • DEL
  • MGET

Source: KEYS | Docs - Redis. Checked 2026-10-16.