Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of the underlying data structure of Redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of the underlying data structure of Redis, which is very detailed and has a certain reference value. Friends who are interested must finish it!

Global command

Redis has five data structures, and although they are different at the bottom, there are some common commands that are the same.

View all keys (return all keys and output its specific keys)

Keys *

View the total number of keys (returns the number of keys in the current database)

Dbsize

Note: the dbsize command does not traverse all keys when calculating the total number of keys, but directly gets the total number of keys variable built in Redis, so the time complexity of the dbsize command is O (1). The keys command traverses all keys, so its time complexity is O (n), so the keys command should be used with caution if there are a large number of keys in Redis.

Check to see if the key exists

Exists key

We see that the exists command has a return value. When the key exists, the return value is 1. Of course, when the key is not stored, the return value is 0.

Delete key

Del key

We know that there are five data structures in Redis, but the del command can directly delete any type of data structure without worrying about its underlying implementation.

We see that the del command, like the exists command, has a return value. The only difference is the number of keys that were successfully deleted when the del command returned. If 0 is returned, the key has not been successfully deleted, which means the key does not exist. If a number greater than 0 is returned, it means that multiple keys have been deleted. Let's take a look at the operation of deleting multiple keys.

Key expiration

Expire key seconds

Redis supports adding expiration time to keys, and when this expiration time is exceeded, Redis will automatically delete the key.

When we set the expiration time of the key through the expire command, we can use the

Ttl key

Command to see the remaining expiration time of the key, so the ttl command has a return value, that is, the remaining time of the key, in seconds. In addition, the ttl command has three types of return values. Let's take a look at the differences between these three return values.

> = 0: indicates the remaining expiration time

-1: the expiration time is not set by the key

-2: key does not exist

The data structure type of the key

Type key

If the key is a string, the type command returns a string, if it is another data type, it will return other data types (because we have not learned other types, we will only consider the string for the time being), if the key does not exist, it will return none.

Data structure and internal coding

We know that the type command returns the data type of the key in Redis, that is, string (string), hash (hash), list (list), set (collection), zset (ordered set), and so on. But these are only external data structures. In fact, different data structures in Redis have different underlying internal codes. Different internal encodings have different performance in Redis, and Redis automatically determines which encoding should be used to store data, which ensures the performance of Redis. We can view the internal code of the key through the following command.

Object encoding key

We can see that the internal code of the jilinwula key saved above is embstr. Let's take a look at the internal coding corresponding to all the data structures in Redis.

Let's analyze why Redis designs the data structure and underlying coding in this way. First of all, the first benefit is that the internal coding can be improved. When doing so, there is no need to change the internal data structure, and there is no need to modify the external structure and commands. The second benefit is that we know that different internal encodings have different performance. When designing in this way, if we want to change the underlying internal coding, we only need to change the underlying encoding of the key that we have stored according to the Redis configuration option, so that we can personalize the configuration for different business scenarios, and then improve performance.

Single thread architecture

We know that Redis uses a single-threaded architecture and an I-map O multiplexing model to implement high-performance memory services.

Single thread model

Whenever the client invokes a command, it goes through three steps: sending the command, executing the command, and returning the result. As we mentioned earlier, Redis is single-threaded, so when each command is sent from the client to the server, the command is not executed immediately, but all the commands are put into a queue and executed sequentially. In this way, when our client starts multiple commands, we don't have to think about concurrency, because they all enter the queue and execute sequentially.

Why single thread processing speed is so fast first, we know that Redis stores all the data in memory, and the processing speed of memory is much faster than that of pure hard disk IO.

Second, non-blocking I _ stop O _ Magi Redis is implemented by using epoll as the I _ paw O multiplexing technology. In addition, Redis's own event handling model converts the connection, read and write, and shutdown in epoll into events, and does not waste too much time on the network I _ Redis.

Single thread to avoid consumption such as thread switching

First, we know that when doing project development, if you want to be compatible with multithreading, then it is much more complex than single threading, the code becomes more complex, and there is more possibility of BUG.

Second, when developing multiple threads, we know that switching between threads is very resource-consuming, and it takes a lot of time for the server to add a lock to the thread and judge the lock every time it is executed.

Although Redis's single-threaded architecture has advantages, it is also its drawback. We know that commands are executed sequentially, and if the last command is not finished, then the remaining commands will not be executed, which leads to command blocking. For an in-memory database like Redis, if there is a blockage, you can imagine the consequences, so we must be careful when executing the relevant commands.

The above is all the contents of the article "sample Analysis of the underlying data structures of Redis". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report