In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of the IO thread model, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
So if it's single-threaded, why is it so fast?
Redis data is in memory, all operations are memory level, processing data is very fast, so it is important to note that some instructions with complexity of O (n) may cause server stutters.
So how does Redis handle concurrent client connections when it is a single thread?
This is what we'll talk about next: non-blocking IO, multiplexing, and event polling API.
Non-blocking IO
So what is the blocking IO model? That is, blocking occurs in the process of reading and writing data.
When the user thread makes an IO request, the kernel checks to see if the data is ready, and if not, it waits for the data to be ready, while the user thread is blocked and the user thread hands over the CPU. When the data is ready, the kernel copies the data to the user thread and returns the result to the user thread before the user thread releases the block state.
# blocking the IO model. If the data is not ready, it will always block the read method. Data = socket.read ()
Non-blocking IO
When the user thread initiates a read operation, it does not need to wait, but immediately gets a result, and the next line of code is executed immediately, whether you send it in or not. If the result is an error, it knows that the data is not ready, so it can send the read operation again. Once the data in the kernel is ready and again receives a request from the user thread, it immediately copies the data to the user thread and returns.
The problem with non-blocking IO is that the thread reads the data and returns after reading part of it, so how does the thread know when to continue reading, that is, how the thread is notified when the data arrives. The same is true for writing. if the buffer is full and cannot be finished, the thread should be informed when the rest of the data should continue to be written. Then event polling API is to solve this problem. Multiplexing (event polling)
The simplest event polling API is the select function, which is the API that the operating system provides to the user program. The input is the list of read-write descriptors read_fds&write_fds, and the output is the corresponding readable and writable event. It also provides a timeout parameter that waits for the value of timeout at most if no event arrives, and the thread is in a blocking state. As soon as any event arrives during the period, you can return immediately. After time has passed, there is still no event to come, and the Lord will return.
Because we handle the read and write events to be described by multiple channels at the same time through select system calls, we call this type of system calls multiplexing API. Multiplexing API in modern operating systems no longer uses select system calls, but instead uses epoll (linux) and kqueue (FreeBSD) and (macosx), because the performance of select system calls becomes very poor when there are too many descriptors. They may be slightly different in form, but they are all similar in nature and can be understood using the pseudo-code logic above.
Instruction queue
Redis associates each client socket with an instruction queue. Client instructions are queued for sequential processing on a first-come-first-served basis.
Response queue
Redis also associates a response queue for each client socket. The Redis server returns the return result of the instruction to the client by responding to the queue.
If the queue is empty, it means that the connection is temporarily idle and there is no need to get write events, that is, the current client descriptor can be removed from the write_fds. Wait until the queue has data, and then put the descriptor in it to prevent the select system call from returning a write event immediately. As a result, it is found that there is no data to write, and the thread that occurs this situation will cause CPU consumption to rise.
Scheduled task
The server has other things to do in addition to responding to the IO event. For example, scheduled tasks are very important. If the thread blocks on the select system call, the timed task will not be scheduled on time. So how does Redis solve this problem?
The scheduled tasks of Redis are recorded in a data structure called "minimum heap". In this heap
The fastest tasks are at the top of the heap. In each cycle, Redis processes tasks in the smallest heap that have reached a point in time. After processing, record the time required for the fastest task to be executed, which is the timeout parameter of the select system call. Because Redis knows the value of timeout in the future, there are no other scheduled tasks to deal with, so you can rest assured that you can sleep with the value of timeout.
The event handling principles of Nginx and Node are similar to those of Redis.
The above is all the content of the article "sample Analysis of the IO Thread Model of Redis". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.
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.