• Stars
    star
    188
  • Rank 205,563 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 12 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

another redis automatic master/slave failover solution for ruby by using built-in redis sentinel (deprecated)

Redis::Sentinel (Deprecated)

The redis gem supports sentinel from version 3.2, redis-sentinel is not necessary if you are using Redis 2.8.x or later.

Another redis automatic master/slave failover solution for ruby by using built-in redis sentinel.

It subscribes message with channel "+switch-master", when message received, it will disconnect current connection and connect to new master server.

Installation

Add this line to your application's Gemfile:

gem 'redis-sentinel'

If you are using redis-server less than 2.6.10, please use redis-sentinel 1.3.0

gem 'redis-sentinel', '~> 1.3.0'

And then execute:

$ bundle

Or install it yourself as:

$ gem install redis-sentinel

Usage

Specify the sentinel servers and master name

Redis.new(master_name: "master1", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])

Sentinels can also be specified using a URI. This URI syntax is required when using Rails.config.cache_store:

config.cache_store = :redis_store, { master_name: "master1",
                                     sentinels: ['sentinel://localhost:26379', 'sentinel://localhost:26380'] }

After doing the above, you might still see #<Redis client v3.1.0 for redis://localhost:6379/0>. This is fine because redis-sentinel will only try to connect when it is actually required.

However, if none of the sentinel servers can be reached, a Redis::CannotConnectError will be thrown.

There are two additional options:

  1. :failover_reconnect_timeout (seconds) will block for that long when redis is unreachable to give failover enough time to take place. Does not wait if not given, or time given is 0.

  2. :failover_reconnect_wait (seconds) how long to sleep after each failed reconnect during a failover event. Defaults to 0.1s.

Slaves clients

If you need it, you can get an array of Redis clients, each pointing to one of the slaves:

client = Redis.new(master_name: "master1", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])
client.slaves
# => [#<Redis client v3.0.7 for redis://127.0.0.1:6380/0>, #<Redis client v3.0.7 for redis://127.0.0.1:6381/0>]

You can also get an array of all the clients (master + slaves):

client = Redis.new(master_name: "master1", sentinels: [{host: "localhost", port: 26379}, {host: "localhost", port: 26380}])
client.all_clients
# => [#<Redis client v3.0.7 for redis://127.0.0.1:6379/0>, #<Redis client v3.0.7 for redis://127.0.0.1:6380/0>, #<Redis client v3.0.7 for redis://127.0.0.1:6381/0>]

Example

Start redis master server, listen on port 16379

$ redis-server example/redis-master.conf

Start redis slave server, listen on port 16380

$ redis-server example/redis-slave.conf

Start 2 sentinel servers

$ redis-server example/redis-sentinel1.conf --sentinel
$ redis-server example/redis-sentinel2.conf --sentinel
$ redis-server example/redis-sentinel3.conf --sentinel

Run example/test.rb, which will query value of key "foo" every second.

$ bundle exec ruby example/test.rb

You will see output "bar" every second. Let's try the failover process.

  1. Stop redis master server.
  2. You will see error message output.
  3. Redis sentinel promote redis slave server to master. During this time you will see errors instead of "bar" while the failover is happening.
  4. Then you will see correct "bar" output every second again.

Example of Failover Timeout

Run the same example code above but run:

$ bundle exec ruby example/test_wait_for_failover.rb

You will see the stream of "bar" will stop while failover is taking place and will resume once it has completed, provided that failover takes less than 30 seconds.

Authors and Contributors

https://github.com/flyerhzm/redis-sentinel/graphs/contributors

Please fork and contribute, any help in making this project better is appreciated!

This project is a member of the OSS Manifesto.

Copyright

Copyright @ 2012 - 2015 Richard Huang. See MIT-LICENSE for details

More Repositories

1

bullet

help to kill N+1 queries and unused eager loading
Ruby
7,081
star
2

rails_best_practices

a code metric tool for rails projects
Ruby
4,163
star
3

switch_user

Easily switch current user
Ruby
732
star
4

chinese_pinyin

translate chinese hanzi to pinyin
Ruby
430
star
5

activemerchant_patch_for_china

A rails plugin to add an active_merchant patch for china online payment platform including alipay (ๆ”ฏไป˜ๅฎ), 99bill (ๅฟซ้’ฑ) and tenpay (่ดขไป˜้€š)
Ruby
305
star
6

css_sprite

automatically css sprite
Ruby
242
star
7

uniform_notifier

uniform notifier for rails logger, customized logger, javascript alert, javascript console, growl and xmpp
Ruby
235
star
8

rails-bestpractices.com

HTML
200
star
9

eager_group

fix n+1 aggregate sql functions for rails
Ruby
122
star
10

seo_checker

check your website if it is seo.
Ruby
117
star
11

simple_cacheable

a simple cache implementation for rails
Ruby
91
star
12

code_analyzer

code analyzer tool which is extracted from rails_best_practices
Ruby
86
star
13

resque-restriction

resque-restriction is an extension to resque queue system that restricts the execution number of certain jobs in a period time.
Ruby
86
star
14

rfetion

rfetion is a ruby gem for China Mobile fetion service that you can send SMS free.
Ruby
61
star
15

chinese_regions

provides all chinese regions, cities and districts
Ruby
60
star
16

mongoid-eager-loading

eager loading for mongoid (DEPRECATED)
Ruby
54
star
17

rails-brakeman.com

online security check for rails projects
Ruby
52
star
18

contactlist

java api to retrieve contact list of email(hotmail, gmail, yahoo, sohu, sina, 163, 126, tom, yeah, 189 and 139) and im(msn)
Java
49
star
19

regexp_crawler

A crawler which uses regular expression to catch data from website.
Ruby
45
star
20

chinese_permalink

This plugin adds a capability for AR model to create a seo permalink with your chinese text. It will translate your chinese text to english url based on google translate.
Ruby
41
star
21

apis-bench

Ruby
34
star
22

sitemap

This plugin will generate a sitemap.xml from sitemap.rb whose format is very similar to routes.rb
Ruby
32
star
23

twitter_connect

facebook connect style twitter oauth
Ruby
30
star
24

taobao

Ruby Client Library for Taobao Open Platform
Ruby
27
star
25

huangzhimin.com

my homepage
HTML
24
star
26

railsbp.com

railsbp.com
JavaScript
23
star
27

multiple_mailers

extend actionmailer to allow one smtp account per mailer class.
Ruby
23
star
28

contactlist-client

The contactlist-client gem is a ruby client to contactlist service which retrieves contact list of email(hotmail, gmail, yahoo, sohu, sina, 163, 126, tom, yeah, 189 and 139) and im(msn)
Ruby
20
star
29

donatecn

demo for activemerchant_patch_for_china
Ruby
17
star
30

monitor

Monitor gem can display ruby methods call stack on browser based on unroller
JavaScript
17
star
31

authlogic_renren_connect

Extension of the Authlogic library to add Renren Connect support built upon the renren plugin
Ruby
5
star
32

rails3-template

rails3 template includes a lot of useful plugins/gems
Ruby
5
star
33

nodeslide

node.js related slideshows [deprecated], move to nodeslide.heroku.com
JavaScript
4
star
34

rubyslide.com

collect ruby rails related presentations [deprecated], moved to rubyslide.heroku.com
Ruby
4
star
35

visual_partial

This plugin provides a way that you can see all the partial pages rendered. So it can prevent you from using partial page too much, which hurts the performance.
Ruby
4
star
36

codelinestatistics

The code line statistics takes files and directories from GUI, counts the total files, total sizes of files, total lines, lines of codes, lines of comments and lines of blanks in the files, displays the results and can also export results to html file.
Ruby
4
star
37

clock_chrome_extension

google chrome extension to display multiple clock analogs for multiple timezones
2
star
38

dotfiles

Vim Script
2
star
39

showoff-understanding-mongoid

My understanding mongoid showoff presentation
Ruby
2
star
40

enough_fields

only select specified fields used
Ruby
2
star
41

skype_archive

company hackathon
Ruby
1
star
42

bullet_test

Ruby
1
star
43

play_skype

JavaScript
1
star
44

test_code_analyzer

test code for code_analyzer gem
Ruby
1
star
45

blog.huangzhimin.com

HTML
1
star
46

test_error

raise an error to test if exception_notification really works.
Ruby
1
star
47

try-ripper

code mirror of try-ripper.heroku.com
CSS
1
star