Error Medic

How to Fix 'Redis Connection Refused' and Pool Exhaustion Errors

Resolve 'Redis connection refused' and pool exhaustion errors by fixing bind addresses, tuning maxclients, and optimizing connection pooling. Complete guide.

Last updated:
Last verified:
1,179 words
Key Takeaways
  • Network or Binding: Redis service is down, or bound exclusively to 127.0.0.1 preventing external access.
  • Resource Limits: Connection pool exhausted leading to 'ERR max number of clients reached'.
  • Performance: Single-threaded blocking due to slow queries causing application-side timeouts.
  • Quick fix: Verify Redis daemon status, adjust the bind directive in redis.conf, and audit your app's connection pool settings.
Fix Approaches Compared
MethodWhen to UseTimeRisk
Update redis.conf bindRedis bound to 127.0.0.1 but needs external network access5 minsMedium (Requires proper firewalling)
Increase maxclientsHitting 'ERR max number of clients reached' under normal load10 minsLow (if host OS RAM/limits permit)
Implement Connection PoolingHigh connection churn causing timeouts and port exhaustionHoursLow (Best practice for production)
Optimize Slow QueriesRedis slow query causing timeouts or perceived deadlocksDaysLow (Improves overall stability)

Understanding the Error: Redis Connection Refused

When you encounter a Redis connection refused error, your application is attempting to open a TCP socket connection to the Redis server, but the operating system at the destination is actively rejecting it. In application logs, this typically surfaces as Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379.

While the symptom is simple—a failed connection—the root cause can span network misconfigurations, resource exhaustion, or blocked event loops.

Root Cause 1: Service Status and Bind Configuration

The most frequent cause for a raw connection refused error is that Redis simply isn't listening on the IP address your application is targeting. By default, modern Redis installations bind exclusively to 127.0.0.1 (localhost) for security. If your application resides on a different server or within a separate Docker container, the connection will be refused.

Diagnosis: Run sudo netstat -plunt | grep 6379 on the Redis server. If you see it bound only to 127.0.0.1, external traffic cannot reach it.

Fix: Modify your /etc/redis/redis.conf file. Locate the bind directive and update it to accept connections from your application's IP, or your internal private network (e.g., bind 127.0.0.1 10.0.0.5). Restart the service using sudo systemctl restart redis.

Root Cause 2: Redis Too Many Connections and Pool Exhaustion

If the connection is occasionally accepted but frequently fails, or if you see ERR max number of clients reached, you are experiencing a redis too many connections issue. This directly leads to redis connection pool exhausted errors in your application code (common in Java/Jedis, Node.js, and Python/Celery).

By default, Redis supports 10,000 concurrent client connections (maxclients). Once this limit is hit, Redis will actively refuse new connections.

Diagnosis: Check the current connected clients using redis-cli info clients. Look at the connected_clients metric.

Fix:

  1. Application Side: Implement proper connection pooling. Do not open a new Redis connection per HTTP request. Use robust libraries and ensure connections are released back to the pool.
  2. Server Side: Increase the maxclients directive in redis.conf if your server has sufficient RAM and file descriptors: maxclients 20000. You will also need to increase the OS open file limit (ulimit -n).

Root Cause 3: The Single-Threaded Bottleneck - Slow Queries and Timeouts

Redis is primarily single-threaded for command execution. If you run a redis slow query (like KEYS * on a large dataset, or a massive SMEMBERS evaluation), the entire Redis instance blocks. During this blocking period, all other incoming commands queue up, often resulting in a redis timeout or a perceived redis deadlock on the client side.

Diagnosis: Access the Redis slow log by running redis-cli slowlog get 10 to see the top 10 slowest recent queries.

Fix: Replace blocking O(N) commands with iterative O(1) commands. For example, replace KEYS * with SCAN. Offload heavy analytical queries to a read-replica.

Root Cause 4: Security and Permission Denied

Sometimes the TCP connection succeeds, but Redis immediately drops the payload or returns a redis permission denied error (e.g., -NOAUTH Authentication required or -NOPERM). This happens when Redis is protected by a password (requirepass) or ACLs (Access Control Lists) introduced in Redis 6.

Fix: Ensure your connection string includes the correct password: redis://username:password@host:6379. Review ACL rules using redis-cli ACL LIST.

Root Cause 5: Architecture and Replication Lag

In distributed architectures, you might encounter redis replication lag. If your application reads from replica nodes, high write volume on the primary can cause replicas to fall behind. While this doesn't directly cause a raw TCP connection refused, it leads to stale data reads and, in edge cases where replicas are configured to stop serving stale data (replica-serve-stale-data no), they will reject read queries, resembling a connection drop.

Summary of Actionable Steps:

  1. Verify Redis is running: systemctl status redis
  2. Validate network bindings: grep "^bind" /etc/redis/redis.conf
  3. Inspect active connection counts: redis-cli info clients
  4. Review the slowlog for blocking operations: redis-cli slowlog get
  5. Audit application-side connection pooling logic.

Frequently Asked Questions

bash
# 1. Check if Redis is running and listening on the expected port
sudo netstat -plunt | grep 6379

# 2. Check the configured bind address
grep "^bind" /etc/redis/redis.conf

# 3. Check for maxclients limit
grep "^maxclients" /etc/redis/redis.conf

# 4. View current connection metrics via redis-cli
redis-cli info clients

# 5. Check the slow log for commands blocking the single thread
redis-cli slowlog get 10

# 6. Monitor real-time commands (Caution: impacts performance on busy servers)
redis-cli monitor | grep -v "PING"
E

Error Medic Editorial

Error Medic Editorial is a team of seasoned Site Reliability Engineers and DevOps practitioners dedicated to solving complex infrastructure bottlenecks, database tuning, and distributed systems troubleshooting.

Sources

Related Articles in Redis

Explore More Database Guides