Redis Keys vs Scan Command

Retrieve All Keys in Redis: Using KEYS vs SCAN Commands

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.

  • pattern: A string pattern with optional wildcards like * (matches any sequence of characters) or ? (matches a single character).

Example

To fetch all keys:

To fetch keys starting with “user:”:

Sample Output


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

  • 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:

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:

Second iteration:

Repeat until the cursor returns 0.


Pros and Cons

FeatureKEYSSCAN
PerformanceBlocks the server for large keyspaces.Non-blocking, works incrementally.
Time ComplexityO(N) with N being the number of keys.O(1) per call, scans the keyspace in chunks.
Use in ProductionNot recommended for large keyspaces.Safe for production environments.
Pattern MatchingSupports wildcard patterns.Supports wildcard patterns.
Memory UsageHigh for large responses.Efficient, as results are streamed.
Ease of UseSimple 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, prefer SCAN 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 in SCAN 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:

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.


  1. Redis KEYS Command Documentation
  2. Redis SCAN Command Documentation
  3. Stack Overflow: Redis command to get all available keys

Leave a Reply

Your email address will not be published. Required fields are marked *