Max Clients

Difficulty: Beginner

Question

How do you check how many clients are connected to Redis right now, and what the maximum connection limit is?

Answer

Run INFO clients to see the live count in connected_clients, and CONFIG GET maxclients to see the ceiling, which defaults to 10,000. When the limit is hit, Redis rejects new connections with ERR max number of clients reached and increments rejected_connections in INFO stats.

Reading the numbers that matter

INFO clients returns a small block of fields. The ones worth knowing:

  • connected_clients: open client connections, not counting replicas.
  • blocked_clients: clients waiting on blocking calls such as BLPOP, BRPOP or XREAD BLOCK.
  • maxclients: the configured limit (shown here in recent versions, so you may not even need CONFIG GET).
  • client_recent_max_input_buffer and client_recent_max_output_buffer: early warning that one client is sending or receiving huge payloads.

For per-connection detail use CLIENT LIST. Each line shows the client's addr, name, age (seconds connected), idle (seconds since last command), db and the last cmd. Sorting this by idle is the fastest way to spot leaked connections.

Why the real limit can be lower than maxclients

Every client is a file descriptor. At startup Redis checks the process's open-file limit (ulimit -n) and reserves 32 descriptors for its own use. If maxclients 10000 is configured but the OS only allows 4,096 files, Redis lowers maxclients and logs a warning. So on a fresh Linux box you must raise the limit (systemd LimitNOFILE, or /etc/security/limits.conf) before raising maxclients. Managed services such as ElastiCache or Upstash set their own per-plan limits, and on those you check the provider's console as well.

What usually causes connection exhaustion

Hitting 10,000 connections is almost always an application bug, not genuine load:

  1. No connection pooling. A Node.js or Python handler that creates a new client per request and never calls quit() leaks one connection per call.
  2. Serverless fan-out. Each Lambda or Vercel function instance holds its own connection. A traffic spike during a Swiggy-style lunch rush can spin up thousands of instances, each with a pool of 10.
  3. Too many pods times pool size. 200 Kubernetes pods with a pool of 50 is already 10,000.
  4. Idle connections never closed. timeout defaults to 0, meaning Redis never drops idle clients.

Fixes and safe defaults

  • Use one shared client (or a small pool) per process. Redis is single-threaded for command execution, so a huge pool rarely adds throughput.
  • Set timeout 300 so idle clients are dropped, and keep tcp-keepalive at its default of 300 seconds so dead peers are detected.
  • Name connections with CLIENT SETNAME checkout-api so CLIENT LIST shows which service is misbehaving.
  • Kill a runaway client with CLIENT KILL ID <id> or a whole group with CLIENT KILL USER <name>.
  • Alert on connected_clients crossing around 80% of maxclients, and on any increase in rejected_connections.
Diagnosing a connection limit problem. A quick checklist when clients start seeing max number of clients reached.
  1. Check INFO clients: Compare connected_clients with maxclients.
  2. Check rejected_connections: A rising count in INFO stats confirms clients are being refused.
  3. Run CLIENT LIST: Group connections by name or addr and sort by idle time.
  4. Find the leaking service: Usually a missing pool, serverless fan-out or no quit() call.
  5. Fix and set timeout: Share one client per process and drop idle connections.

Code examples

Inspecting connections from redis-cli

# Current connection count and limit
redis-cli INFO clients | grep -E 'connected_clients|blocked_clients|maxclients'
redis-cli CONFIG GET maxclients

# Have any connections been refused?
redis-cli INFO stats | grep rejected_connections

# Who is connected? Count connections per client name
redis-cli CLIENT LIST | grep -o 'name=[^ ]*' | sort | uniq -c | sort -rn

# Drop idle clients after 5 minutes and raise the limit
redis-cli CONFIG SET timeout 300
redis-cli CONFIG SET maxclients 20000   # needs ulimit -n above 20032

Counting connections by name immediately shows which service holds the most. Unnamed connections (name= with nothing after) are a hint that some code path is not using the shared, named client.

One shared client per process in Node.js

// redis.js - import this module everywhere instead of creating clients
import { createClient } from 'redis';

const client = createClient({
  url: process.env.REDIS_URL,
  name: 'orders-api',          // shows up in CLIENT LIST
});

client.on('error', (err) => console.error('Redis error', err));
await client.connect();

// Close cleanly on shutdown so connections are not left dangling
process.on('SIGTERM', async () => {
  await client.quit();
  process.exit(0);
});

export default client;

Creating the client once at module load means every request reuses the same connection. Redis pipelines commands from one connection efficiently, so one client per process is enough for most apps.

Key points

  • connected_clients in INFO clients is the live count; maxclients defaults to 10,000.
  • Redis reserves 32 file descriptors and silently lowers maxclients if ulimit -n is too small.
  • rejected_connections in INFO stats counts refused connections; alert on it.
  • timeout defaults to 0, so idle clients are never disconnected unless you set it.
  • CLIENT SETNAME plus CLIENT LIST is the quickest way to find the leaking service.

Common follow-up questions

  • What happens to a client that connects after maxclients is reached?
  • How would you size a connection pool for 50 pods talking to one Redis primary?
  • Why do serverless functions cause connection storms, and how do you mitigate them?
  • What is the difference between timeout and tcp-keepalive in redis.conf?

Interview tips

  • Mention both commands and the 10,000 default, then immediately add the ulimit caveat; that is what separates people who have run Redis from people who read about it.
  • Frame connection exhaustion as an application problem (pooling, leaks) rather than a reason to raise maxclients.
  • If you have a real incident story about leaked connections, this is a good place to tell it briefly.

Concepts covered

maxclients, INFO clients, CLIENT LIST, Connection Pooling, File Descriptors

More Redis & Caching questions