Analysis of redis single thread

Why is redis so fast? There are three conclusions, as everyone knows, here is mainly analysis.

First point

redis is memory accessed, so fast

Of course everyone knows this, so it ’s not the point


 

io intensive and cpu intensive

Generally, we divide tasks into io-intensive and cpu-intensive

 

io intensive

  • IO-intensive means that the CPU performance of the system is much better than the hard disk and memory. At this time, the system is operating. Most of the conditions are that the CPU is waiting for I / O (hard disk / memory) read / write operations. not tall.
  • For io intensive tasks, its main time is on the disk io, and after the io itself issues an interrupt to inform the cpu, the cpu only needs to deal with it briefly, and then the DMA (see appendix) is responsible for data transmission. The utilization rate of CPU is very low. Therefore, we need to open more threads to make full use of the CPU. That is, the number of general threads = the number of CPU cores * 2 , such as the database connection pool

 

CPU-intensive

  • CPU-intensive is also called computing-intensive, which means that the system's hard disk and memory performance is much better than that of the CPU. At this time, most of the system's operation is CPU Loading 100%. ), I / O can be completed in a short time, and the CPU still has a lot of operations to process, CPU loading is very high.
  • For CPU-intensive tasks, it has a high CPU utilization, so there is no need to open more threads to improve CPU utilization. If you increase the number of threads, it will only cause frequent switching of threads, which will cause the CPU that is not enough to use even more. So generally the number of threads = cpu core number + 1

 


 

Where is the bottleneck of redis

Redis is basically doing memory io, is its bottleneck in io?

Redis uses epoll on the network io to implement a reactor model of io multiplexing. epoll is non-blocking io, so it avoids cpu blocking on io, so it is not io intensive , the bottleneck is not waiting for io to cause cpu utilization High, no need for multiple threads to shield the time waiting for io execution to complete. Of course, redis has a high io utilization rate, but high io utilization rate does not mean that it is io intensive, because its bottleneck is not waiting for io.

 

So the second point

Redis uses epoll to implement an io multiplexed reactor model on the network io, which makes CPU utilization higher and wastes less time on io

Redis does not need multi-threading to improve CPU utilization and reduce io waiting time, and the single-threaded architecture is also relatively easy to implement, so it is logical to adopt the single-threaded architecture.

You can read my article about epoll: https://www.cnblogs.com/fatmanhappycode/p/12362423.html

Third point

Due to the single-threaded architecture, the consumption caused by thread switching is avoided

Because a CPU context switch is about 1500ns.

It takes about 250us to read 1MB of continuous data from memory. Assuming that 1MB of data is read 1000 times by multiple threads, then there are 1000 time context switches,

So there is 1500ns * 1000 = 1500us, and I only read 250MB after reading 1MB of data in a single thread. You only used 1500us to switch the time context. I do n’t count the time you read a little data

 

So is redis cpu intensive? the answer is negative.

redis also not a cpu-intensive. In most cases, the CPU on the redis machine is sufficient.

 

The bottleneck of redis is the memory size and network bandwidth.

 

If you want to make full use of multi-core CPUs, you can use the method of multiple redis instances. At the same time, in order to reduce thread contention, you can bind the instance and CPU methods.

However, if CPU binding is done, the child process will share a CPU with the parent process in RDB and AOF. When the child process is rewritten, the single-core CPU usage rate is usually above 90%. The parent process and the child process will have fierce CPU competition, which greatly affects the stability of Redis. (The solution is unclear, and would it be better to bind a CPU?)

 


 

appendix

DMA

DMA transfer copies data from one address space to another address space. When the CPU initiates this transfer action, the transfer action itself is carried out and completed by the DMA controller.

A typical example is to move a block of external memory to a faster memory area inside the chip. For example, the memory is moved to the disk.

 

The final convention is attached with a picture:

 


Reference materials:

https://www.php.cn/redis/422123.html

https://blog.csdn.net/youanyyou/article/details/78990156

 

Guess you like

Origin www.cnblogs.com/fatmanhappycode/p/12708861.html