Home > Back-end >  Unable to connect to Heroku Redis from Node Server
Unable to connect to Heroku Redis from Node Server

Time:08-13

Works well on connecting to Redis locally and through Official Redis Docker image. But, when I switch to Heroku Redis values for ENV variables. It is unable to connect.

I have tried full url option as well, but that doesn't seem to work for any Redis connections when I need to add options object as 2nd parameter to new Redis(), Url option works if I don't pass any options for only locally and Official Redis Docker image.

Adding only heroku redis URI with no options to new Redis(), looks like it works, but then I get Redis Connection Failure after 10 seconds.

Does Heroku-Redis need some sort of extra preparation step?

import Redis, { RedisOptions } from 'ioredis';
import logger from '../logger';

const REDIS_HOST = process.env.REDIS_HOST || '127.0.0.1';
const REDIS_PORT = Number(process.env.REDIS_PORT) || 6379;
const REDIS_PASSWORD = process.env.REDIS_PASSWORD;
const REDIS_DB = Number(process.env.REDIS_DB) || 0;

const redisConfig: RedisOptions = {
  host: REDIS_HOST,
  port: Number(REDIS_PORT),
  password: REDIS_PASSWORD,
  db: Number(REDIS_DB),
  retryStrategy: function (times) {
    if (times % 4 == 0) {
      logger.error('Redis reconnect exhausted after 4 retries');
      return null;
    }
    return 200;
  },
};

const redis = new Redis(redisConfig);
redis.on('error', function () {
  logger.error('Redis Connection Failure');
});

export default redis;

CodePudding user response:

I'm not sure where you got the idea to use environment variables called REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, and REDIS_DB. Heroku Data for Redis provides a single environment variable that captures all of this:

After Heroku Data for Redis has been created, the new release is created and the application restarts. A REDIS_URL config var is available in the app configuration. It contains the URL you can use to access the newly provisioned Heroku Data for Redis instance.

Here is their example of how to connect from Node.js:

const redis = require("redis");

const client = redis.createClient({
  url: process.env.REDIS_URL,  
  socket: {
    tls: true,
    rejectUnauthorized: false
  }
});

So, change your configuration object accordingly:

const REDIS_URL = process.env.REDIS_URL;

const redisConfig: RedisOptions = {
  url: REDIS_URL,              // <--
  socket: {                    // <--
    tls: true,                 // <--
    rejectUnauthorized: false  // <--
  },                           // <--
  retryStrategy: function (times) {
    if (times % 4 == 0) {
      logger.error('Redis reconnect exhausted after 4 retries');
      return null;
    }
    return 200;
  },
};

You are already using an environment variable locally to set your Redis password locally. Replace that with an appropriate REDIS_URL that contains all of your defaults, e.g. something like this:

REDIS_URL=redis://user:password@host:port/database
  • Related