Skip to content

Anurag-Varma/consistent-hashing-load-balancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Consistent Hashing

This Python package implements consistent hashing, an algorithm that helps distribute keys (or requests) across a changing set of server nodes. It's useful when the number of servers can dynamically increase or decrease. The algorithm used is based on libketama.

Usage

Using the consistent hash implementation is simple and intuitive. Here are the three ways you can construct a consistent hash:

  1. Using a dictionary with nodes and weights:

    from consistent_hash import ConsistentHash
    
    con_hash = ConsistentHash({'192.168.0.101:11212':1, '192.168.0.102:11212':2, '192.168.0.103:11212':1})
  2. Using a list of nodes (without weights):

    con_hash = ConsistentHash(['192.168.0.101:11212', '192.168.0.102:11212', '192.168.0.103:11212'])
  3. Using a single node as a string:

    con_hash = ConsistentHash('192.168.0.101:11212')

Add servers to the hash ring

You can add additional nodes to the hash ring:

con_hash.add_nodes({'192.168.0.104:11212':1})

Get a server for a specific key

Retrieve a server for a given key:

server = con_hash.get_node('my_key')

Remove servers from the hash ring

Remove nodes from the hash ring. No need to specify the weights:

con_hash.remove_nodes(['192.168.0.102:11212', '192.168.0.104:11212'])

Demo testing

You can run the demo test file present in the repo so see the Consistant Hash project working:

python test.py

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages