Error: connect ECONNREFUSED 127.0.0.1:6379

Hi,

I have redis instance running in region ‘Singapore’ and also a node web service running in same region under ‘Starter’ plan.

Even though the server logs says ‘Redis connection established!’ I get this ECONNREFUSED 127.0.0.1:6379 in the logs. Why I am I getting this?
my web service runs at https://warm-mailer.onrender.com

Please help

The 127.0.0.1 there means that you have application code that is trying to use Redis on the localhost address and not using a separate Redis resource. You would need to review your application code to determine the cause of that.

I don’t have any other Redis resource in my code. I have this below one only

require('dotenv').config();
const { Queue, Worker, QueueEvents } = require("bullmq");
const Redis = require("ioredis");

const connection = new Redis(
  {
    host: process.env.REDIS_SERVICE_NAME,
    port: process.env.REDIS_PORT || 6379,
  },
  {
    maxRetriesPerRequest: null
  }
);

const mailQueue = new Queue("jobQueue", {
  connection
});

connection.on("connect", () => {
  console.log("Redis connection established!");
});

connection.on('error', (err) => {
  console.log('Redis connection error', err)
})

server logs shows ‘Redis connection established!’ as well. How is it trying to connect to 127.0.0.1?

If I intentionally change the REDIS_SERVICE_NAME variable for hostname to this ‘redis://’, the service is trying to connect to both ‘redis://’ and '127.0.0.1. Please check the logs below:

Error: getaddrinfo ENOTFOUND redis://<hostname>
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'redis://<hostname>'
}
Error: connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1606:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 6379
}
Error: getaddrinfo ENOTFOUND redis://<hostname>
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'redis://<hostname>'
}

I’d defer to their documentation at https://github.com/redis/ioredis#readme as this is a configuration issue using the package.

I would also suggest that redis:// is not a hostname but a URL, what if you just try the Redis hostname, without any protocol specified?

yes I understand. I just want to show you if I change the redis hostname, the service is trying to connect to both 127.0.0.1 and the wrong hostname I provided. I have reverted to the correct Redis hostname already and now the logs show Error: connect ECONNREFUSED 127.0.0.1:6379

This will be something in your code that is not using the connection that you’ve defined, have you shared the complete code here?

I fixed the problem by changing my code to this:

I gave the full REDIS_INTERNAL_URL instead of only REDIS_SERVICE_NAME.

const connectRedis = new IORedis(
process.env.NODE_ENV === “prod”
? process.env.REDIS_URL
: “localhost”,
{ maxRetriesPerRequest: null }
);

connectRedis.on(“connect”, () => {
console.log(“Redis connection established!”);
});

connectRedis.on(“error”, (err) => {
console.log(“Redis connection error”, err);
});

Thanks

Great, I’m glad you got it all figured out here.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.