A consistent hash-ring implemention leveraging the excellent C hash-ring lib by Chris Moos for Elixir.
- Elixir 0.15.1 or newer
Add HashRing as a dependency in your mix.exs
file
defp deps do
[
{hash_ring_ex: "~> 1.0"}
]
end
Then run mix deps.get
in your shell to fetch the dependencies.
Start or supervise a new HashRing with HashRing.start_link/1
or HashRing.start/1
and add some nodes
{:ok, pid} = HashRing.start_link
:ok = HashRing.add(pid, "first_node")
:ok = HashRing.add(pid, "second_node")
Then find the appropriate node for your key
{:ok, "first_node"} = HashRing.find(pid, "my_key")
Nodes can also be easily dropped
:ok = HashRing.drop(pid, "first_node")
{:ok, "second_node"} = HashRing.find(pid, "my_key")
Started rings will use an MD5 hash function by default. In tests MD5 is on average about 25% faster than sha1. The hashing function to use can be specified as an option sent to start_link/1 or
start/1`
{:ok, md5} = HashRing.start_link(hash_func: :md5)
{:ok, sha1} = HashRing.start_link(hash_func: :sha1)
The number of replicas can be configured, too (default: 128)
{:ok, pid} = HashRing.start_link(replicas: 5)
And the standard GenServer options can be passed in, too
{:ok, pid} = HashRing.start_link(name: :my_hash_ring)
Jamie Winsor ([email protected])