🔧 Error Fixes
· 2 min read
Last updated on

Redis: Connection Refused — How to Fix It


Error: connect ECONNREFUSED 127.0.0.1:6379

What causes this

Your application tried to connect to Redis but nothing is listening on that address and port. Common causes:

  • Redis isn’t installed or isn’t running
  • Redis is running on a different port or host
  • Redis is bound to a different interface (e.g., only accepting connections from 127.0.0.1 but you’re connecting from a Docker container)
  • A firewall is blocking the connection
  • Redis crashed due to memory issues

Fix 1: Check if Redis is running

# Check the service status
sudo systemctl status redis
# or
sudo systemctl status redis-server

# Try pinging Redis directly
redis-cli ping
# Should return: PONG

If it’s not running, start it:

# Linux
sudo systemctl start redis

# macOS (Homebrew)
brew services start redis

# Or run it directly
redis-server

Fix 2: Check which port Redis is using

# Check Redis config
grep "^port" /etc/redis/redis.conf

# Or check what's listening on port 6379
ss -tlnp | grep 6379
# or on macOS:
lsof -i :6379

If Redis is on a different port, update your connection:

// Node.js
const redis = new Redis({ host: 'localhost', port: 6380 });

Fix 3: Run Redis with Docker

If you don’t want to install Redis locally:

docker run -d --name redis -p 6379:6379 redis:7-alpine

Verify it’s running:

docker exec redis redis-cli ping
# PONG

Fix 4: Check the bind address

By default, Redis only accepts connections from localhost. If your app is in a Docker container connecting to Redis on the host:

# In redis.conf, change:
bind 127.0.0.1
# To:
bind 0.0.0.0

Or if both are in Docker, use Docker networking:

# docker-compose.yml
services:
  app:
    build: .
    depends_on:
      - redis
  redis:
    image: redis:7-alpine

Then connect to redis (the service name) instead of localhost:

const redis = new Redis({ host: 'redis', port: 6379 });

Fix 5: Check your connection string

Make sure the connection URL matches your setup:

// ❌ Wrong host or port
const redis = new Redis('redis://localhost:6380');

// ✅ Default Redis
const redis = new Redis('redis://localhost:6379');

// ✅ With password
const redis = new Redis('redis://:yourpassword@localhost:6379');

// ✅ Redis Cloud or managed service
const redis = new Redis('redis://default:password@redis-12345.c1.us-east-1.ec2.cloud.redislabs.com:12345');

Fix 6: Redis crashed — check memory

Redis stores everything in memory. If it runs out:

# Check Redis logs
tail -50 /var/log/redis/redis-server.log

# Check memory usage
redis-cli info memory

If Redis was killed by the OOM killer, you’ll see it in system logs:

dmesg | grep -i "killed process"

Set a memory limit in redis.conf:

maxmemory 256mb
maxmemory-policy allkeys-lru

How to prevent it

  • Use Docker Compose with depends_on to ensure Redis starts before your app
  • Add retry logic to your Redis connection — Redis might restart and your app should reconnect automatically
  • Set maxmemory in Redis config to prevent OOM crashes
  • Monitor Redis with redis-cli info or a tool like RedisInsight
  • In production, use a managed Redis service (AWS ElastiCache, Upstash, Redis Cloud) to avoid managing the server yourself