Packages
raft_fleet
0.1.13
0.12.0
0.11.0
0.10.2
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.1
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
A fleet of Raft consensus groups
Current section
Files
Jump to
Current section
Files
raft_fleet
README.md
README.md
# RaftFleet
Elixir library to run multiple [Raft](https://raft.github.io/) consensus groups in a cluster of Erlang VMs
- [API Documentation](http://hexdocs.pm/raft_fleet/)
- [Hex package information](https://hex.pm/packages/raft_fleet)
[](https://hex.pm/packages/raft_fleet)
[](https://travis-ci.org/skirino/raft_fleet)
[](https://coveralls.io/github/skirino/raft_fleet?branch=master)
## Feature & Design
- Reasonably scalable placement of processes for multiple Raft consensus groups
- consensus member processes are distributed to ErlangVMs in a data center-aware way using [randezvous hashing](https://en.wikipedia.org/wiki/Rendezvous_hashing)
- automatic rebalancing on adding/removing nodes
- Each consensus group leader is accessible using the name of the consensus group (which must be an atom)
- Process IDs of consensus leaders are cached in a local ETS table
- [rafted_value](https://github.com/skirino/rafted_value) as a building block
## Example
Suppose we have a cluster of 4 erlang nodes:
```ex
$ iex --sname 1 -S mix
iex(1@skirino-Manjaro)>
$ iex --sname 2 -S mix
iex(2@skirino-Manjaro)> Node.connect :"1@skirino-Manjaro"
$ iex --sname 3 -S mix
iex(3@skirino-Manjaro)> Node.connect :"1@skirino-Manjaro"
$ iex --sname 4 -S mix
iex(4@skirino-Manjaro)> Node.connect :"1@skirino-Manjaro"
```
Load the following module that implements `RaftedValue.Data` behaviour on all nodes in the cluster.
```ex
defmodule JustAnInt do
@behaviour RaftedValue.Data
def new, do: 0
def command(i, {:set, j}), do: {i, j }
def command(i, :inc ), do: {i, i + 1}
def query(i, :get), do: i
end
```
Call `RaftFleet.activate/1` on all nodes.
```ex
iex(1@skirino-Manjaro)> RaftFleet.activate("zone1")
iex(2@skirino-Manjaro)> RaftFleet.activate("zone2")
iex(3@skirino-Manjaro)> RaftFleet.activate("zone1")
iex(4@skirino-Manjaro)> RaftFleet.activate("zone2")
```
Create 5 consensus groups each of which replicates an integer and has 3 consensus members.
```ex
iex(1@skirino-Manjaro)> rv_config = RaftedValue.make_config(JustAnInt)
iex(1@skirino-Manjaro)> RaftFleet.add_consensus_group(:consensus1, 3, rv_config)
iex(1@skirino-Manjaro)> RaftFleet.add_consensus_group(:consensus2, 3, rv_config)
iex(1@skirino-Manjaro)> RaftFleet.add_consensus_group(:consensus3, 3, rv_config)
iex(1@skirino-Manjaro)> RaftFleet.add_consensus_group(:consensus4, 3, rv_config)
iex(1@skirino-Manjaro)> RaftFleet.add_consensus_group(:consensus5, 3, rv_config)
```
Now we can run query/command from any node in the cluster:
```ex
iex(1@skirino-Manjaro)> RaftFleet.query(:consensus1, :get)
{:ok, 0}
iex(2@skirino-Manjaro)> RaftFleet.command(:consensus1, :inc)
{:ok, 0}
iex(3@skirino-Manjaro)> RaftFleet.query(:consensus1, :get)
{:ok, 1}
```
Adding/removing nodes triggers rebalancing of consensus member processes.