0%

Redis机器产线优化

Redis部署机器最佳系统优化实践执行内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vi /etc/sysctl.conf
vm.swappiness = 0
vm.overcommit_memory = 1
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 262144

# 立即生效
sysctl -p
# 查看
cat /proc/sys/vm/overcommit_memory

vi /etc/rc.local
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
fi

echo never > /sys/kernel/mm/transparent_hugepage/enabled

禁用SWAP

Redis官方建议配置和内存大小相同的SWAP,不设置SWAP在内存超过一半时,如果调用redis重写AOF或者写入RDB时,可能导致OOM,但是SWAP会导致Redis影响变慢.个人建议如果内存充足,完全可以关闭SWAP,如果单台server只部署一个redis实例,为了充分利用内存,则开启SWAP。

1
2
3
# 默认值为60
vm.swappiness = 0
echo 0 >/proc/sys/vm/swappiness

存储分配策略

The default value is 0. By default, the kernel performs heuristic memory overcommit handling by estimating the amount of memory available and failing requests that are too large. However, since memory is allocated using a heuristic rather than a precise algorithm, overloading memory is possible with this setting.

When this parameter is set to 1, the kernel performs no memory overcommit handling. This increases the possibility of memory overload, but improves performance for memory-intensive tasks.

When this parameter is set to 2, the kernel denies requests for memory equal to or larger than the sum of total available swap space and the percentage of physical RAM specified in overcommit_ratio. This reduces the risk of overcommitting memory, but is recommended only for systems with swap areas larger than their physical memory.

  • 0:表示内核将检查是否有足够的可用内存供应用进程使用;如果有申请通过,否则申请失败,并把错误返回给进程。
  • 1:表示可以申请实际物理内存大小,而不关注内存状态。
  • 2:表示可以申请实际内存+交换内存大小的内存。

redis 选用1是为了提高性能,因为不在执行内存过量检测处理。

1
2
3
vi /etc/sysctl.conf
# 默认值0
vm.overcommit_memory = 1

端口最大的监听队列的长度

1
2
3
4
5
6
7
# linux 默认值为128
net.core.somaxconn = 65535
# 网络接口介绍的数据包的速率比内核处理的快时,允许入队的数据包最大条目,默认300
net.core.netdev_max_backlog = 262144
# 动态修改
echo 65535 >/proc/sys/net/core/somaxconn
echo 262144 >/proc/sys/net/core/netdev_max_backlog

文件句柄数调整

Redis 对文件访问并不多,个人建议不需要修改,官方也没有相关推荐。

1
2
3
4
5
6
7
8
# 系统文件句柄数,centos7 默认值794026
vi /etc/sysctl.conf
fs.file-max = 5000000

# 用户句柄数调整
vi /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

透明大页

1
2
3
4
5
vi /etc/grub.conf
transparent_hugepage = never

# 临时修改
echo never > /sys/kernel/mm/transparent_hugepage/enabled