node-redis-failover

redis failover solution based on ZooKeeper

npm install node-redis-failover
27 downloads in the last week
38 downloads in the last month

Node Redis Failover NPM version

A redis failover solution based on ZooKeeper for Node.js

Architecture chart:

Architecture

Features

  • Automatic master/slave failover
  • Read/Write Splitting
  • All components high availability

Installation

Install with npm:

npm install -g node-redis-failover

Usage

Start redis watcher

redis-failover -h

  Usage: redis-failover -n 127.0.0.1:6379,127.0.0.1:6479 -z 172.17.5.72:2381,172.17.5.73:2381,172.17.5.74:2381

  Options:

    -h, --help                  output usage information
    -V, --version               output the version number
    -n, --nodes <nodes>         Comma-separated redis host:port pairs
    -z, --zk-servers <servers>  Comma-separated ZooKeeper host:port pairs
    -p, --password [password]   Redis password
    -c, --config [path]         Path to JSON config file
    -l, --log [path]            The log file path
    --zk-chroot [rootpath]      Path to ZooKeepers chroot
    --zk-username [username]    ZooKeepers username
    --zk-password [password]    ZooKeepers password

start a redis watcher (we recommend the wathers' count should be an odd number, and more than 3):

redis-failover -n 127.0.0.1:6379,127.0.0.1:6479 -z 172.17.5.72:2381,172.17.5.73:2381,172.17.5.74:2381

with a config file config.json (config json file support comments use json-comments):

{
  // redis nodes
  "nodes": "127.0.0.1:6379,127.0.0.1:6479,127.0.0.1:6579",
  // redis password
  //"password": "abc123",

  // redis ping timeout default 6000ms
  //"pingTimeout": 6000,

  // redis ping interval(ms). default 3000ms
  //"pingInterval": 3000,

  // the maxFailures for the redis. default 3
  //"maxFailures": 3,

  // zookeeper nodes
  "zkServers": "127.0.0.1:2181",
  // zookeeper chroot
  "zkChroot": "/test",

  // log path
  "log": "logs/",

  // pid file
  "pid": "a.pid"
}

start a watcher:

redis-failover -c config.json

start with forever

forever start -m 10 redis-failover -c config.json

Use redis in your application

install:

npm install node-redis-failover --save

example code:

var redisFailover = require('node-redis-failover');

var zookeeper = {
  servers: '192.168.1.1:2181,192.168.1.2:2181,192.168.1.3:2181',
  chroot: '/appName'
};

var redis = redisFailover.createClient(zookeeper);

redis.on('ready', function() {
  // master client
  redis.masterClient.ping(function(err, res){
    console.log('ping master', res);
  });

  // slave client
  redis.getClient('slave').ping(function(err, res) {
    console.log('ping slave', res);
  });
}); 

// catch error
redis.on('error', function(err) {
  console.warn(err);
});

redis.on('change', function() {
  console.log('redis state changed, %j', redis.redisState);
});

redis.on('masterChange', function() {
  console.log('master changed, %s', redis.masterClient);
});

License

(The MIT License)

Copyright (c) 2013 Johnny Wong wzhao23@gmail.com

Bitdeli Badge

npm loves you