redis-clustr
This module is a relatively thin wrapper around the node redis client to enable use of Redis Cluster. It tries to be as unobtrusive as possible - mimicing the behaviour of the node_redis client.
Usage
var RedisClustr = require('redis-clustr');
var redis = new RedisClustr({
servers: [
{
host: '127.0.0.1',
port: 7000
}
]
});
redis.set('key', 'value');
Servers
Servers in the cluster will be automatically connected to (via the response of cluster slots
). Of course, to allow discovery there must be at least one server specified in the configuration.
Client creation
By default, clients will be created using Redis.createClient(port, host)
. This can be overridden by providing a function which must return a node_redis client. Clients are cached so only one connection will be made to each server.
var RedisClustr = require('redis-clustr');
var RedisClient = require('redis');
var redis = new RedisClustr({
servers: [...],
createClient: function(port, host) {
return RedisClient.createClient(port, host);
}
});
Options
var RedisClustr = require('redis-clustr');
var redis = new RedisClustr({
servers: [...],
slotInterval: 1000,
maxQueueLength: 100,
queueShift: false,
wait: 5000,
slaves: 'share',
createClient: function(port, host, options) {
return require('redis').createClient(port, host, options);
},
redisOptions: {
retry_max_delay: 500
}
});
Supported functionality
Slot reallocation
Supported - when a response is given with a MOVED
error, we will immediately re-issue the command on the other server and run another cluster slots
to get the new slot allocations. ASK
redirection is also supported - we wil re-issue the command without updating the slots. TRYAGAIN
responses will be retried automatically.
Multi / Exec (Batch)
Multi commands are supported but treated as a batch of commands (not an actual multi) and the response is recreated in the original order. Commands are grouped by node and sent as node_redis batches
Multi-key commands (del
, mget
and mset
)
Multi-key commands are also supported and will be split into individual commands (using a batch) then have the response recreated. Only del
, mget
and mset
are supported at the moment.
Pub/Sub
Pub/Sub is fully supported. When subscribe is used, a new client will be created (connected to a random node). This client is shared for all subscriptions.
var RedisClustr = require('redis-clustr');
var redis = new RedisClustr({...});
redis.on('message', function(channel, message) { });
redis.subscribe('my-channel', function(err) {
redis.publish('my-channel', 'have a lovely day!');
});
Errors
Just like node_redis, listen to the error
event to stop your application from crashing due to errors. Redis Clustr automatically intercepts connection errors and will try to reconnect to the server.