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

How to design and implement Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to design and implement Redis". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to design and implement Redis.

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

Several efficient data structures supported by Redis: string (string), hash (hash), list (list), set (collection), zset (ordered set)

The underlying coding methods of the above exposed data structures are all optimized in different ways, so it is not the focus of this article.

2. Multiplexing IO model

Suppose 10,000 long connections are 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. According to our experience, we generally set thread number = 2 * CPU + 1 for IO-intensive operations, and thread = CPU number + 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. Why Redis is single-threaded, click here to read this article.

In addition, you can follow Wechat official account: Java technology stack, reply: redis in the background, you can get my N Redis tutorials, all of which are practical information.

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, greatly increasing 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, I believe you have a deeper understanding of "how to design and implement Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report