This is my first article on getting started

In daily use, interview process, we are often asked: Redis single-threaded? Most of the answers on the web so far are pretty positive: Redis is single-threaded. But is this really the case? In fact, Redis added multithreaded operations as early as version 4. X, and in version 6. X, it officially supports multithreading. So, to the question of whether Redis is single-threaded, we have to answer this:

  • Redis 3.x version is single threaded;
  • Redis 4.x has partial support for multithreading;
  • Since Redis 6.x, it’s multithreaded.

So, why did Redis start using multithreading after 6.x? In other words, from the original single thread to the current multi-thread, Redis update, is to solve what problem?

Factors affecting Redis

It’s not very frequent that CPU becomes your bottleneck with Redis, as usually Redis is either memory or network bound. For instance, using pipelining Redis running on an average Linux system can deliver even 1 million requests per second, So if your application mainly uses O(N) or O(log(N)) commands, it is hardly going to use too much CPU

According to the official Redis FAQ, the only things that affect Redis performance are memory size and network congestion. CPU quality is not a bottleneck for Redis performance. Therefore, increasing memory size or reducing network congestion become two directions to improve Redis performance. Memory can be bought directly, so how to improve the speed of network congestion, has become the most critical factor in Redis performance improvement. This is the first premise and one of the most important factors in Redis becoming multithreaded.

The downside of single threading

In phase 3. X, due to the FEATURES of IO multiplexing, memory storage and simple data structure, The performance of Redis has been able to match most KV databases on the market. However, no matter how optimized, a single-threaded task is still a process that requires the previous task to complete before the later task can be executed. Hence the situation:

If a large amount of data is stored in the value, will deleting the key occupy time and cause subsequent tasks to wait?

The answer is yes. So the downside of single threading is obvious: when a user executes a time-consuming command, subsequent commands need to wait until the command is executed.

Redis 4.x

Redis 4.x added an asynchronous deletion directive unlink: elements cannot be accessed synchronously by other threads, and then give the big value to the asynchronous thread to recycle. However, 4.x does not fully support multithreading, like an enhanced version to address single threading

Redis 6.x

After the release of Redis 6.x, Redis announced that it officially supports multithreading. But note:

Redis worker threads are single-threaded, and for Redis as a whole, multi-threaded

How do you understand that? In fact, 6.x multithreading is to solve the network congestion caused by performance degradation. It splits the time consuming read and write tasks of I/O into a set of separate threads. The remaining commands are still executed sequentially by the main thread and interact with the data in memory.

conclusion

  • Redis 3.x is single threaded and has large key deletion issues
  • Redis 4.x supports some multithreading, mainly to solve the large key operation of 3.x.
  • The Redis 6.x version is multithreaded. The goal is to reduce the time consumption of network IO