4

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

Max
  • 5,493
  • 4
  • 26
  • 42
  • What version of ioredis do you have installed? – michelem Aug 19 '15 at 07:21
  • @Michelem It's 1.7.5 Which is latest – Max Aug 19 '15 at 10:55
  • What version of Redis are you running (should be 3+) and have you configured the Redis cluster properly? – Itamar Haber Aug 19 '15 at 10:57
  • @ItamarHaber Haber It looks like it is old 2.8.6. So do i need to update it or any to keep using this? I did a click to deploy in google compute they do the cluster setup. http://prntscr.com/86ff7b – Max Aug 19 '15 at 13:18
  • Sorry, my mistake, you probably don't need to upgrade, was thinking about the other cluster. Anyway, IIUC, you don't need to use the ioredis.Cluster thing - just use the master's endpoint for your connections (sub could be to either of the slaves as well). – Itamar Haber Aug 19 '15 at 14:20
  • How would i know which is master and what if master changed? – Max Aug 20 '15 at 05:21

1 Answers1

9

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.

Didier Spezia
  • 63,324
  • 10
  • 166
  • 145
  • How can I get the master name? – Max Aug 24 '15 at 08:54
  • Supposing you can connect to any sentinel instance using redis-cli, command SENTINEL MASTERS will return a list of configured masters. I expect only one to be defined here. – Didier Spezia Aug 24 '15 at 09:03
  • This won't work. It says unknown command. My redis version is 2.8.6. http://prntscr.com/888wvy – Max Aug 24 '15 at 10:06
  • Yo It worked ! I earlier try that sentinel code but i was using port 6379 which is not sentinel port. Thanks!! – Max Aug 24 '15 at 10:43
  • Ok, so the name of the master is just 'master' - you should be able to connect from node.js with that. – Didier Spezia Aug 24 '15 at 10:56
  • Hi, I am using same code as above and using ioredis, but not connecting to redis server. And It is not throwing any event. Not able to investigate the problem. How to fix it ? – rajeshpanwar Jul 29 '16 at 13:36