A distributed hash table (DHT) is a class of a decentralized distributed system that provides a lookup service similar to a hash table, in which (key, value) pairs are are stored and retrieved efficiently.
The DHT scales up by partitioning the hashed key
space between the nodes. Each (key, value) set is stored on two replicas for redundancy.
In case of failures two replicas may diverge. Conflicts are handled on read get/1
by picking an arbitrary value and updating the diverging replica.
The application is standalone. To run execute:
mix deps.get
iex --name [email protected] -S mix
Other nodes will automatically attempt to connect to [email protected]
on launch.
In order to set a value 1
in the DHT for the key :foo
use the set/2
function.
iex(node1@127.0.0.1)1> Dht.Service.set(:foo, 1)
true
Use get/1
to get the value for the key and if the values do not match it will resolve the conflict and update the replicas.
iex(node1@127.0.0.1)4> Dht.Service.get(:foo)
1
18:36:43.532 [info] Updated replica #PID<15428.208.0> with {:foo, 1}
The next get/1
will not show a conflict.
iex(node1@127.0.0.1)5> Dht.Service.get(:foo)
1