Redis - Client Connection
Redis - Client Connection
Section titled “Redis - Client Connection”Redis accepts client connections on its configured TCP port (default 6379) and optionally on a Unix socket. When a new client connects, Redis performs several operations to handle it efficiently:
- The client socket is set to non-blocking mode, as Redis uses multiplexed, non-blocking I/O to handle many clients concurrently with a single thread.
- The
TCP_NODELAYoption is set to disable Nagle’s algorithm, ensuring commands are sent immediately without delay for better latency. - A readable file event is registered, allowing the Redis event loop to process client commands as soon as they arrive.
Maximum Number of Clients (maxclients)
Section titled “Maximum Number of Clients (maxclients)”The maxclients directive in redis.conf sets the maximum number of concurrent client connections. The default is typically 10,000.
127.0.0.1:6379> CONFIG GET maxclients
1) "maxclients"2) "10000"While you can change this value, the practical limit is often determined by the operating system’s maximum number of file descriptors. In most applications, you should not need to raise this limit.
Best Practice: Client-Side Connection Pooling
Section titled “Best Practice: Client-Side Connection Pooling”Instead of opening and closing connections for every request, applications should use a connection pool. A pool maintains a set of ready-to-use connections, which significantly reduces the latency and resource overhead associated with establishing new TCP connections. All mature Redis client libraries provide built-in support for connection pooling.
Security: Protected Mode
Section titled “Security: Protected Mode”Modern versions of Redis (3.2+) enable protected-mode by default. This is a critical security feature. If protected mode is on and no password (requirepass) has been set, Redis will only respond to clients connecting from the loopback interface (localhost). This prevents unsecured Redis instances from being accidentally exposed to the public internet.
Client Management Commands
Section titled “Client Management Commands”Redis provides commands to inspect and manage connected clients, which are invaluable for debugging and administration.
| Command | Description |
|---|---|
| CLIENT LIST | Returns a detailed list of all clients connected to the server, including their address, connection age, and last command. |
| CLIENT SETNAME | Assigns a name to the current connection. This name appears in CLIENT LIST and is very useful for identifying application clients during debugging. |
| CLIENT GETNAME | Returns the name of the current connection. |
| CLIENT KILL [filter] | Closes a specific client connection. You can filter by address, client ID, or name. Useful for terminating misbehaving or idle connections. |
| CLIENT PAUSE <timeout_ms> [WRITE|ALL] | Suspends all clients for a specified time in milliseconds. This is a powerful command used during failover procedures in a managed environment. |