Home >Database >Redis >This article will take you to quickly understand the thread IO model in Redis

This article will take you to quickly understand the thread IO model in Redis

青灯夜游
青灯夜游forward
2021-12-21 10:19:322517browse

Redis is single-threaded, but why is it so fast? One reason is that redis uses non-blocking IO and multiplexing to handle a large number of client connections. The following article will take you to understand the thread IO model in Redis. I hope it will be helpful to you!

This article will take you to quickly understand the thread IO model in Redis

Redis is a single-threaded application. NodeJs and Nginx are both single-threaded. They are all models of high-performance servers. [Related recommendations: Redis Video Tutorial]

The reason why Redis is single-threaded and so fast:

Firstly, because all its data is in memory All operations are memory-level operations, so when using redis, pay attention to instructions with a time complexity of O(n). Because it is single-threaded, if the amount of data is too large, other instructions will be blocked and wait;

The second reason is that redis uses non-blocking IO and multiplexing to handle a large number of client connections.

Non-blocking IO

When we use the read and write methods of the socket, the default is blocking,

That is, calling the read method to pass a parameter n, indicating the maximum number of reads Return after taking n bytes. If there is not a byte, the thread will continue to wait in the read method until data comes or the connection is closed. The read method returns at this time and the thread can execute the following logic,

The write method generally does not block. Unless the write buffer allocated by the kernel for the socket is full, the write method will block until there is free space in the buffer.

The following figure is the detailed process of socket reading and writing.

This article will take you to quickly understand the thread IO model in Redis

Non-blocking IO provides an option Non_Blocking when using sockets. When this option is turned on, the read and write methods will not block, but can read as much as they can. , write as much as you can,

How much you can read depends on the number of data bytes in the read buffer allocated by the kernel for the socket, and how much you can write depends on the data allocated by the kernel on the write buffer of the socket. Number of bytes,

The read and write methods will tell the program how many bytes have been read and written through the return value.

Non-blocking IO means that the thread no longer has to be blocked when reading and writing. Reading and writing can be completed instantly, and the thread can continue to do other things.

Multiplexing (event polling)

Although non-blocking IO is very fast, it also brings a problem. The thread reads the data and returns after reading part of it. It has not finished reading. When will the remaining data continue to be read? , writing data, the buffer is full and has not been written completely. When will the remaining data continue to be written?

When you can continue to read or continue to write, you should give the application a notification to tell the application that you can continue to read or continue to write. The event polling API is used to handle this problem.

select

The operating system provides a select function for the user program. The input is the read and write descriptor list read_fds & write_fds, and the output is The corresponding readable and writable events,

also provide the timeout parameter, the thread can wait for the timeout at most. During this period, if an event comes, the method will return immediately, and the thread will continue processing. If the timeout time is exceeded, The method will also return.

If the event is obtained, the thread can process the corresponding events one by one. After processing, it will continue to call the select api polling, so the thread is actually an infinite loop and keeps selecting. Non-stop processing, back and forth, this endless loop is called an event loop, and a loop is a cycle.

This article will take you to quickly understand the thread IO model in Redis

Event loop pseudocode:

while True
    read_events, write_events = select(read_fds, write_fds, timeout)
    for event in read_events:
        handle_read(event.fd)
    for event in write_events:
        handle_write(event.fd)
    handle_others() # 做其他的逻辑处理,处理定时任务等等

Through the select function we can handle the read and write events of multiple channel descriptors, so systems like select will The function call is called multiplexing API.

The multiplexing API of modern operating systems no longer uses the select system call, but uses epoll (linux) and kqueue (FreeBSD, macosx) instead.

The performance of select will become very poor when the number of descriptors increases. There are slight differences in the use of epoll and select, but they can be understood using the above pseudocode. When an event occurs on the descriptor, the descriptor will be looped. The event is processed. The read operation of the

serversocket object refers to calling accept to accept a new connection from the client. When a connection comes, it is also notified through the read event called by select.

The NIO technology in Java is event polling, and other languages ​​also have this technology.

Command Queue

Redis associates a command queue with each client socket, and the commands sent by the client are processed in first-in, first-out order through the queue.

Response Queue

Similarly, the results returned by Redis are also returned through a queue associated with each client. If the queue is empty, there is no need to obtain write events for the time being,

At this time, the client descriptor will be removed from write_fds, and when the queue has data, the descriptor will be put in. This can avoid finding that there is no data to write when the select system call returns the write event, causing empty space. Polling, useless polling, consumption of machine CPU.

Timing tasks

The server not only needs to respond to IO events, but also needs to handle some other things, such as the application's own timing tasks. If the thread blocks on the select call, waiting for the return of select, this will cause Some scheduled tasks have expired but have not been executed.

Redis’ scheduled tasks are recorded in a data structure called the minimum heap. In this heap, the fastest tasks to be executed are ranked at the top. Each cycle During the cycle, redis will process the tasks in the heap that have reached the time point.

After the processing is completed, the time required for the tasks to be executed in the heap will be recorded. When select is called again, this time will be The value of timeout means that no other tasks will need to be executed during this period. Redis can safely block for this long at most, and then perform corresponding processing after the time is up.

The event processing principles of NodeJs and Nginx are similar to Redis.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of This article will take you to quickly understand the thread IO model in Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete