multiple-redis



Run redis commands against multiple redis instances.
Overview
This library enables to submit redis commands to multiple redis instances.
The client interface is the same as the 'redis' node package at: https://github.com/NodeRedis/node_redis
However, every command actually invokes multiple redis backends.
When To Use
Generally in production you would like a failover capability for your redis server.
Working with only 1 redis instance can cause your entire production system to fail in case redis goes down for any reason.
For example, when using redis as an express session store and redis is down, users HTTP requests will be rejected.
Redis does come with a built in failover and clustering capabilities via master/slave solution and monitoring via redis sentinel.
However, those solutions sometimes might cause other issues, for example, express redis works with only 1 redis client.
If that redis client is not available, it will not failover to the slave redis.
There are other libraries to resolve the issue, but the basic library does not provide any solution.
Other issues might be if you failover to the slave redis but it is only readonly mode (by default slave redis is read only).
This does not mean that I do not support the Redis master/slave + sentinal solution but sometimes using multiple
independent Redis servers for none critical data serves as a better solution which is much more simple to deploy and manage in production.
How This Library Works
This library basically does 2 main things.
- Get commands - When a get (which does not modify data) command is called, the redis client will go redis by redis in a sequence until
it finds a redis which provides data for the get command.
Any error, or any redis which is unable to provide the data is ignored.
Once a specific redis provides the data, the next redis servers are skipped and the command callback is invoked with that data. - Set/Other commands - When a non get command is called, all redis servers are invoked in parallel with the same command to
ensure that all redis servers are updated.
If any redis server was able to process the command, the original command callback will receive a valid response.
This means that at least once redis server needs to work good for the main client to notify the calling code that everything works ok.
A side affect of this solution is that every publish event would be received multiple times by the subscribers,
so you need to code accordingly and prevent any possible issues.
Simple Scenario
Let us take the express redis session store as an example.
Since this library provides the same redis interface as the common redis client, you can provide this library to the
redis session store.
When a new session is created, it will be created in all redis servers (in this example, lets assume we have 2).
In case the first redis server suddenly fails, the session store is still able to fetch and update the session data from the
second redis server.
When the first redis server comes back up, the session is still available to the session store from the second redis server and
any session modification (due to some HTTP request) will cause both redis servers to now hold the latest express session data.
It is by no means, not a perfect solution, but it does has its advantages.
First and foremost, its simple deployment requirements.
Usage
In order to use this library, you need to either create the redis clients or provide the redis connection data as follows:
var redis = require('redis');
var client1 = redis.createClient(...);
var client2 = redis.createClient(...);
var MultipleRedis = require('multiple-redis');
var multiClient = MultipleRedis.createClient([client1, client2]);
multiClient.set('string key', 'string val', callback);
Or
var MultipleRedis = require('multiple-redis');
var multiClient = MultipleRedis.createClient([{
host: 'host1',
port: 6379
}, {
host: 'host2',
port: 6379
}], options);
multiClient.set('string key', 'string val', callback);
The rest of the API is the same as defined in the redis node library: https://github.com/NodeRedis/node_redis#api
Installation
In order to use this library, just run the following npm install command:
npm install --save multiple-redis
Limitations
Not all of the original redis client attributes are available (for example: redis.debug_mode).
Also Multi is currently not supported.
API Documentation
See full docs at: API Docs
Release History
Date | Version | Description |
---|
2015-09-06 | v0.0.5 | Maintenance |
2015-09-03 | v0.0.3 | Added support for connected and server_info attributes. |
2015-09-03 | v0.0.2 | Initial release. |
License
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.