An implementation of the Raft consensus algorithm on top of Celluloid.
Add this line to your application's Gemfile:
gem 'floss'And then execute:
$ bundleOr install it yourself as:
$ gem install flossWe're going to implement a distributed counter. While not very useful, it's a good enough demonstration of what you can
do with this library. Let's start with the counter service. It accepts three commands, get, reset and increase.
The first simply returns the current count, the second sets the count to zero and the third changes the current count,
optionally by a given amount.
class Counter
  attr_accessor :count
  def initialize
    self.count = 0
  end
  def get
    count
  end
  def reset
    self.count = 0
  end
  def increase(amount = 1)
    self.count += amount
  end
endTo increase reliability of your counter, you decide to distribute it across multiple machines. This is where floss
comes into play. To simplify this demonstration, we're going to start multiple nodes in the same process.
addresses = [10001, 10002, 10003].map { |port| "tcp://127.0.0.1:#{port}" }
$nodes = addresses.size.times.map do |i|
  combination = addresses.rotate(i)
  options = {id: combination.first, peers: combination[1..-1]}
  Floss::Proxy.new(Counter.new, options)
end
# Give your nodes some time to start up.
$nodes.each(&:wait_until_ready)Now we're ready to play with our distributed counter.
def random_node; $nodes.sample; end
random_node.get # => 0
random_node.increase # => 1
random_node.get # => 1That was easy wasn't it? Let's see what happens if the cluster is damaged.
# Terminate a random node in the cluster.
doomed_node = $nodes.delete(random_node)
doomed_node_id = doomed_node.id
doomed_node.terminate
random_node.increase # => 2Your cluster still works. If you'd kill another one, executing a command would result in an error because insufficient nodes are available to ensure your system's consistency.
- Fork it
 - Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request