On Redis Desktop Manager and Redis Keys
Join the DZone community and get the full member experience.Join For Free
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.
Published at DZone with permission of Daniel D'agostino, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.