Is Redis single threaded? and why single-threaded (Redis' network model)

Is Redis single-threaded or multi-threaded?

  • If you only talk about the core business part of Redis (command processing), it is of course single-threaded.
  • If you are talking about the entire Redis, then the answer is multithreading.

During the iterative process of Redis version, multithreading support was introduced at two important time nodes:

  • Redis v4.0: Introduce multi-threaded asynchronous processing of some time-consuming tasks, such as asynchronous deletion command unlink
  • Redis v6.0: Introduce multi-threading in the core network model to further improve the utilization of multi-core CPUs

Therefore, for Redis's core network model, it is indeed single-threaded before Redis 6.0. It is to use IO multiplexing technology such as epoll (Linux system) to continuously process the client situation in the event loop.

Why Redis chooses to use single thread

  • Aside from persistence, Redis is a pure memory operation, and its execution speed is very fast. ItsPerformance bottleneck is network latency not execution speed, so multithreading does not bring huge performance improvements;
  • Multi-threading will lead to excessive context switching, bringing unnecessary overhead;
  • The introduction of multi-threading will face thread safety issues, and it is necessary to introduce security measures such as thread locks, which will increase the complexity of implementation, and the performance will be greatly reduced.

Single-threaded model of Redis

When our client wants to connect to our server, it will first go to the IO multiplexing model to queue, and there will be a connection response processor, which will accept the read request, and then register the read request to the specific model At this time, for these established connections, if the client requests the processor to execute the command, he will read the data, and then put the data into the client, and the clinet will analyze the current command and convert it into The commands that redis recognizes, and then start to process these commands, find these commands from the command in redis, and then actually operate the corresponding data. When the data operation is completed, it will find the command reply processor, and then by He writes the data out.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_45970271/article/details/126163693