In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail what is the threading IO model in Redis. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Redis is a single-threaded application. NodeJs and Nginx are both single-threaded. They all belong to the model of high performance of the server.
The reason why Redis is single-threaded and can be so fast is:
One is because all its data is in memory and all its operations are memory-level operations, so when using redis, you should 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 waited.
The second is because redis uses non-blocking IO to handle a large number of client connections by multiplexing.
Non-blocking IO
When we use the read-write method of sockets, it is blocked by default
That is, the call to the read method passes a parameter n, which means that a maximum of n bytes are read and returned. If there is not a single byte, the thread will wait here in the read method until data comes or the connection is closed and the read method returns. The thread can only execute the following logic.
The write method generally does not block, and the write method does not block until there is free space in the cache unless the write buffer allocated by the kernel to the socket is full.
The following figure shows the detailed process of socket reading and writing.
Non-blocking IO provides an option, Non_Blocking, when using sockets. When this option is turned on, the read and write method does not block, but reads as much as it can, and writes as much as it can.
How much you can read depends on the number of bytes of data that the kernel allocates for the socket read buffer, and how much you can write depends on the number of bytes the kernel allocates to the socket write buffer.
Both read and write methods tell the program how many bytes have been read and written through the return value.
Non-blocking IO means that when reading and writing, the thread does not have to be blocked, the read and write can be done instantly, and the thread can continue to do something else.
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. When will the rest of the data continue to be read? Write data, the buffer is full, not finished, when will the rest of the data continue to be written?
When you can continue reading or writing, you should give the application a notification that it can continue reading or writing, and event polling API is used to deal with this problem.
Select
The operating system provides a select function to the user program. The input is the list of read and write descriptors read_fds & write_fds, and the output is the corresponding readable and writable event.
At the same time, it also provides the timeout parameter, the maximum time for a thread to wait for timeout, during which an event comes, the method returns immediately, and the thread processes it down. if the timeout time is exceeded, the method will also return.
If you get the event, the thread can process the corresponding event one by one, and then continue to call select api polling, so the thread is actually an endless loop, constantly select, constantly processing, back and forth, this dead cycle is called an event loop, a cycle is a cycle.
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 () # do other logic processing, handle scheduled tasks, etc.
Through the select function, we can handle the read and write events of multiple channel descriptors, so system function calls such as select are called multiplexing API.
The multiplexing API of modern operating system no longer uses select system call, but uses epoll (linux) and kqueue (FreeBSD, macosx).
The performance of select will become very poor when there are more descriptors. Epoll and select are slightly different to use, but both can be understood by the pseudo code above. When the descriptor event occurs, the loop handles the descriptor event.
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 by the read event called by select.
The NIO technology in Java is event polling, which is also available in other languages.
Instruction queue
Redis associates an instruction queue for each client socket, and the instructions sent by the client are processed sequentially through the queue.
Response queue
Similarly, the result returned by Redis is 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 point, the client descriptor will be removed from the write_fds, and then put in when there is data in the queue, so as to avoid finding that there is no data to write when the select system call returns the write event, resulting in empty polling, useless polling, and consumption of machine CPU.
Scheduled task
The server not only has to respond to IO events, but also needs to deal with other things, such as the scheduled tasks of the application itself. If the thread blocks on the select call, waiting for the select to return, this will cause some scheduled tasks to expire but not to be executed.
Redis's scheduled tasks are recorded in a data structure called the minimum heap, in which the fastest tasks are at the top, and in each cycle, redis processes the tasks in the heap that have reached a point in time.
After processing, record the time required for the task to be executed in the heap. When select is called again, this time is the value of timeout. During this period, there will be no other tasks to execute. Redis can rest assured that it can block for so long at most, and then deal with it after the time.
The event handling principles of NodeJs and Nginx are similar to those of Redis.
This is the end of this article on "what is the thread IO model in Redis". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.