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

What is the design and implementation of Redis?

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the design and implementation method of Redis". In daily operation, I believe that many people have doubts about the design and implementation of Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the design and implementation method of Redis?" Next, please follow the editor to study!

Design and implementation of Redis

In fact, Redis mainly meets the performance requirements of such efficient throughput through three aspects.

Efficient data structure

Multiplexing IO model

Event mechanism

1. Efficient data structure Redis supports several efficient data structures such as string (string), hash (hash), list (list), set (set), zset (ordered set). Their underlying coding methods are all optimized in different ways, so it is not the focus of this article. 2. The multiplexing IO model assumes that 10,000 long connections have been established with the Redis server at a certain time. For blocking IO, one thread is established for each connection, then 10,000 threads are needed. At the same time, according to our experience, we generally set threads = 2 * CPU + 1 for IO-intensive operations, and thread = CPU + 1 for CPU-intensive operations. Of course, various books or the Internet also have a detailed formula to calculate the more appropriate and accurate number of threads, but the result is often a relatively small value, such as blocking IO this also creates thousands of threads, the system is unable to carry such a load, even less efficient throughput and services. The practice of multiplexing the IO model is to use a thread to put these 10, 000 successfully established links into the event_poll,event_poll one after another. When a persistent connection is ready (successfully established, data read, etc.), it will be written to the event_poll 's ready queue rdlist through the callback function. In this way, the single thread can get the required data by reading the rdlist. It should be noted that with the exception of asynchronous IO, all the other Imax O models can actually be classified as blocking Imax O model, except that when the data is read in the first stage, if the data is not ready and needs to be blocked, the step that needs to copy the data from kernel state to user state after the second stage data is ready is also blocked. The multiplexing IO model does not block in the first phase, but only in the second phase.

In this way, one or more threads can be used to handle a large number of connections, which greatly improves the throughput. 3. Event mechanism

The Redis client connects with the Redis server, sends commands, and the Redis server responds to commands through the event mechanism, as shown in the following figure

First, the redis server is running, and the AE_READABLE event of the listening socket is in the listening state, and the connection reply processor is working at this time.

The client initiates to establish a connection with the Redis server, and the listening socket generates AE_READABLE events. When the IO multiplexer listens that it is ready, the event is pressed into the queue, and the events in the queue are obtained by the file event dispatcher. The events in the queue are handed over to the connection response processor, and the answering client establishes the connection successfully. At the same time, the AE_READABLE events of the client socket are pressed into the queue by the file event dispatcher to obtain the event delivery command request processor association in the queue.

The client sends a set key value request, and the AE_READABLE event of the client socket. When the IO multiplexer listens that it is ready, the event is pressed into the queue, and the events in the queue are obtained by the file event dispatcher and handed over to the command request processor association processing.

After the command request processor association processing is completed, the response client operation needs to be completed. At this time, the AE_WRITEABLE event generating socket will be pressed into the queue, and the file event dispatcher will obtain the event in the queue and hand it over to the command recovery processor for processing. The operation result will be returned. After completion, the AE_WRITEABLE event will be disassociated with the command recovery processor.

Reactor mode

Generally speaking, the working mode of Redis is that reactor mode cooperates with a queue, uses a serverAccept thread to process the link to establish the request, and lets the kernel listen to these socket through the IO multiplexing model. Once some socket read and write events are ready, the corresponding events are pressed into the queue, and then the worker works, and the file event dispatcher obtains the events from them and hands them to the corresponding processor for execution. When the execution of an event is complete, the file event dispatcher gets the next event from the queue for processing. For example, in netty, we generally set the bossGroup of bossGroup and workerGroup to 1 worker Group = 2 * cpu by default, so that multiple threads can handle read-write-ready events, but there cannot be any time-consuming operations that need to be put into the thread pool if any, otherwise their throughput will be reduced. In Redis, we can think of the value of both as 1.

Why should the stored value not be too large?

For example, a string key = a stores 500MB, and the first read event is pushed into the queue. After the file event dispatcher obtains it, it is handed over to the command request processor for processing. Here, it involves loading 500MB from disk. For example, an ordinary SSD hard disk with a reading speed of 200MB/S requires a reading time of 2.5s. Reading data in memory is faster, such as 50G/ seconds in DDR4, and it takes about 100ms to read 500MB. Generally speaking, a thread's library defaults to 10 milliseconds as a slow query, and most instructions are executed in microseconds. At this time, all other socket requests will be in the waiting process, which will cause blocking of 100ms. At the same time, it will take up a large amount of bandwidth and further reduce the throughput. At this point, the study on "what is the design and implementation of Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report