Persistent Connection Issues with Bull Queue and Redis in Heroku-Deployed Node.js App

47 Views Asked by At

ENVIROMENT: Node.js application Redis for caching Bull for queue management Heroku for deployment

OVERVIEW: My Node app uses Bull queues with Redis to make api calls to OpenAI's GPT-4 (gpt4_1106) and dalle 3, with processing times that obviously vary. The application was functioning correctly until an upgrade of the Redis plugin was required to support an increased number of queues. Now this: errors

THE ISSUE: Bull queues are consistently failing to connect to Redis, leading to ECONNREFUSED and ETIMEDOUT errors. Initially, the app was attempting to connect to 127.0.0.1:6379, which is of course for the local environment, not production. Confirmed environment variables in Heroku dash are correct, tried countless redisOptions configs to no avail. Even without frontend requests, my application repeatedly logs errors indicating failed Redis connections. These include ECONNREFUSED 127.0.0.1:6379 and ECONNREFUSED 100.25.73.215:24990.

redisClient.js:**

  • Utilizes REDIS_URL and REDIS_TLS_URL environment variables.
  • Implements TLS options in the Redis configuration.
  • Manages Redis connections with a defined maximum number of connection attempts and custom retry strategy.
  • Disconnects the Redis client after reaching the maximum connection attempts.
  • centralizedQueues_1106.js:
  • Various methods for initializing Bull queues were attempted, including custom createClient function.

redisClient.js:
const Redis = require('ioredis');
const redisUrl = process.env.REDIS_TLS_URL || process.env.REDIS_URL;

let connectionAttempts = 0;
const maxConnectionAttempts = 5;
let isConnected = false;

const redisOptions = {
  retryStrategy(times) {
    if (connectionAttempts >= maxConnectionAttempts) {
      console.error('Max initial connection attempts reached. Stopping retries.');
      return null;
    }
    const delay = Math.min(times * 50, 2000);
    connectionAttempts++;
    return delay;
  },
  maxRetriesPerRequest: 10,
  connectTimeout: 10000,
  keepAlive: 60
};

if (process.env.NODE_ENV === 'production') {
  redisOptions.tls = { rejectUnauthorized: false };
}

const redis = new Redis(redisUrl, redisOptions);

module.exports = {
  redis,
  isConnected 
};

centralizedQueues_1106_v1.js:

const queues = {
  queue_1_Story_1106: new Queue('queue_1_Story_1106', {
    createClient: function (type) {
      return createClient(true); // Pass true for Bull clients
    }
  }),
  queue_2_Prompt_Image_1106: new Queue('queue_2_Prompt_Image_1106', {
    createClient: function (type) {
      return createClient(true); // Pass true for Bull clients
    }
  })
};

centralizedQueues_1106_v2.js:

const queues = {
  queue_1_Story_1106: new Queue('queue_1_Story_1106', {
    createClient: function (type) {
      return createClient(true); // Pass true for Bull clients
    }
  }),
  queue_2_Prompt_Image_1106: new Queue('queue_2_Prompt_Image_1106', {
    createClient: function (type) {
      return createClient(true); // Pass true for Bull clients
    }
  })
};

Any help profoundly appreciated.

0

There are 0 best solutions below