Retrieving keys in Redis is a common task, but it’s important to choose the right command for the job. Redis provides two primary options for fetching keys: KEYS
and SCAN
. While both commands achieve similar results, they have different performance characteristics and use cases. In this guide, we’ll explain how each command works, provide examples, and discuss the advantages and limitations of each.
Method 1: Using the KEYS
Command
Note: The KEYS
command is a blocking command. Prefer to use the SCAN
command explaind in the next section for larger datasets.
The KEYS
command retrieves all keys that match a given pattern in the current Redis database.
KEYS pattern
pattern
: A string pattern with optional wildcards like*
(matches any sequence of characters) or?
(matches a single character).
Example
To fetch all keys:
redis-cli KEYS "*"
To fetch keys starting with “user:”:
redis-cli KEYS "user:*"
Sample Output
1) "user:123"
2) "user:456"
3) "user:789"
Method 2: Using the SCAN
Command
The SCAN
command retrieves keys in a non-blocking manner, returning a small subset of keys in each iteration.
Syntax
SCAN cursor [MATCH pattern] [COUNT count]
cursor
: An integer representing the iteration position. Start with 0 and continue until it returns 0 again.MATCH pattern
: Optional, filters keys matching the given pattern.COUNT count
: Optional, provides a hint for how many keys to return per iteration.
Example
To fetch all keys in batches:
redis-cli SCAN 0
Retreive keys matching a particular pattern:
redis-cli SCAN 0 MATCH "user:*"
To increase the batch size:
redis-cli SCAN 0 MATCH "user:*" COUNT 100
Sample Output
First iteration:
1) "1"
2) "user:123"
3) "user:456"
Second iteration:
1) "2"
2) "user:789"
Repeat until the cursor returns 0
.
Pros and Cons
Feature | KEYS | SCAN |
---|---|---|
Performance | Blocks the server for large keyspaces. | Non-blocking, works incrementally. |
Time Complexity | O(N) with N being the number of keys. | O(1) per call, scans the keyspace in chunks. |
Use in Production | Not recommended for large keyspaces. | Safe for production environments. |
Pattern Matching | Supports wildcard patterns. | Supports wildcard patterns. |
Memory Usage | High for large responses. | Efficient, as results are streamed. |
Ease of Use | Simple single command. | Requires loop to handle cursors. |
Key Differences
1. Blocking Behavior:
KEYS
: Executes in a single operation, blocking the Redis server during execution. This can degrade performance in production environments with large datasets.SCAN
: Iterative and non-blocking, allowing other operations to proceed while fetching keys.
2. Suitability for Production:
KEYS
: Suitable for debugging or small databases.SCAN
: Designed for production use, especially with large keyspaces.
3. Flexibility
KEYS
: Provides an all-at-once approach.SCAN
: Requires handling cursor state in application logic.
Best Practices
- Use
SCAN
in Production: For large datasets, preferSCAN
to avoid blocking the server. - Limit Key Fetching: Fetch only what you need. Use
MATCH
to narrow results and avoid overloading the network. - Set Appropriate Counts: Adjust the
COUNT
parameter inSCAN
to balance between performance and result size.
Example: Looping with SCAN
When using SCAN
, loop through the results until the cursor returns 0
. Here’s an example in Python using the Redis client:
import redis
client = redis.StrictRedis(host='localhost', port=6379, decode_responses=True)
# Initialize cursor
cursor = 0
while True:
cursor, keys = client.scan(cursor=cursor, match='user:*', count=10)
for key in keys:
print(key)
if cursor == 0:
break
This approach ensures you process all matching keys without overwhelming the server.
Conclusion
When retrieving keys in Redis:
- Use
KEYS
for debugging or small datasets where performance isn’t a concern. - Use
SCAN
for production environments to avoid blocking and ensure efficient memory usage.
Choosing the right command depends on your use case, but understanding their trade-offs ensures better performance and stability in your Redis-based applications.
Reference Links
- Redis KEYS Command Documentation
- Redis SCAN Command Documentation
- Stack Overflow: Redis command to get all available keys