Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection to Redis cluster failed

I have setup Redis cluster in Google compute Engine by click to deploy option. Now i want to connect to this redis server from my node js code using 'ioredis' here is my code to connect to single instance of redis

var Redis = require("ioredis");

var store = new Redis(6379, 'redis-ob0g');//to store the keys
var pub =   new Redis(6379, 'redis-ob0g');//to publish a message to all workers
var sub =   new Redis(6379, 'redis-ob0g');//to subscribe a message 

var onError = function (err) {
    console.log('fail to connect to redis ',err);
};
store.on('error',onError);
pub.on('error',onError);
sub.on('error',onError);

And it worked. Now i want to connect to redis as cluster, so i change the code as

/**
 * list of server in replica set
 * @type {{port: number, host: string}[]}
 */
var nodes =[
    {   port: port,    host: hostMaster},
    {   port: port,    host: hostSlab1},
    {   port: port,    host: hostSlab2}
];
var store =  new Redis.Cluster(nodes);//to store the keys
var pub =   new Redis.Cluster(nodes);//to publish a message to all workers
var sub =    new Redis.Cluster(nodes);//to subscribe a message channel

Now it throw this error: enter image description here

Here is my Redis cluster in my google compute console:

enter image description here

like image 463
Max Avatar asked Jan 08 '23 17:01

Max


1 Answers

Ok, I think there is a confusion here.

A Redis Cluster deployment is not the same than a number of standard Redis instances protected by Sentinel. Two very different things.

The click-to-deploy option of GCE deploys a number of standard Redis instances protected by Sentinel, not Redis Cluster.

ioredis can handle both kind of deployments, but you have to use the corresponding API. Here, you were trying to use the Redis Cluster API, resulting in this error (cluster related commands are not activated for standard Redis instances).

According to ioredis documentation, you are supposed to connect with:

var redis = new Redis({
    sentinels: [{ host: hostMaster, port: 26379 },
                { host: hostSlab1, port: 26379 },
                { host: hostSlab2, port: 26379 } ],
    name: 'mymaster'
});

Of course, check the sentinel ports and name of the master. ioredis will manage automatically the switch to a slave instance when the master fails, and sentinel will ensure the slave is promoted as master just before.

Note that since you use pub/sub, you will need several redis connections.

like image 182
Didier Spezia Avatar answered Jan 11 '23 15:01

Didier Spezia