Home / Databases / Redis

DatabasesRunbookIntermediate

Sev1: Redis refuses connections with ERR max number of clients reached

New Redis connections fail because maxclients is reached, often from a connection leak or a scale-out. Free slots, find the client that is leaking, and size maxclients and file limits correctly.

Deepak Bhardwaj6 min read

Applies toRedis OSS 6.x-8.x and Valkey 7.2-8.x (standalone, Sentinel, Cluster); notes for Amazon ElastiCache and Azure Cache for Redis / Azure Managed Redis

Symptoms

  • Applications log ERR max number of clients reached on connect, or see timeouts and connection resets from the client library.
  • Existing connections keep working, so some application instances look fine while new or restarted ones fail.
  • redis-cli from your own workstation also fails with the same error. There is no reserved admin connection.
  • It often starts after a deployment, an autoscaling event or a Redis failover (every client reconnects at once).

1. Get one connection in

You need one working connection to diagnose and fix this. In order of preference:

  1. Use a connection you already have open, such as a monitoring agent's or an open redis-cli session.
  2. Free a few slots by scaling down or restarting the application instances that hold the most connections. If you know the leaking service, stop one instance and connect right away.
  3. Drop TCP connections from the server side on self-managed Linux hosts. If the kernel supports socket destroy, ss -K closes matching sockets:
# count connections per client IP first
ss -tn state established '( sport = :6379 )' | awk 'NR>1 {split($4,a,":"); print a[1]}' | sort | uniq -c | sort -rn | head

# close connections from one leaking host
sudo ss -K state established '( sport = :6379 )' dst 10.20.3.41

Warning

Restarting Redis clears all connections, but on a cache without persistence it also empties the data set, and the cold cache can overload the database behind it. On a Sentinel or Cluster primary, a restart can start a failover. Restart only if the steps above are not possible and you know the data impact.

Use REDISCLI_AUTH instead of -a so the password does not show in the process list:

export REDISCLI_AUTH='<password>'
redis-cli -h redis01 -p 6379 PING

2. Confirm the limit and who is using it

redis-cli CONFIG GET maxclients
redis-cli INFO clients
redis-cli INFO stats | grep rejected_connections

connected_clients close to maxclients, and a growing rejected_connections, confirm the problem. Group connections by client IP, by client name and by idle time:

# by client IP
redis-cli CLIENT LIST | awk '{for(i=1;i<=NF;i++){split($i,a,"="); if(a[1]=="addr"){split(a[2],h,":"); print h[1]}}}' \
  | sort | uniq -c | sort -rn | head -20

# by client name (set by well-behaved libraries) and command
redis-cli CLIENT LIST | grep -o 'name=[^ ]* .*cmd=[^ ]*' | awk '{print $1, $NF}' | sort | uniq -c | sort -rn | head

# connections idle for more than 10 minutes
redis-cli CLIENT LIST | awk '{for(i=1;i<=NF;i++){split($i,a,"="); f[a[1]]=a[2]} if (f["idle"]>600) n++} END {print n+0}'

Map the IPs to hosts, pods or services. One application usually holds most of the connections.

Pattern Likely cause Fix
One service holds thousands of idle connections Connection leak (new client per request, never closed) Restart the service now; fix the code to use one shared client or pool
Connections spread evenly over many instances Pool size × instance count exceeds maxclients after a scale-out Lower the pool size or raise maxclients
Many clients with cmd=blpop, brpop, xread or subscribe Each blocking or pub/sub consumer holds its own connection Share subscribers, limit worker count
Spike right after a failover Reconnect storm, clients retrying without backoff Add jittered backoff; clear stale connections on the old primary
maxclients lower than you configured Redis reduced it to fit the OS file descriptor limit Step 4

3. Free connections now

Kill idle normal clients. This keeps replicas, the primary link and pub/sub clients connected (flags=N means a normal client):

redis-cli CLIENT LIST \
  | awk '{for(i=1;i<=NF;i++){split($i,a,"="); f[a[1]]=a[2]} if (f["idle"]>600 && f["flags"]=="N") print f["id"]}' \
  | xargs -r -n1 redis-cli CLIENT KILL ID

Or kill every connection from one leaking host:

redis-cli CLIENT LIST | grep -o 'id=[0-9]* addr=10\.20\.3\.41:[0-9]*' | cut -d' ' -f1 | cut -d= -f2 \
  | xargs -r -n1 redis-cli CLIENT KILL ID

Set an idle timeout so Redis closes abandoned connections by itself:

redis-cli CONFIG SET timeout 300

Caution

A low timeout closes long-lived but quiet pooled connections, and some client libraries do not recover well from that. Use a value longer than your clients' idle period, and check that they reconnect cleanly after the change. Do not kill clients with flags containing S (replica) or M (primary).

4. Raise maxclients if the load is legitimate

The default maxclients is 10000. Redis needs a file descriptor for each client plus some for its own use, so it cannot go above the process's open files limit. If the limit is too low, Redis lowers maxclients at startup and logs a warning such as You requested maxclients of 10000 requiring at least 10032 max file descriptors.

grep -i 'max open files' /proc/$(pidof redis-server)/limits
redis-cli CONFIG SET maxclients 20000
redis-cli CONFIG REWRITE      # persist to redis.conf, if Redis was started with a config file

CONFIG SET maxclients fails if the new value needs more descriptors than the hard limit allows. Raise the systemd limit and restart in a maintenance window:

# systemctl edit redis-server
[Service]
LimitNOFILE=65536

Each connection also uses memory for its buffers. Check INFO memory before you raise the limit a lot.

Managed services

  • Amazon ElastiCache: the connection limit is fixed per node and not configurable in the same way. Check the CurrConnections and NewConnections CloudWatch metrics, and scale out (more shards or replicas for reads) or fix the client.
  • Azure Cache for Redis / Azure Managed Redis: the connection limit depends on the tier and size. Check the current limits in the documentation and the Connected Clients metric. Scaling to a larger size raises the limit.
aws cloudwatch get-metric-statistics --namespace AWS/ElastiCache --metric-name CurrConnections \
  --dimensions Name=CacheClusterId,Value=prod-redis-0001-001 \
  --start-time $(date -u -d '-3 hours' +%FT%TZ) --end-time $(date -u +%FT%TZ) \
  --period 300 --statistics Maximum

5. Verify recovery

  • INFO clients shows connected_clients well below maxclients, and rejected_connections stops growing.
  • Application error rates for Redis connect failures drop to zero.
  • After the leaking service is fixed, its connection count stays flat during a load test instead of climbing.

Prevent it next time

  • Alert at 70-80% of maxclients (or the managed service limit), and on any rise in rejected_connections.
  • Use one shared, thread-safe client or a bounded pool per process. Set CLIENT SETNAME (most libraries have a client name option) so CLIENT LIST shows which service owns each connection.
  • Budget connections: pool size × maximum instances (including autoscaling) must fit under the limit with room for failover reconnects.
  • Add jittered exponential backoff to reconnect logic.
  • Set timeout to a value that matches your clients, and raise LimitNOFILE on self-managed hosts so the configured maxclients is the real one. For memory-related refusals, see Redis memory full and evictions.