Redis is an in-memory data structure store, often used as a database, cache, and message broker. A common task when working with Redis is retrieving a list of keys that match certain criteria, or simply all keys in the database. This tutorial will cover the different methods available to accomplish this, highlighting their performance implications and best practices.
Listing Keys with the KEYS
Command
The most straightforward way to retrieve keys is using the KEYS
command. This command accepts a pattern as an argument and returns all keys matching that pattern.
KEYS *
: This retrieves all keys in the current Redis database.KEYS pattern*
: Retrieves keys starting with a specific pattern.KEYS *pattern
: Retrieves keys ending with a specific pattern.KEYS ?
: Retrieves all keys of length 1.KEYS *t*
: Retrieves keys that contain the character ‘t’.
Example (using redis-cli
):
redis-cli KEYS *
This will output a list of all keys in the currently selected Redis database.
Important Considerations with KEYS
:
While simple, the KEYS
command has significant performance implications, particularly in large databases.
- Blocking Operation:
KEYS
is a blocking operation. While it’s executing, Redis will be unable to process other commands, potentially impacting application responsiveness. - Memory Usage: Retrieving all keys requires Redis to create a list of all matching keys in memory before returning them to the client. This can consume a substantial amount of memory, especially if the database contains a large number of keys, potentially leading to server instability.
The SCAN
Command: A Better Approach
To avoid the performance issues associated with KEYS
, Redis 2.8 introduced the SCAN
command. SCAN
is an iterator that allows you to retrieve keys in batches, avoiding blocking the server and reducing memory consumption.
How SCAN
Works:
SCAN
returns a cursor and a limited number of keys that match the provided pattern. You then use the cursor in subsequent SCAN
commands to retrieve the next batch of keys. This process continues until the cursor is set to 0, indicating that all keys have been retrieved.
Syntax:
SCAN [MATCH pattern] [COUNT count]
MATCH pattern
: Filters the keys returned by the scan, similar to theKEYS
command’s pattern matching. Using*
will match all keys.COUNT count
: Specifies the approximate number of keys to return in each batch. Increasing this value can improve performance, but also increases memory usage per call.
Example (using redis-cli
):
redis-cli SCAN 0 MATCH * COUNT 100
This will start a scan from cursor 0, matching all keys, and requesting a batch of 100 keys. The output will be a cursor and a list of keys.
12345 [key1, key2, key3, ...]
To get the next batch, use the returned cursor (12345 in this example) in another SCAN
command:
redis-cli SCAN 12345 MATCH * COUNT 100
Continue this process until the cursor is 0.
Selecting the Correct Database
Remember that Redis supports multiple databases (numbered from 0 to the maximum configured value). If you are not connected to the correct database, the KEYS
or SCAN
commands will only return keys from the currently selected database.
To select a specific database, use the SELECT
command:
SELECT <dbnumber>
For example, to select database number 1:
SELECT 1
Counting Keys
Often you may only need to know the number of keys matching a pattern, not the keys themselves. Redis provides the dbsize
command to accomplish this.
dbsize
This command returns the total number of keys in the currently selected database. It is much faster than using SCAN
or KEYS
just to count the keys.
Best Practices
- Prefer
SCAN
overKEYS
: For large databases, always useSCAN
to avoid blocking the server and consuming excessive memory. - Use appropriate
COUNT
withSCAN
: Experiment with differentCOUNT
values to find the optimal balance between performance and memory usage. - Select the correct database: Ensure you are connected to the correct database before running
KEYS
,SCAN
, ordbsize
. - Use
dbsize
when counting: If you only need the number of keys,dbsize
is the most efficient option.