redis / redis/node-redis

Error: Socket closed unexpectedly & ECONNREFUSED

Open
#2,141 7 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
TypeScript
Stars
17.6k
Forks
2k
Avg merge
2d 3h
Merged PRs (30d)
40

Description

Hi! I'm working with a Redis Cluster with four masters and four slaves distributed in two different hosts, with the following configuration:

Host 1:
Master in port 6000 (nodes 0 to 4095);
Master in port 6001 (nodes 8192 to 12287);
Slave in port 6002 (master in host 2, port 6001);
Slave in port 6003 (master in host 2, port 6000);

Host 2:
Master in port 6000 (nodes 4096 to 8191);
Master in port 6001 (nodes 12288 to 16383);
Slave in port 6002 (master in host 1, port 6001);
Slave in port 6003 (master in host 1, port 6000);

All is set that in case a master goes down, the corresponding slave takes its place. In Redis this works fine, but in node-redis once a master goes down it keeps trying to reconnect to the closed socket and ignores the new master, wich keeps printing in console the same error: connect ECONNRFUSED XXX.XXX.XXX.XXX:6000

I have the following configuration for my cluster in node-redis:

try {
        (async()=>{
            let path = [];
            for(let i in self.config.get('redis').cluster){
                let item = self.config.get('redis').cluster[i];
                path.push({
                    'url': 'redis://'+ item.host +':' + item.port
                });
            }
            self.redis = Redis.createCluster({
                rootNodes: path,
                defaults: {
                    socket: {
                        reconnectStrategy: function (times) {
                        var delay = Math.min(100 + times * 2, 2000);
                        return delay;
                        }
                    }
                },
                maxCommandRedirections: 16, 
            });

            self.redis.on('error', function(error) {
                self.logger.error("Error: " + error);
            });

            await self.redis.connect();

        })();
    } catch (ex) {
        self.logger.error("Couldn't connect to Redis Cluster ", ex);
        throw ex;
    } 

Is there anything I need to change in my configuration to stop the error from appearing and connect to the new master?
Thanks in advance for the help.

Environment:

  • Node.js Version: v16.14.0
  • Redis Server Version: 6.2.6
  • Node Redis Version: 4.1.0
  • Platform: Alpine Linux v3.15

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the createCluster configuration and its socket reconnectStrategy, then reproduce the Redis 6.2.6 failover using the four-master, four-slave topology and Node Redis 4.1.0. Trace how the client handles the closed socket and promoted master; done means it follows the new master without repeatedly reporting ECONNREFUSED.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, redis, typescript
Domain
databases, distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.