Update 11th August 2015: As from version 0.8.0 beta 1, Redis Desktop Manager now supports the SCAN command (rather than KEYS) for Redis 2.8 onwards. Although this limits the applicability of this article to older servers, I am more than happy that this shortcoming has been addressed. The original article below remains both for historical purposes and to warn developers of incorrect use of the KEYS command.
There’s a tool called Redis Desktop Manager which can sometimes be useful to inspect the keys in a Redis database. Indeed one of its features is that it presents a treeview showing a structured representation of the keys in the database:
But how is this treeview built? That’s easy to find out, by using the Redis MONITOR command to see the incoming commands:
The first two commands are executed when Redis Desktop Manager connects to the Redis server, and the other two are executed when the database is expanded in the treeview, revealing the keys in that database.
You’ll notice that the last command is a KEYS command, which with its wildcard argument (*) is effectively retrieving every key in the database. We can see an example of what this gives us by running the KEYS command ourselves:
Now, in this case I only have a handful of keys in my Redis database, but it’s pretty normal for real Redis databases to have very large numbers of keys. Retrieving all that data places a large burden on the Redis server, which due to its single-threaded nature will not be able to serve other requests while it is stuck retrieving every key in the database.
In fact, the KEYS command documentation particularly warns against its use in production environments:
“Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don’t use KEYS in your regular application code. If you’re looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.”
I understand why Redis Desktop Manager uses the KEYS command: it needs to retrieve all the keys in the database in order to determine how the tree structure will be displayed (since each delimited part of the key is rendered as a node). That’s the whole point of having a treeview.
However, what seems to be a useful feature can actually be very dangerous, especially when used on production servers. So do take care when using Redis Desktop Manager in such environments.
My recommendation to developers using Redis is to keep good documentation of your keys, so you won’t need any Redis command to tell you what keys are in the database. That’s not what Redis is for.
But if you do ever need to inspect your Redis keys on the server, at least follow the advice in the documentation and use SCAN instead. While this may still be expensive to retrieve the entire set of keys, it can be done in small batches, thus allowing other requests to be serviced in between iterations.