Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Redis binds CPU

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to bind Redis CPU, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Bind CPU

Redis 6.0supports binding CPU, which can effectively reduce thread context switching.

CPU affinity (CPU Affinity) is a scheduling property that "binds" a process or thread to a CPU or group of CPU. Also known as CPU binding.

Setting CPU affinity can avoid CPU context switching to some extent and improve the hit rate of CPU L1 and L2 Cache.

Under the early "SMP" architecture, each CPU shared resources through the BUS bus. CPU binding doesn't make much sense.

Under the current mainstream "NUMA" architecture, each CPU has its own local memory. Access to local memory is faster. Accessing other CPU memory results in significant latency. At this time, CPU binding is of great significance to improve the running speed of the system.

The real-world NUMA architecture is more complex than the image above, usually grouping CPU, and allocating a set of memory to several CPU, called "node".

You can view NUMA hardware information through the "numactl-H" command.

Numactl-Havailable: 2 nodes (0-1) node 0 cpus: 0 24 68 10 12 14 16 18 20 22 26 28 30 32 34 36 38node 0 size: 32143 MBnode 0 free: 26681 MBnode 1 cpus: 13 5 7 9 11 13 17 21 21 23 25 27 29 31 33 35 37 39node 1 size: 32309 MBnode 1 free: 24958 MBnode distances:node 0 10: 10 21 1: 21 10

In the figure above, you can see that the machine has 40 CPU, grouped into 2 node.

Node distances is a two-dimensional matrix that represents the "access distance" between node, with 10 as the base value. As you can see from the above command, node itself accesses a distance of 10. Cross-node access, such as node 0 to node 1, is 21.

In fact, as early as 2015, it was suggested that Redis needed to support setting CPU affinity, and Redis did not support IO multithreading at that time, and the proposal was shelved.

Redis 6.0introduces IO multithreading. At the same time, it also supports setting CPU affinity.

I drew a picture of the thread family of Redis 6.0for your reference.

The above picture can be divided into three modules.

Main thread and IO thread: responsible for command reading, parsing, and result return. Command execution is done by the main thread.

Bio thread: responsible for performing time-consuming asynchronous tasks, such as close fd.

Background process: the fork child process to execute time-consuming commands.

Redis supports configuring the CPU affinity of the above modules separately. You can find the following configuration at redis.conf (this configuration needs to be turned on manually).

# IO thread (including main thread) bound to CPU 0, 2, 4, 6server_cpulist 0-7 bio thread bound to CPU 1, 3bio_cpulist 1 management aof rewrite background process to CPU 8, 9, 10, 11aof_rewrite_cpulist 8-1 "bgsave background process bound to CPU 1, 10, 11bgsave_cpulist 1 10-11

I tested the IO thread and the main thread on the above machine as follows:

First, turn on the IO thread configuration.

Io-threads 4 # main thread + 3 IO threads io-threads-do-reads yes # IO thread enables reading and parsing commands

Test the following three scenarios:

CPU binding configuration is not enabled.

Bind to a different node.

"server_cpulist 0pm 1pm 2pm 3"

Bind to the same node.

"server_cpulist 0pic 2pr 4pr 6"

Benchmark the get command through redis-benchmark, executing 3 times for each scenario.

$redis-benchmark-n 5000000-c 50-t get-- threads 4

The results are as follows:

1. Do not enable CPU binding configuration

Throughput summary: 248818.11 requests per secondthroughput summary: 248694.36 requests per secondthroughput summary: 249004.00 requests per second

two。 Bind different node

Throughput summary: 248880.03 requests per secondthroughput summary: 248447.20 requests per secondthroughput summary: 248818.11 requests per second

3. Bind the same node

Throughput summary: 284414.09 requests per secondthroughput summary: 284333.25 requests per secondthroughput summary: 265252.00 requests per second

According to the test results, binding to the same node,qps increases by about 15%.

With binding CPU, you need to pay attention to the following points:

Under Linux, you can use "numactl-- hardware" to view the hardware layout and make sure that NUMA is supported and enabled.

Threads should be distributed in "different CPU, same node" as much as possible, and setting CPU affinity is effective. Otherwise, it will result in frequent context switching and remote memory access.

You should be familiar with the CPU architecture and do a good job of testing. Otherwise, it may be counterproductive, resulting in a decline in Redis performance.

Thank you for reading this article carefully. I hope the article "how to bind Redis to CPU" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report