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.
Using the consistent hash implementation is simple and intuitive. Here are the three ways you can construct a consistent hash:
-
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})
-
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'])
-
Using a single node as a string:
con_hash = ConsistentHash('192.168.0.101:11212')
You can add additional nodes to the hash ring:
con_hash.add_nodes({'192.168.0.104:11212':1})
Retrieve a server for a given key:
server = con_hash.get_node('my_key')
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'])
You can run the demo test file present in the repo so see the Consistant Hash project working:
python test.py