Overview of Redis data structure and global commands

This article talks about Redis data structure and global commands

Article Directory


Overview

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 .


Pre-knowledge

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 :

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> set java java8
OK
127.0.0.1:6379> set python python3
OK

The command will output all the keys :

127.0.0.1:6379> keys *
1) "hello"
2) "python"
3) "java"
127.0.0.1:6379>

Total number of keys

dbsize

Insert a key-value pair of the list type below (the value is composed of multiple elements) :

127.0.0.1:6379> rpush mylist a b c d e f g
(integer) 7

The dbsize command returns the total number of keys in the current database .

127.0.0.1:6379> 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 :

127.0.0.1:6379> exists java
(integer) 1
127.0.0.1:6379> exists go
(integer) 0
127.0.0.1:6379>

Delete key

del key

del is a general command, no matter what data structure type the value is, the del command can delete it .

127.0.0.1:6379> del java
(integer) 1
127.0.0.1:6379> exists java
(integer) 0
127.0.0.1:6379> del go
(integer) 0
127.0.0.1:6379> exists go
(integer) 0
127.0.0.1:6379>

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 :

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> 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秒
127.0.0.1:6379> ttl hello
(integer) 6
# 还剩1秒
127.0.0.1:6379> ttl hello
(integer) 1
# 返回结果为-2,说明键hello已经被删除
127.0.0.1:6379> ttl hello
(integer) -2
127.0.0.1:6379> set hello word
OK
# 返回结果为-1,说明键hello没有设置过期时间
127.0.0.1:6379> ttl hello
(integer) -1
127.0.0.1:6379>

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 .

127.0.0.1:6379> set aa bb
OK
127.0.0.1:6379> type aa
string
127.0.0.1:6379> rpush mylist a b c d e f g
(integer) 7
127.0.0.1:6379> type mylist
list


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:

127.0.0.1:6379> set 11 22
OK
127.0.0.1:6379> object encoding 11
"int"
127.0.0.1:6379> set aa bb
OK
127.0.0.1:6379> object encoding aa
"embstr"
127.0.0.1:6379> set long 6666666666666666666666666666666666666666666666666666666666666666
OK
127.0.0.1:6379> object encoding long
"raw"
127.0.0.1:6379> object encoding mylist
"ziplist"
127.0.0.1:6379>

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.