查看 Redis 当前连接数/最大连接数
127.0.0.1:6379> info clients
#Clients
connected_clients:621
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
127.0.0.1:6379>
方法2:config get maxclients 可以查询redis允许的最大连接数
127.0.0.1:6379> CONFIG GET maxclients
##1) "maxclients"
##2) "10000"
修改 Redis 最大连接数
方法1
在2.6之后版本,可以修改最大连接数配置,默认10000,可以在redis.conf配置文件中修改
...
# maxclients 10000
...
方法2
config set maxclients num 可以设置redis允许的最大连接数
127.0.0.1:6379> CONFIG set maxclients 10
OK
127.0.0.1:6379>
启动redis.service服务时加参数–maxclients 100000来设置最大连接数限制
方法3
启动redis.service服务时加参数–maxclients 100000来设置最大连接数限制
$ redis-server --maxclients 100000 -f /etc/redis.conf
How Redis handles client connections
How client connections are accepted
Redis accepts clients connections on the configured listening TCP port and on the Unix socket if enabled. When a new client connection is accepted the following operations are performed:
- The client socket is put in non-blocking state since Redis uses multiplexing and non-blocking I/O.
- The
TCP_NODELAY
option is set in order to ensure that we don’t have delays in our connection. - A readable file event is created so that Redis is able to collect the client queries as soon as new data is available to be read on the socket.
After the client is initialized, Redis checks if we are already at the limit of the number of clients that it is possible to handle simultaneously (this is configured using the maxclients
configuration directive).
In case it can’t accept the current client because the maximum number of clients was already accepted, Redis tries to send an error to the client in order to make it aware of this condition, and closes the connection immediately. The error message will be able to reach the client even if the connection is closed immediately by Redis because the new socket output buffer is usually big enough to contain the error, so the kernel will handle the transmission of the error.
See https://redis.io/topics/clients for more details.
Client timeouts
By default recent versions of Redis don’t close the connection with the client if the client is idle for many seconds: the connection will remain open forever.
However if you don’t like this behavior, you can configure a timeout, so that if the client is idle for more than the specified number of seconds, the client connection will be closed.
TCP keepalive
Recent versions of Redis (3.2 or greater) have TCP keepalive (SO_KEEPALIVE
socket option) enabled by default and set to about 300 seconds. This option is useful in order to detect dead peers (clients that cannot be reached even if they look connected). Moreover, if there is network equipment between clients and servers that need to see some traffic in order to take the connection open, the option will prevent unexpected connection closed events.
查看当前client 连接情况
The Redis client command allows to inspect the state of every connected client, to kill a specific client, to set names to connections. It is a very powerful debugging tool if you use Redis at scale.
CLIENT LIST is used in order to obtain a list of connected clients and their state:
redis 127.0.0.1:6379> client list
addr=127.0.0.1:52555 fd=5 name= age=855 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
addr=127.0.0.1:52787 fd=6 name= age=6 idle=5 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
In the above example session two clients are connected to the Redis server. The meaning of a few of the most interesting fields is the following:
- addr: The client address, that is, the client IP and the remote port number it used to connect with the Redis server.
- fd: The client socket file descriptor number.
- name: The client name as set by CLIENT SETNAME.
- age: The number of seconds the connection existed for.
- idle: The number of seconds the connection is idle.
- flags: The kind of client (N means normal client, check the full list of flags).
- omem: The amount of memory used by the client for the output buffer.
- cmd: The last executed command.
See the CLIENT LIST documentation for the full list of fields and their meaning.
Once you have the list of clients, you can easily close the connection with a client using the CLIENT KILL command specifying the client address as argument.
The commands CLIENT SETNAME and CLIENT GETNAME can be used to set and get the connection name. Starting with Redis 4.0, the client name is shown in the SLOWLOG output, so that it gets simpler to identify clients that are creating latency issues.