Redis is single-threaded, but why is it so fast?

Mainly due to the following reasons:

  1. All Redis operations are basically completed in memory . Memory operations are much faster than disk operations.

  2. Redis uses many efficient data structures . Hash tables, skip tables, etc. are required.

  3. Redis uses a multiplexing mechanism , which can use a single thread to handle a large number of client requests.

What is multiplexing?

        IO multiplexing refers to one thread processing multiple IO streams, which is the select() and epoll() mechanisms in Linux, which can achieve the effect of one Redis thread processing multiple IO streams.

As shown in the picture:

illustrate:

        There is a thread that constantly monitors different events. For example, it determines whether it wants to connect, read, or write events. After identifying the event, it puts the event into the event queue, and then hands it to different processing functions for processing. Redis only needs to obtain the event queue and call the corresponding processing function, so it can quickly respond to multiple clients and achieve high concurrency.

How does the event find the corresponding handler function?

        This part is actually completed by Redis in conjunction with the Linux callback mechanism. Once the select and epoll mechanisms detect the corresponding socket, the corresponding event will be triggered whether the request arrives.

Guess you like

Origin blog.csdn.net/m0_70314224/article/details/126482851