这里谈一下Redis最大客户端连接数。redis中有个配置,是maxclients,这个配置是设置redis服务器同时能接收最大客户端连接数(包括普通客户端、Pub/Sub订阅者、Sentinel、主从复制连接等)。默认值是10000。
maxclients配置说明
################################### CLIENTS ####################################
# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# IMPORTANT: When Redis Cluster is used, the max number of connections is also
# shared with the cluster bus: every node in the cluster will use two
# connections, one incoming and another outgoing. It is important to size the
# limit accordingly in case of very large clusters.
#
# maxclients 10000
maxclients设置同时连接的最大客户端数量。默认情况下,此限制为10000个客户端。但如果Redis服务器无法将进程的文件描述符限制配置为允许达到指定的客户端数量,则实际允许的最大客户端数将被设为当前文件描述符限制减去32(因为Redis会预留少量文件描述符用于内部用途)。一旦达到该限制,Redis 将拒绝所有新连接,并返回错误信息:max number of clients reached(已达到最大客户端数量)。
重要提示:当使用Redis Cluster(集群)时,最大连接数也会被集群总线(cluster bus)共享:集群中的每个节点都会占用两个连接(一个用于入站,另一个用于出站)。因此,在集群规模非常大时,务必相应地调整此连接数限制。
我们将redis.conf设置为maxclients 64000,看一下实际redis启动日志:
postgres@slpc:~/redis6.0$ ./bin/redis-server redis-node1.conf
31011:C 09 Jan 2026 11:15:35.625 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31011:C 09 Jan 2026 11:15:35.625 # Redis version=6.0.20, bits=64, commit=de0d9632, modified=0, pid=31011, just started
31011:C 09 Jan 2026 11:15:35.625 # Configuration loaded
31011:M 09 Jan 2026 11:15:35.631 * Increased maximum number of open files to 64032 (it was originally set to 1024). # 这里显示redis启动时,已经将文件描述符限制设置为64032
我们查看一下其设置的文件描述符限制,查看到Max open files被设置为64032。
postgres@slpc:~/redis6.0$ ps -ef | grep redis
postgres 31011 28442 0 11:15 pts/1 00:00:02 ./bin/redis-server 0.0.0.0:6379
postgres 34536 34468 0 11:28 pts/3 00:00:00 grep --color=auto redis
postgres@slpc:~/redis6.0$ cat /proc/31011/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size unlimited unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 63461 63461 processes
Max open files 64032 64032 files
Max locked memory 2090074112 2090074112 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 63461 63461 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
查看当前连接数:
127.0.0.1:6379> info clients
# Clients
connected_clients:1 # 这里显示当前连接数是1
client_recent_max_input_buffer:8
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0
操作系统FD限制
最大连接数的修改受限于操作系统文件描述符FD的限制,调整最大连接数应先确认操作系统文件描述符限制,调整合适的操作系统文件描述符限制后,再调整redis的maxclients。
操作系统对FD的限制分为系统级和进程级两个维度,共三层:
| 层级 | 配置项 | 作用范围 | 默认值 | 可调 |
|---|---|---|---|---|
| 1. 系统级上限 | fs.file-max |
整个系统所有进程 FD 总和 | 几十万 ~ 上亿 | ✅ |
| 2. 进程级硬限制 | ulimit -Hn |
单个进程最多能打开的 FD 数 | 通常 65536 | ✅(root) |
| 3. 进程级软限制 | ulimit -Sn |
当前生效的 FD 限制 | 通常 1024 | ✅(≤ hard) |
查看系统级限制:fs.file-max,表示整个系统可分配的最大FD总数(所有进程可打开的FD上限)
postgres@slpc: sysctl fs.file-max
fs.file-max = 9223372036854775807
postgres@slpc:/etc$ sudo sysctl -a | grep -E "file|fs.nr_open"
fs.file-max = 9223372036854775807 # 系统全局最大FD数
fs.file-nr = 7776 0 9223372036854775807 # 当前已分配/ 使用/最大文件描述符
fs.nr_open = 1048576 # 单个进程最大文件描述符数
修改系统级限制:
# 编辑 /etc/sysctl.conf
echo 'fs.file-max = 2097152' >> /etc/sysctl.conf
# 立即生效
sysctl -p
进程级限制:
通过systemd配置文件修改进程级限制,修改文件为/etc/systemd/system/redis.service.d/override.conf,内容如下:
[Service]
LimitNOFILE=1048576
创建该文件并保存后,执行systemctl daemon-reload,然后systemctl restart redis重启redis服务,查看进程级限制:
postgres@slpc:/etc$ cat /proc/31011/limits | grep "Max open files"
Max open files 64032 64032 files
理论上64位系统FD上限位2^64,但实际上,受限于内存等限制,实际上可用的FD是有上限的。内存是决定实际可支持FD数量的最关键因素。每个打开的文件描述符,内核需要维护一定的元数据,分配数据结构(如struct file, struct inode等),占用一定的内核内存(不可swap):
用户空间 内核空间
----------- -----------
文件描述符表 struct file // 打开文件的上下文
(fd_table) ↓
struct inode // 文件系统元数据
↓
struct dentry // 目录项缓存
↓
VFS (虚拟文件系统层)
可以去查看Linux内核源码,大致的内存占用1~4KB作用
经验上,实际最大FD数约等于可用内核内存/每FD平均内存开销。




