Improving Redis Performance through Multi-Thread Processing

Single Process and Single Thread Model


  1. Due to restrictions of the single-process and single-thread model, time-consuming operations (such as dict rehash and expired key deletion) are broken into multiple steps and executed one by one in Redis implementation. This prevents execution of an operation for a long time and therefore avoids long time blocking of the system by an operation. The single-process and single-thread code is easy to compile, which reduces the context switching and lock seizure caused by multi-process and multi-thread.


  1. Only one CPU core can be used, and the multi-core advantages cannot be utilized.
  2. For heavy I/O applications, a large amount of CPU capacity is consumed by the network I/O operations. Applications that use Redis as cache are often heavy I/O applications. These applications basically have a high QPS, use relatively simple commands (such as get, set, and incr), but are RT sensitive. They often have a high bandwidth usage, which may even reach hundreds of megabits. Thanks to popularization of the 10-GB and 25-GB network adapters, the network bandwidth is no longer a bottleneck. Therefore, what we need to think about is how to utilize the advantages of multi-core and performance of the network adapter.

Multi-Thread Model and Implementation

Thread Model

  1. Main thread
  2. I/O thread
  3. Worker thread
  1. Main thread: Receives connections, creates clients, and forwards connections to the I/O thread.
  2. I/O thread: Processes the connection read/write events, parses commands, forwards the complete parsed commands to the worker thread for processing, sends the response packets, and deletes connections.
  3. Worker thread: Processes commands, generates the client response packets, and executes the timer events.
  4. The main thread, I/O thread, and worker thread are driven by events separately.
  5. Threads exchange data through the lock-free queue and send notifications through tunnels.

Benefits of Multi Thread Model

Increased Read/Write Performance

Increased Master/Slave Synchronization Speed

Subsequent Tasks

Setting of the Number of I/O threads

  1. Test results indicate that the number of I/O threads should not exceed 6. Otherwise, the worker thread will become a bottleneck for simple operations.
  2. Upon startup of a process, the number of I/O threads must be set. When the process is running, the number of I/O threads cannot be modified. Based on the current connection allocation policy, modification of the number of I/O threads involves re-allocation of connections, which is quite complex.


  1. With popularization of the 10-GB and 25-GB network adapters, how to fully utilize the hardware performance must be carefully considered. We can use technologies, such as multiple threads for networkI/O and the kernel bypass user-mode protocol stack.
  2. The I/O thread can be used to implement blocking-free data migration. The I/O thread encodes the data process or forwards commands, whereas the target node decodes data or executes commands.




Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using Blatann on macOS Catalina

AWS EC2: Exploring Instance Types

Implementing Agile Processes Within Human Factors Research

Easy log forwarding on K8S using filebeat CRD’s

Structured concurrency

Learning Code Through Listening

Shaping software development teams

CS371p Spring 2022: Sarunas Budreckis — Entry #11

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:

More from Medium

Nested Virtualisation in GCP… for your Android Emulator!

Error handling with Apache Beam, Asgarde with Kotlin

Understanding Gradle and Build Automation Tools

Outgrowing Postgres? Keep using Postgres!