How can I integrate socket.io, and a master-slave Redis configuration with automatic failover?
Asked
Active
Viewed 1,763 times
1 Answers
0
I've used the following configuration:
- Create a master redis instance.
- Create a trio of redis-sentinel-client processes and point them to the master redis instance. You only need to reference one of them in the configuration below, because the client will fill in the rest.
- Create a slave redis instance, and point it to the master.
Use the following to configure your RedisStore with socket.io:
var redisOptions = {
host: 'localhost', // || redisSentinelHost,
port: 26379, // Default sentinel port.
masterName: 'mymaster'
};
var RedisStore = require('socket.io/lib/stores/redis'),
// The redis-sentinel-client requires a forked redis client
// that is available here:
redis = require('redis-sentinel-client/node_modules/redis'),
// A sentinel client is required to back each of the redis
// clients below. The sentinel clients handle the fail-overs.
sentinel = require('redis-sentinel-client'),
redisPubSentinel = sentinel.createClient(redisOptions),
redisSubSentinel = sentinel.createClient(redisOptions),
redisClientSentinel = sentinel.createClient(redisOptions),
redisPub = redisClientSentinel.getMaster(),
redisSub = redisSubSentinel.getMaster(),
redisClient = redisPubSentinel.getMaster();
// We must be robust to connection errors in order to allow
// for a reconnect. We therefore prevent redis client errors
// from stopping the application.
[ redisPubSentinel,
redisSubSentinel,
redisClientSentinel,
redisPub,
redisSub,
redisClient ].forEach(function(c) {
c.on('error', function(err) {
logger.log(err);
});
});
io.set('store', new RedisStore({
redis: redis,
redisPub: redisPub,
redisSub: redisSub,
redisClient: redisClient
})
);

schwerwolf
- 250
- 1
- 7
- 20