Skip to content Skip to sidebar Skip to footer

How To Create A Redis Connection With Master And Slave

I am trying to do a Redis connection, I have a 'master' port and two slaves. I want to do this with a Sentinel. My actually code to connect redis is actually deprecated, I think s

Solution 1:

I finally got it done and it works nice.

I will let you here my code, I hope this helps others!

var Redis = require('ioredis');
// preferredSlaves array format
var preferredSlaves = [
  { ip: config.redis_slave_1, port: config.redis_port, prio: 1 },
  { ip: config.redis_slave_2, port: config.redis_port, prio: 2 }
];
var redis = new Redis({
  port: config.redis_port,
  host: config.redis_host,
  sentinels: [{ host: config.redis_sentinel_1, port: config.redis_sentinel }, { host: config.redis_sentinel_2, port: config.redis_sentinel }, { host: config.redis_sentinel_3, port: config.redis_sentinel }],
  name: config.redis_master_name,
  password: config.redis_password,
  preferredSlaves: preferredSlaves
});

redis.on('connect', function(err, res) {
  logger.info('Connected to Redis ' + process.pid);
  redisIsReady = true;
  console.log('Connected to Redis ' + process.pid + " REDIS " + JSON.stringify(redis.options) )
});

redis.on('error', function(err) {
  logger.error('Error connecting to Redis ' + process.pid);
  redisIsReady = false;
  console.log('error to Redis ' + err)
});

Thanks for all your responses,

Regards!!

Post a Comment for "How To Create A Redis Connection With Master And Slave"