What is the method of redis traversal keys and database management

01-19-2023

This article introduces the relevant knowledge of Redis traversal keys and database management methods. During the operation of actual cases, many people will encounter such difficulties. Next, let the editor guide you to learn How to deal with these situations! I hope you read it carefully and learn something!

1 Traversal key

1.1 Full traversal key

Sometimes we need to traverse all keys in full, so we need to use keys pattern command, and this command supports pattern matching


127.0.0.1:6379> mset name luke neme josh OK



If you want to traverse all the keys, you can use the command keys *

127.0.0.1:6379> keys * 1) "name" 2) "neme"


pattern uses glob-style wildcards, where:

  • * stands for any character

  • ? Represents a character

  • [] represents matching part of the character, for example [a,b] means matching a, b two characters, [1-10] means matching 1 to 10 Any number

  • \x means escape, when you need to match the character *, you need to escape

We can do the following:


127.0.0.1:6379> keys n[a,e]me 1) "name" 2) "neme"



You can also do this


127.0.0.1 :6379> keys n?me 1) "name" 2) "neme"



But when there are a large number of keys in the redis database, the keys will block redis.

What should we do if we need to traverse keys?

Generally, our production environment is multi-node, so we can find a redis slave node that does not provide external services to traverse data, but if the amount of data is large, redis will still be blocked, but for slave nodes, It only affects master-slave replication.

If you are sure that there are not many keys on redis, you can execute it directly.

1.2 Progressive traversal

Progressive traversal is to traverse some keys each time, and then return, and then follow the data after traversal next time. In this way, all data can be traversed without blocking the redis service.


scan cursor [MATCH pattern] [COUNT count]



The parameters are explained as follows:

  • cursor is a required parameter, it is a cursor, which indicates where the traversal is, next time it will start from this cursor, if it returns 0, it means the traversal is complete up.

  • MATCH pattern is an optional parameter, which is the same as the pattern of keys

  • COUNT count means to traverse several keys, The default is 10, and it can be increased according to the actual situation


127.0.0.1:6379> mset a 1 b 1 c 1 d 1 e 1 f 1 g 1 h 1 i 1 g 1 k 1 l 1 m 1 n 1 o 1 p 1 q 1 r 1 s 1 t 1 u 1 v 1 w 1 x 1 y 1 z 1 OK



We use scan to traverse, the first execution returns as follows:

127.0.0.1:6379> scan 0 1) "1" 2) 1) "l" 2) "f" 3) "k" 4) "y" 5) "c" 6) "e" 7) "w" 8) "d" 9) "b" 10) "o" 11) "q"


Use the 1 returned for the first time to traverse for the second time, and you can traverse to 10 keys again


127.0.0.1:6379> scan 1 1) "23" 2) 1) "v" 2) "u" 3) "z" 4) "g" 5) "n" 6) "s" 7) "i" 8) "a" 9) "r" 10) "t"



The third use the secondThe 23 times returned to traverse, when the return is 0, it means that the traverse is completed


127.0.0.1:6379> scan 23 1) "0" 2) 1) "x" 2) "h" 3) "m" 4) "p"



At the same time, there are hscan for hash type, sscan for collection type, and The zscan of sequenced collection is used in the same way as scan

2 Database management

redis also has several commands for database operations: dbsize, select, flushdb/flushall

2.1 Switch databases, select

select dbIndex dbIndex is the corresponding database serial number, there are 16 databases in the default redis configuration, select the number to switch to the database .

For example, set a key in the default database number 0


127.0.0.1:6379> set name luke OK



Then we switch to the No. 1 database to get this key, but we can’t get it, indicating that each database in a redis service are not interoperable.


127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> get name (nil)



So can it be used as multiple redis, of course not. Although there are more libraries, because redis is single-threaded, it is actually a CPU. If a command in a database executes slowly, other libraries will also be affected. So in this case, if it is blocked by other libraries, For developers using a certain library, it is difficult to analyze what is wrong.

2.2 flushall/flushdb

The difference between flushall and flushdb is that flushall will clear all data in all libraries, while flushdb will only clear the current database.

This is easy to understand, we will not give examples, but it should be noted that these two commands will clear all data, and the consequences of misoperation will be unimaginable. And when there are too many keys, redis will also be blocked, so you must be cautious when using these two commands.


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us

Recommended reading

high perspicacity