找回密码
 立即注册
首页 业界区 业界 Redis 调优:必须关注的几个参数

Redis 调优:必须关注的几个参数

志灿隐 2026-1-19 13:35:00
背景

在一台未经过任何调优的 Linux 服务器上部署 Redis,在 Redis 启动过程中,可能会碰到以下警告信息。
  1. 1363410:M 15 Jan 2026 13:07:34.879 # WARNING: The TCP backlog setting of 512 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  2. 1363410:M 15 Jan 2026 13:07:34.879 # Server initialized
  3. 1363410:M 15 Jan 2026 13:07:34.879 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
  4. 1363410:M 15 Jan 2026 13:07:34.879 # WARNING You have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
复制代码
这些警告信息实际上是在提醒我们,操作系统的某些参数设置得不合理,需要调整,否则会影响 Redis 的性能和稳定性。
除此之外,Redis 还提供了多个参数,用于在进程或连接级别进行内核参数优化,例如:

  • tcp-backlog:设置 TCP 服务器中listen()的 backlog 参数。
  • disable-thp:在进程级别关闭透明大页(THP)。
  • tcp-keepalive:在连接级别设置 TCP Keepalive 参数。
  • server_cpulist、bio_cpulist:可将 Redis 进程或后台 I/O 线程绑定到指定 CPU。
  • oom-score-adjoom-score-adj-values:调整进程的 oom_score。oom_score 是 Linux 内核为每个进程计算的一个整数值(位于/proc/[pid]/oom_score),分数越高,进程在内存不足时越容易被 OOM Killer 杀死。
下面,我们看看这些参数的实现细节和设置建议。
tcp-backlog
  1. createIntConfig("tcp-backlog", NULL, IMMUTABLE_CONFIG, 0, INT_MAX, server.tcp_backlog, 511, INTEGER_CONFIG, NULL, NULL), /* TCP listen backlog. */
复制代码
tcp-backlog 用于设置 TCP 服务器在调用listen()时使用的 backlog 参数。该参数用于指定已完成三次握手、但尚未被accept()处理的连接队列长度。
下面是一个典型的 TCP 服务器流程:
  1. // 1. 创建 socket
  2. int fd = socket(AF_INET, SOCK_STREAM, 0); 
  3. // 2. 绑定 IP + 端口
  4. bind(fd, ...);   
  5. // 3. 开始监听
  6. listen(fd, backlog);       
  7. // 4. 接受连接
  8. int connfd = accept(fd, ...);              
复制代码
当客户端发起连接时:

  • TCP 三次握手完成后,连接会被放入已完成连接队列(accept queue),其长度由listen(backlog)控制。
  • 服务器通过accept()从队列中取走连接,交给应用层处理。
  • 当已完成连接队列已满时,新完成三次握手的连接无法进入 accept queue,内核通常会丢弃或延迟 ACK,导致客户端出现连接超时、重传。
tcp-backlog 的默认值为 511,对应的内存变量为 server.tcp_backlog。
  1. static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len, int backlog) {
  2.     // 将 socket 绑定到指定的 IP 和端口
  3.     if (bind(s,sa,len) == -1) {
  4.         anetSetError(err, "bind: %s", strerror(errno));
  5.         close(s);
  6.         return ANET_ERR;
  7.     }
  8.     // 将 socket 设置为监听状态,开始接收客户端连接,backlog 指定内核允许排队的最大未处理连接数
  9.     if (listen(s, backlog) == -1) {
  10.         anetSetError(err, "listen: %s", strerror(errno));
  11.         close(s);
  12.         return ANET_ERR;
  13.     }
  14.     return ANET_OK;
  15. }
复制代码
实例在启动时,会调用checkTcpBacklogSettings()检查 server.tcp_backlog 的配置是否合理。
  1. void checkTcpBacklogSettings(void) {
  2. #if defined(HAVE_PROC_SOMAXCONN)
  3.     FILE *fp = fopen("/proc/sys/net/core/somaxconn","r");
  4.     char buf[1024];
  5.     if (!fp) return;
  6.     if (fgets(buf,sizeof(buf),fp) != NULL) {
  7.         int somaxconn = atoi(buf);
  8.         if (somaxconn > 0 && somaxconn < server.tcp_backlog) {
  9.             serverLog(LL_WARNING,"WARNING: The TCP backlog setting of %d cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of %d.", server.tcp_backlog, somaxconn);
  10.         }
  11.     }
  12.     fclose(fp);
  13. ...
  14. #endif
  15. }
复制代码
具体实现上,它会获取/proc/sys/net/core/somaxconn的值。
somaxconn 决定了 Linux 内核允许的单个 TCP socket 的最大 backlog 队列长度,也就是listen()的 backlog 参数在内核层面的上限。
如果 somaxconn 的值小于 tcp-backlog,Redis 会打印如下信息,此时,在listen()函数中实际生效的就是 somaxconn 的值。
  1. # WARNING: The TCP backlog setting of 512 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
复制代码
设置建议:对于普通的 Web 服务(几百并发),默认值(511)已经足够。如果是高并发服务(上万并发 TCP 连接),可将 backlog 调整为 4096 甚至更高。
disable-thp
  1. createBoolConfig("disable-thp", NULL, IMMUTABLE_CONFIG, server.disable_thp, 1, NULL, NULL),
复制代码
disable-thp 用来控制是否在进程级别关闭透明大页。默认值是 1,对应的内部变量是 server.disable_thp。
实例在启动时,会调用linuxMemoryWarnings判断 Linux 系统内存相关的配置。
[code]void linuxMemoryWarnings(void) {
    sds err_msg = NULL;
    if (checkOvercommit(&err_msg)

相关推荐

您需要登录后才可以回帖 登录 | 立即注册