Build Status Code Climate security Gitter

Elected - A ruby distributed leader election through Redis locks.

Elect a leader out of many processes and run code only in one of your servers at a time.

This library depends heavily on the redlock ruby gem to obtain distributed locks through Redis.

Elected is a Spanglish-fluent gem so expect Spanish and English names all over. Pardon my French!

Installation

Add this line to your application's Gemfile:

gem 'elected'

And then execute:

$ bundle

Or install it yourself as:

$ gem install elected

Documentation

RubyDoc

Usage example

  # Config your redis urls or use the default REDIS_URL environment variable by default.
  Elected.redis_urls = ['redis://localhost:6379', 'redis://someotherhost:6379']
  # Create a leader manager with a key and timeout (in milliseconds)
  mgr = Elected::Senado.new 'my_app_name:stage', 30_000
  # Are we the leaders?
  mgr.leader?
  => true

  # Then in some other app/shell
  mgr = Elected::Senado.new 'my_app_name:stage', 30_000
  mgr.leader?
  => false
  # But if we wait over 30 seconds
  mgr.leader?
  => true

Elected, like Redlock, works seamlessly with redis sentinel, which is supported in redis 3.2+. It also allows clients to set any other arbitrary options on the Redis connection, e.g. password, driver, and more.

Elected.redis_urls= [ 'redis://localhost:6379', Redis.new(:url => 'redis://someotherhost:6379') ]
Elected.electorado

Run tests

Make sure you have at least 1 redis instances up.

$ rake rspec

Disclaimer

The hard work of securing a distributed lock is all done through the great redlock gem. Thanks to Leandro Moreira for his hard work. This code, thanks to Redlock, implements an algorithm which is currently a proposal, it was not formally analyzed. Make sure to understand how it works before using it in your production environments. You can see discussion about this approach at reddit.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/simple-finance/elected. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.