Repository navigation
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
Activity
KunalBurangi commentedon Jul 4, 2022
I don't think it is a problem with master and slave.
this has been a long-running problem where no one knows why this happens. They just say that Redis will reconnect automatically.
But I think this is very annoying.
jleppert commentedon Jul 7, 2022
I'm having the same issues, what is worse there is no good way to handle the exception, the reconnect stuff just plain doesn't work. Until its fixed, there is no way to use node-redis in any production capacity.
deegoy2 commentedon May 12, 2023
It took me several hours to reach here, only to know it's not fixable.
I'm trying to maintain connections to multiple independent Redis instances and one connection failure takes over all the processing and it goes into the following loop:
Does the below config even work?
reconnectStrategy: retries => falseAnechaS commentedon Jun 7, 2023
The reconnection of createCluster has an issue, but there is a simple solution to fix it.
dtikhonov commentedon Jun 8, 2023
I cannot reproduce this failure.
node-redis 4.6.6
Node.js 16.4.2
Redis 7.0.11
My code:
Output:
It's a bit weird that there are two successful Redis calls after the socket error (that's when I killed one of the master nodes), but you can see that it reconnected just fine once I brought that node back up.
scorpionknifes commentedon Feb 6, 2024
I can only reproduce this inside k8s (update: I was also able to reproduce this in docker)
I'm running a redis-cluster with bitnami
I run the following + code from above in a pod:
After killing a master redis pod that the client was connected to
Output:
This loops forever until I restart the pod (the only way to fix this)
When I kill a redis pod a new pod spins up with a new IP address.
I'm assuming node-redis doesn't like it when a node IP address is unreachable?
This makes it impossible to run this in production in k8s.
fix redis#2141 - cluster redirect if client closed
scorpionknifes commentedon Feb 8, 2024
I've drafted out a fix #2701 for my use case.
I've written it with my limited abilities, appreciate it if someone can rewrite it. 🙏
fix(cluster): handle socket disconnections and MOVED redirections dur…
github-actions commentedon Oct 10, 2026
This issue has been automatically marked as stale due to inactivity.
It will be closed in 30 days if no further activity occurs.
If you believe this issue is still relevant, please add a comment to keep it open.
github-actions commentedon Nov 10, 2026
This issue has been automatically closed due to inactivity.
If you believe this issue is still relevant, please reopen it or create a new issue with updated information.