Redis KEYS Command
The KEYS command is a super useful command when developing in Redis. We’ll quickly talk about what the Redis KEYS command does and then jump straight into some examples of how you use it. Let’s jump right in!
KEYS Command and What it Does
The KEYS command returns all the Redis keys that match a pattern. You provide the KEYS command with the pattern and it’ll give you back the keys that match. It is very similar to the SCAN command especially when SCAN is used with MATCH. It’s pattern matching syntax is the hardest part about the command. If you’re already familiar with regex ( regular expressions ) then it should be a walk in the park. If you’re not familiar with regex we’ll demonstrate some examples that will show you some of the basics.
A Production No-No
The KEYS command shouldn’t be used in production because it returns all the results. If there’s tons of results this could be problematic and could cause performance issues or worse. That’s why the SCAN command is a better choice for a production environment. But if you’re working in a development environment then KEYS is a great tool.
Let’s jump straight into some examples! We’ll do a FLUSHALL command between each demo to clear all keys and values.
The most basic use is to find a key that is an exact match to a string. It might look like this:
127.0.0.1:6379> KEYS key1
But what happens when you search for a key that doesn’t exist. Let’s see:
(empty list or set)
Using the ? to Match Any Character
Let’s create a few keys:
What if we wanted to get both of these keys using KEYS? ‘cat’ and ‘cut’ only differ by the middle character so we could use the ‘?’ which will match any single character. So to match both we could use KEYS like this:
Using the * to match any string or no string
What if we had the following keys:
We could match all of these using ‘‘. The ‘‘ will match any string including or no string at all ( A string of 0 characters).
Let’s see it in action:
127.0.0.1:6379> KEYS h*llo
Noticed how the ‘‘ matched each key: It matched the ‘eee’ in ‘heeello’ It matched the ‘a’ in ‘hallo’ This is the case where it still matched because it matched ‘‘ will still match an empty string. It matched the ‘e’ in ‘hello’
Using [abcd] to match a specific set of characters
If you want to match only specific characters this next matching pattern will be of great use. Let’s jump straight into another example to demonstrate:
So we have var_a, var_b, var_c, var_d, and var_e as our keys. What if we wanted a pattern that would only match var_b and var_c? We could use this command:
The characters inside the brackets are the ones you want to match, so it will only match characters ‘b’ and ‘c’.
Using [^abcd] to match any character but this set
Oftentimes you’ll want to match all characters except a certain few. There is also pattern matching syntax for that case.
Say we have this:
Now we want to have a pattern that gives us back var_a, var_b, var_c, var_d, BUT NOT var_e. We will use a pattern matching syntax that will match every character except ‘e’. To do that use
[^e] and use it in your KEYS command like so:
Match a range of characters with [a-z]
If you want to match a range of characters say only lowercase alphabetical characters ‘a’ to ‘z’ you could use syntax like this
[a-z]. Let’s see an example, first let’s set some keys:
So now we have
var_2. Now if we wanted to use KEYS to single out
var_b you could use the KEYS command like this:
If you wanted to do the opposite and isolate the keys with numbers at the end you could use:
We hope you’ve enjoyed this summary of the KEYS command. We’ve explained what it does and the pattern matching syntax used in the command. Remember that this command isn’t typically used in production with large data sets.
If you’re looking for someone you trust to handle your database needs including Redis, Elasticsearch, MongoDB, CockroachDB, and PostgreSQL, please don’t hesitate to reach out to us at Object Rocket so we can discuss your exact needs.