SCAN Redis Command Examples

Introduction

In this article we’ll cover the SCAN Redis command with some examples to demonstrate. If you’re working with Redis you’ll need this command in your repertoire. Let’s get started!

What SCAN does?

SCAN iterates through all of the keys you have set in your database. You can see why this would be a very commonly used command. It takes in a position as an argument so you can indicate where to start. You can also set how many keys to return per call.

Let’s jump straight into some code. We’ll start by setting 20 variables so we have some data:

127.0.0.1:6379> MSET key1 value1 key2 value2 key3 value3 key4 value4 key5 value5 key6 value6 key7 value7 key8 value8 key9 value9 key10 value10 key11 value11 key12 value12 key13 value13 key14 value14 key15 value15 key16 value16 key17 value17 key18 value18 key19 value19 key20 value20
OK

Now let’s use the SCAN command with an argument of 0:

127.0.0.1:6379> SCAN 0
1) "22"
2)  1) "key2"
    2) "key3"
    3) "key4"
    4) "key17"
    5) "key1"
    6) "key16"
    7) "key5"
    8) "key11"
    9) "key14"
   10) "key15"
127.0.0.1:6379>

First it returned “22” which is our cursor. The cursor keeps track of how where we are at in the scan.

The second thing it returned was 10 keys. It only returned 10 because by default it doesn’t return all your keys, by default it only returns 10.

So how do we get the rest of the keys? You use the cursor it returned (“22”) and run another SCAN command passing it in as the argument:

127.0.0.1:6379> SCAN 22
1) "0"
2)  1) "key9"
    2) "key19"
    3) "key12"
    4) "key18"
    5) "key8"
    6) "key20"
    7) "key6"
    8) "key13"
    9) "key10"
   10) "key7"
127.0.0.1:6379>

The cursor value it returned is now “0” which indicates that the scan is complete.

MSET key1 value1 key2 value2 key3 value3 key4 value4 key5 value5 key6 value6 key7 value7 key8 value8 key9 value9 key10 value10 key11 value11 key12 value12 key13 value13 key14 value14 key15 value15 key16 value16 key17 value17 key18 value18 key19 value19 key20 value20

COUNT to Limit Results

We said earlier that by default SCAN returns keys in sets of 10. If you want more or less than 10 you can specify using COUNT. Let’s see an example where we only want 5 results:

127.0.0.1:6379> SCAN 0 COUNT 5 [MATCH pattern] [COUNT count]
1) "12"
2) 1) "key2"
   2) "key3"
   3) "key4"
   4) "key17"
   5) "key1"
127.0.0.1:6379>

If on the next iteration we want the rest of the results we could use:

127.0.0.1:6379> SCAN 12 COUNT 15
1) "0"
2)  1) "key16"
    2) "key5"
    3) "key11"
    4) "key14"
    5) "key15"
    6) "key9"
    7) "key19"
    8) "key12"
    9) "key18"
   10) "key8"
   11) "key20"
   12) "key6"
   13) "key13"
   14) "key10"
   15) "key7"
127.0.0.1:6379>

This is just to demo that you can change the count between iterations.

MATCH to only return keys match criteria

What if we wanted to return all they keys that ended in 5? How would you accomplish that? We’ll show you:

127.0.0.1:6379> SCAN 0 MATCH *5
1) "22"
2) 1) "key5"
   2) "key15"
127.0.0.1:6379>

Here we use MATCH to specify to only return keys that end in ‘5’. The ‘*’ is a placeholder that represents any string. If you wanted to return any key that started with ‘key1’ you would use:

127.0.0.1:6379> SCAN 0 MATCH key1*
1) "22"
2) 1) "key17"
   2) "key1"
   3) "key16"
   4) "key11"
   5) "key14"
   6) "key15"

What about scanning Sets and Hashes

There are also the SSCAN, HSCAN, and ZSCAN commands which perform similar functionality on sets and hashes.

Conclusion

We’ve covered the basic usage of the SCAN command in Redis. This will be an often used tool in your toolbelt so keep it sharp. We’ll be writing more articles on how to use Redis so stay tuned.

If you don’t want to manage the complexity of Redis at a production level or of any database technology for that matter, please don’t hesitate to reach out to us at Object Rocket. We’re eager to hear what you’re needs are and how we can help.

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.