Redis KEYS Command

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

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.

KEYS Examples

Let’s jump straight into some examples! We’ll do a FLUSHALL command between each demo to clear all keys and values.

1
2
127.0.0.1:6379> FLUSHALL
OK

Exact Match

The most basic use is to find a key that is an exact match to a string. It might look like this:

1
2
3
4
127.0.0.1:6379> SET key1 value1
OK
127.0.0.1:6379> KEYS key1
1) "key1"

But what happens when you search for a key that doesn’t exist. Let’s see:

1
2
127.0.0.1:6379> KEYS nope
(empty list or set)

Using the ? to Match Any Character

Let’s create a few keys:

1
MSET cat meow cut ouch

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:

1
2
3
127.0.0.1:6379> KEYS c?t
1) "cat"
2) "cut"

Using the * to match any string or no string

What if we had the following keys:

  • hello
  • hallo
  • heeello
  • hllo

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:

1
2
3
4
5
6
7
127.0.0.1:6379> MSET hello 1 hallo 1 heeello 1 hllo 1
OK
127.0.0.1:6379> KEYS h*llo
1) "heeello"
2) "hallo"
3) "hllo"
4) "hello"

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:

1
2
 127.0.0.1:6379> MSET var_a 1 var_b 1 var_c 1 var_d 1 var_e 1
 OK

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:

1
2
3
7.0.0.1:6379> KEYS var_[bc]
1) "var_b"
2) "var_c"

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:

1
2
 127.0.0.1:6379> MSET var_a 1 var_b 1 var_c 1 var_d 1 var_e 1
 OK

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:

1
2
3
4
5
127.0.0.1:6379> KEYS var_[^e]
1) "var_d"
2) "var_a"
3) "var_b"
4) "var_c"

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:

1
2
127.0.0.1:6379> MSET var_a 1 var_b 1 var_1 1 var_2 1
OK

So now we have var_a, var_b, var_1, and var_2. Now if we wanted to use KEYS to single out var_a and var_b you could use the KEYS command like this:

1
2
3
127.0.0.1:6379> KEYS var_[a-z]
1) "var_a"
2) "var_b"

If you wanted to do the opposite and isolate the keys with numbers at the end you could use:

1
2
3
127.0.0.1:6379> KEYS var_[0-9]
1) "var_1"
2) "var_2"

Conclusion

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.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.