NOTE
1.9 Redis Client Connection Handling
Redis client connection handling, maximum clients, client list, timeouts, and TCP keepalive.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Client Connection Handling Process
- When a client connection is accepted by the server.
- Perform the following steps:
- Because Redis uses multiplexing and non-blocking I/O, the client socket is in non-blocking mode.
- Set the
TCP_NODELAYoption. This is to ensure that the connection has no delay. - Create a readable file event. This allows the server to receive the client’s query request when new data on the socket becomes readable.
- Check whether the number of connections has reached
maxclients. If so, send an error to the client. The client closes the connection immediately after receiving it.
2. Maximum Number of Connections
- It can be configured through
maxclientsinredis.conf. Ifmaxclients + 32 >the system soft limit, Redis automatically adjustsmaxclientsuntil it matches the soft limit. - The operating system’s maximum file-descriptor limit for the process also needs to be checked.
ulimit -Sn 100000 # This will only work if hard limit is big enough.
sysctl -w fs.file-max=100000
3. Commands
client listGet a list of connected clients and their states.- Example
127.0.0.1:6379> client list id=5 addr=127.0.0.1:57066 fd=8 name= age=14549 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client id=6 addr=127.0.0.1:50325 fd=9 name= age=82 idle=78 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=client- Explanation
addr: client address, that is, the client IP and remote port used to connect to the Redis server.fd: client socket file-descriptor number.name: client name set byCLIENT SETNAME.age: number of seconds the connection has existed.idle: number of seconds the connection has been idle.flags: client type (Nmeans a normal client; check the full flags list).omem: amount of memory used by the client’s output buffer.cmd: last command executed.
4. Client Timeout
By default, Redis does not close the connection to a client after it has been idle for a few seconds; it keeps the connection open.
This limit can be configured through redis.conf or directly with CONFIG SET timeout <value>.
This timeout applies only to normal clients and does not apply to pub/sub clients.
5. TCP Keepalive
Redis 3.2 and later enable the SO_KEEPALIVE option by default, with a time of 300 seconds.
It is used to detect whether the client is still alive.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub