Overview of Redis data structure and global commands

This article talks about Redis data structure and global commands

Article Directory


Redis provides 5 data structures. Understanding the characteristics of each data structure is very important for the development, operation and maintenance of Redis. At the same time, mastering Redis's single-threaded command processing mechanism will make the selection of data structures and commands more effective.

Insert picture description here

The next few articles will introduce several Redis data structures, command usage and application scenarios from the following aspects .

Prior knowledge : a few simple global commands , data structures, and internal coding , single-threaded command analysis processing mechanism .

Data structural characteristics : five kinds of data structure characteristics, command , application scenarios .

Data management : key management , traversal key , database management .


Introducing the five kinds of data structures before, we need to understand some of Redis global commands, data structures, and internal coding, single-threaded command handling mechanism.

  • There are hundreds of Redis commands. If you understand some mechanisms of Redis, you will find that these commands have strong versatility.
  • Redis is not a panacea. Some data structures and commands must be used in specific scenarios. Improper use may cause fatal damage to Redis itself or the application itself.

Global command

Redis has 5 data structures, which are values ​​in key-value pairs, and there are some common commands for keys .

View all keys

keys *

Three key-value pairs of string type are inserted below :> set hello world
OK> set java java8
OK> set python python3

The command will output all the keys :> keys *
1) "hello"
2) "python"
3) "java">

Total number of keys


Insert a key-value pair of the list type below (the value is composed of multiple elements) :> rpush mylist a b c d e f g
(integer) 7

The dbsize command returns the total number of keys in the current database .> dbsize
(integer) 4

The dbsize command does not traverse all the keys when calculating the total number of keys, but directly obtains the total number of keys variable built in Redis, so the time complexity of the dbsiz command is O(1) . The key command will traverse all the keys, so its time complexity is O(n). When Redis stores a large number of keys, the online environment prohibits the use.

Check if the key exists

exists key

Return 1 if the key exists, 0 if it doesn't exist :> exists java
(integer) 1> exists go
(integer) 0>

Delete key

del key

del is a general command, no matter what data structure type the value is, the del command can delete it .> del java
(integer) 1> exists java
(integer) 0> del go
(integer) 0> exists go
(integer) 0>

The return result is the number of keys successfully deleted. If a key that does not exist is deleted, 0 will be returned .

Key expired

expire key seconds

Redis supports adding an expiration time to the key. When the expiration time is exceeded, the key will be automatically deleted. For example, set an expiration time of 10 seconds for the key hello :> set hello world
OK> expire hello 10
(integer) 1

The ttl command will return the remaining expiration time of the key. It has 3 return values:

  • An integer greater than or equal to 0: indicates the remaining expiration time of the key.
  • Return -1: The key does not set the expiration time.
  • Return -2: The key does not exist.

You can observe the remaining expiration time of the key hello through the ttl command :

# 还剩6秒> ttl hello
(integer) 6
# 还剩1秒> ttl hello
(integer) 1
# 返回结果为-2,说明键hello已经被删除> ttl hello
(integer) -2> set hello word
# 返回结果为-1,说明键hello没有设置过期时间> ttl hello
(integer) -1>

Key data structure type

type key

For example, the key hello is the value string type, and the return result is string. The value of the key mylist is a list type, and the return result is list. If the key does not exist, none is returned .> set aa bb
OK> type aa
string> rpush mylist a b c d e f g
(integer) 7> type mylist

Data structure and internal coding

The type command actually returns the data structure type of the current key. They are: string (string), hash (hash), list (list), set (collection), zset (ordered collection) , but these are only Redis External data structure. as the picture shows:

Insert picture description here

For each data structure, there is actually its own underlying internal coding implementation, and there are multiple implementations. In this way, Redis will select the appropriate internal coding in the appropriate scenario, as shown in the figure:

Insert picture description here

It can be seen that there are more than two internal coding implementations for each data structure. For example, the list data structure contains two internal codes, linkedlist and ziplist. At the same time, some internal codes, such as ziplist, can be used as internal implementations of various external data structures. The internal codes can be queried through the object encoding command:> set 11 22
OK> object encoding 11
"int"> set aa bb
OK> object encoding aa
"embstr"> set long 6666666666666666666666666666666666666666666666666666666666666666
OK> object encoding long
"raw"> object encoding mylist

You can see that the internal code of the value corresponding to the key hello is embstr, and the internal code of the value corresponding to the key mylist is ziplist .

Redis designed like this has two advantages :

  • One: The internal coding can be improved without affecting the external data structure and commands. For example, the quicklist provided by Redis 3.2 combines the advantages of both ziplist and linkedlist, and provides a more efficient internal coding implementation for list types.
  • Second: Different internal codes can give play to their advantages in different scenarios. For example, ziplist saves memory, but when there are more list elements, the performance will decrease. At this time, Redis will convert the internal implementation of the list type to linkedlist according to the configuration.

Single threaded architecture

Redis using the single-threaded architecture and I / O multiplexing model to achieve high performance in-memory database service. Then why the single thread can be so fast, the following analysis reasons:

Pure memory access

Redis puts all data in memory, and the response time of the memory is about 100 nanoseconds. This is an important basis for Redis to achieve 10,000-level access per second.

Non-blocking I/O

Redis uses epoll as the implementation of I/O multiplexing technology , and Redis's own event processing model converts connections, reads and writes, and closes in epoll into events, so that there is no need to waste too much on network I/O Time, as shown in the figure:

Insert picture description here

Single thread avoids the consumption of thread switching and race conditions

Single-threaded problem: There is a requirement for the execution time of each command. If a command is executed too long, it will cause other commands to block, which is fatal for a high-performance service like Redis, so Redis is a database for fast execution scenarios.

Using single thread to achieve such high performance is a good choice, because single thread can bring several benefits:

  • Single thread can simplify the implementation of data structures and algorithms, and developers do not need to understand complex concurrent data structures.
  • Single thread avoids the consumption of thread switching and race conditions. For server-side development, locks and thread switching are usually performance killers.

This article references

"Redis Development and Operation and Maintenance"

Summary of this article

This article introduces the Redis data structure and global commands in detail. Later, we will introduce the five common data structures in detail, as well as the common reference scenarios for various data structures.