Giter VIP home page Giter VIP logo

hash_ring's Introduction

hash_ring

hex.pm version Build Status Code Coverage License: MIT

An implementation of consistent hashing algorithm.

This algorithm determines which nodes handles which items by hashing.

Consisntent hashing is suitable to manage nodes in environments in which nodes dynamically joins and leaves.

For example, if a node leaves the cluster, the items handled by the node should be reassigned to other nodes. But other items can remain in the current nodes. Thus in average case, only 1/N items are affected by the leaving (where 'N' is the number of nodes in the cluster).

See Reference for more information.

Build

To build the library for stand-alone usage:

$ git clone https://github.com/sile/hash_ring.git
$ cd hash_ring
$ rebar3 compile
$ rebar3 shell
$ > hash_ring:module_info().

If you want to use from your application:

%% In your 'rebar.config'

%% Add following lines
{deps,
 [
   hash_ring
 ]}.

Example

%% Builds a consistent hash ring
> Nodes = hash_ring:list_to_nodes([a,b,c,d,e]).
[{hash_ring_node,a,a,1},
 {hash_ring_node,b,b,1},
 {hash_ring_node,c,c,1},
 {hash_ring_node,d,d,1},
 {hash_ring_node,e,e,1}]

> Ring0 = hash_ring:make(Nodes).

%% Finds the node which handles the item
> hash_ring:find_node(item_1, Ring0).
{ok,{hash_ring_node,c,c,1}}

%% Collects four nodes in descending order of priority
> hash_ring:collect_nodes(item_1, 4, Ring0).
[{hash_ring_node,c,c,1},
 {hash_ring_node,e,e,1},
 {hash_ring_node,b,b,1},
 {hash_ring_node,d,d,1}]

%% Addition of a node
> Ring1 = hash_ring:add_node(hash_ring_node:make(g), Ring0).
> hash_ring:collect_nodes(item_1, 4, Ring1).
[{hash_ring_node,c,c,1},
 {hash_ring_node,e,e,1},
 {hash_ring_node,b,b,1},
 {hash_ring_node,g,g,1}] % The fourth node is changed from 'd' to 'g'

%% Removal of a node
> Ring2 = hash_ring:remove_node(c, Ring1).
> hash_ring:collect_nodes(item_1, 4, Ring2).
[{hash_ring_node,e,e,1}, % 'c' is removed but the remaining order is unchanged
 {hash_ring_node,b,b,1},
 {hash_ring_node,g,g,1},
 {hash_ring_node,d,d,1}]

API

See EDoc Documents

Reference

Benchmark

A simple benchmark result for a 500 nodes ring.

Environment

  • OS: Ubuntu 15.10
  • CPU: Intel(R) Core(TM) i7-6600U CPU @ 2.60GHz
  • Erlang/OTP: OTP18.2 (package)

Result

A result of hash_ring_bench:bench(500, []).

Toal Time and Heap Size

module make_time add_time remove_time find_time heap_size
hash_ring_static 178 ms 7166 ms 2162 ms 0.722 ms 1406 KB
hash_ring_dynamic 396 ms 381 ms 367 ms 1.141 ms 6191 KB

Average Time per Node (or Item)

module add_time remove_time find_time
hash_ring_static 14.332 ms 4.323 ms 0.00144 ms
hash_ring_dynamic 0.762 ms 0.734 ms 0.00228 ms

License

This library is released under the MIT License. See the LICENSE file for full license information.

hash_ring's People

Contributors

paulo-ferraz-oliveira avatar sile avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

hash_ring's Issues

Request deletion of branch develop

This branch (develop) is mainly left untouched and doesn't "follow" master. Do avoid further confusion regarding its existence, could it simply be deleted? Many thanks.

Release request

Hi.

Any chance we could get a release from master soon? We could benefit from the covertool related changes to rebar.config.

Thanks.

Move from Travis CI to GitHub Actions?

Would you be interested in a pull request that moves CI from Travis CI to GitHub Actions, or at least adds GitHub Actions alongside Travis CI? It seems travis-ci.org will stop being useful from Dec 31 2020. I can do it, except that I think you need to create the workflow yml, first, in master, after which I can branch off of.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.