Reconnect strategy not being used and ready event fired without being ready.

See original GitHub issue

I’ve recently upgraded to node redis 4.1.0 and I’m having issues with reconnectStrategy and connection handling in general.

Logs from reconnectstrategy are never printed. When I start with redis server down. I still get the ready event triggered. A bit later I get an error event and it immediately “connects” (triggering ready again); And this just continues; The 5 second reconnect timer is not used.

When redis is online when I start the application and I later turn it off: it seems to go even faster. It does not wait long between the ready event and an error.

I’m initializing redis like so:

const options: RedisClientOptions<RedisModules, RedisFunctions, S> = {
    username: this.username,
    password: this.password,
    socket: {
        host: this.host,
        port: this.port,
        reconnectStrategy: (attempts) => {
            console.log(`Redis reconnecting attempt ${attempts}`);
            this.connected = false;
            if (attempts == 1) {
                console.log(`${this.constructor.name} failed to connect to ${this.host}:${this.port}. Reconnecting...`);
            }
            return 5000;
        },
    },
    scripts: scripts,
};
const redisClient = createClient(options);
redisClient.on('ready', () => {
    console.log(`${this.constructor.name} connected to ${this.host}:${this.port}`);
});
redisClient.on('error', (err) => {
    console.log('error');
    return;
});
redisClient.on('reconnecting', () => {
    return;
});
redisClient.on('end', () => {
    return;
});
redisClient.on('connect', () => {
    return;
});

redisClient.connect();

Environment:

  • Node.js Version: v14.16.0
  • Node Redis Version: 4.1.0
  • Platform: Mac OS 12.3.1

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

8reactions
mpawlowcommented, Jun 7, 2022
  • Experiencing the exact same problem after upgrading from redis v3.x to v4.x
  • Problem: The reconnect strategy behavior is not consistent in handling all error events (as in the previous version)
  • Problem: The specified reconnect strategy is not being invoked initially after the socket is closed unexpectedly
    • A reconnecting event is fired immediately after the connection is lost (with no delay)
  • Problem: Even more alarming is that the client goes into an infinite loop reconnect death spiral if I artificially manufacture a disconnect by temporarily disabling my Ethernet adapter
  • e.g.
[2022-06-06T21:10:56.320] [INFO ] [redis-client    ] [Reconnect Attempt # 0]: Redis server reconnect attempt initiated after 0 ms.
- Total Retry Time: 00:00:00.000 (0 ms).
- Times Connected: 4.
[2022-06-06T21:10:56.324] [ERROR] [redis-client    ] Redis server error encountered:
- Class: Error.
- Message: getaddrinfo ENOTFOUND redis-server-e764038b.databases.appdomain.cloud
- Code: ENOTFOUND.
- Stack: Error: getaddrinfo ENOTFOUND redis-server-e764038b.databases.appdomain.cloud
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26).
[2022-06-06T21:10:56.325] [ERROR] [redis-client    ] Redis Error:
{
   "errno": -3008,
   "code": "ENOTFOUND",
   "syscall": "getaddrinfo",
   "hostname": "redis-server-e764038b.databases.appdomain.cloud"
}

2reactions
Algirdyzcommented, May 13, 2022

So to me sounds like reconnectstrategy should be incorporated into this error flow as well. No?

EDIT: Anyway I did more testing and it seems that literally just changing the port causes the error to go from ECONNREFUSED to ECONNRESET. So I disabled my local redis service and stopped kubernetes portforwarding. So neither of those ports have anything running on them.

When I run the same piece of code you provided just adding localhost as host and either 6379 or 7000 as port. I get ECONNREFUSED on 6379 and ECONNRESET on 7000

I started investigating and found out that there is something called afs-fileserver ruinning on my PC on port 7000. On a mac it is Airplay Receiver apparently… I disabled it and now I get ECONNREFUSED and it uses reconnectstrategy.

However, that makes me think that node-redis does not handle other kinds of disconnects properly? When can ECONNRESET/Socket closed unexpectedly happen? Should I implement ways to recover from that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Proper Way To Connect Redis and Node.js
Some events will be fired according to the state of the connection. It' s better to listen them to take actions. Because this...
Read more >
Lettuce Reference Guide
Redis connections are designed to be long-lived and thread-safe, and if the connection is lost will reconnect until close() is called. Pending ...
Read more >
Setting time interval in HTML5 server sent events
With server sent events it is always for the browser to reconnect & the frequency is determined by the retry interval. actually, a...
Read more >
How Do You Deal With Difficult Employees? - SHRM
Or the worker whose manager never helped set her up for success or who put her in a no-win situation? Is it the...
Read more >
TIP 57 Trauma-Informed Care in Behavioral Health Services
in the public domain and may be reproduced or copied without permission from SAMHSA ... Strategy #2: Use Trauma-Informed Principles in Strategic Planning...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found