Node-redis: No reconnecting

Created on 9 Mar 2019  路  6Comments  路  Source: NodeRedis/node-redis

Hey,
I cannot get this module to reconnect redis.

Taking this configration:

  const client = redis.createClient({
    no_ready_check: false,
    enable_offline_queue: true,
    retry_unfulfilled_commands: true,
    socket_keepalive: true,
    retry_strategy: function (options) {
      if (options.error && options.error.code === 'ECONNREFUSED') {
        // End reconnecting on a specific error and flush all commands with
        // a individual error
        return new Error('The server refused the connection');
      }
      if (options.total_retry_time > 1000 * 60 * 60) {
        // End reconnecting after a specific timeout and flush all commands
        // with a individual error
        return new Error('Retry time exhausted');
      }
      if (options.attempt > 10) {
        // End reconnecting with built in error
        return undefined;
      }
      console.log('here');
      // reconnect after
      return 100;
    },
    host: process.env.REDIS_HOST || '127.0.0.1',
    port: 6379
  });

I see the here when i stop my local redis, but then i restart it again and wait any amount of time, any subsequent redis calls give me:

{ AbortError: EXEC can't be processed. The connection is already closed.
    at handle_offline_command (/Users/kstoney/git/autotrader/platform-alert-forwarder/node_modules/redis/index.js:851:15)
    at RedisClient.internal_send_command (/Users/kstoney/git/autotrader/platform-alert-forwarder/node_modules/redis/index.js:885:9)
    at Multi.exec_transaction (/Users/kstoney/git/autotrader/platform-alert-forwarder/node_modules/redis/lib/multi.js:115:18)
    at Object.RedisCache.self.set (/Users/kstoney/git/autotrader/platform-alert-forwarder/lib/caches/redis.js:41:8)
    at Object.sendMessage [as handle] (/Users/kstoney/git/autotrader/platform-alert-forwarder/lib/forwarders/slack.js:197:28)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
  command: 'EXEC',
  code: 'NR_CLOSED',
  errors:
   [ { AbortError: SET can't be processed. The connection is already closed.

Any ideas? I need some tolerance to redis blips.

question

Most helpful comment

Yeah we switched to ioredis haha

All 6 comments

Same problem without some options like queue offline, my configuration is this one below:

var client = redis.createClient({
    port: process.env.REDIS_PORT,
    host: process.env.REDIS_HOST,
    retry_strategy: function (options) {
        if (options.error && options.error.code === 'ECONNREFUSED') {
            // End reconnecting on a specific error and flush all commands with
            // a individual error
            logger.debug('[Redis Strategy] - Connection Refused options:', options);
            return new Error('The server refused the connection');
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
            // End reconnecting after a specific timeout and flush all commands
            // with a individual error
            logger.debug('[Redis Strategy] - Time exhausted options:', options);
            return new Error('Retry time exhausted');
        }
        if (options.attempt > 10) {
            // End reconnecting with built in error
            return undefined;
        }
        // reconnect after
        return Math.min(options.attempt * 100, 3000);
    }
});

client.on('connect', function () {
    console.log('Redis client connected');
});

client.on('error', function (err) {
    console.log('Something went wrong ' + err);
});

Wow, can't believe I haven't noticed this behavior.
I run my apps with https://github.com/Unitech/pm2, which restarts the app whenever a failure occurs, so this has gone on for quite some time unnoticed.

Great catch!

Is there another way to accomplish reconnection until its fixed? (I see the latest release is 2 years ago)

Yeah we switched to ioredis haha

@Stono i agree, i am switching over to ioredis today. It is getting updated more often, has more features and it's fassttterr.

Thanks, it really sounds better, I was unfamiliar with this library.
Unfourtantley for this current project I won't be able to switch but it's good to know for future work.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

id0Sch picture id0Sch  路  4Comments

adamgajzlerowicz picture adamgajzlerowicz  路  4Comments

lemon707 picture lemon707  路  3Comments

ghost picture ghost  路  3Comments

michaelwittig picture michaelwittig  路  3Comments