# Consistent Hashing
- Wikipedia: https://en.wikipedia.org/wiki/Consistent_hashing
## What is it good for
Reliable load balancing!
Consistent Hashing is important for load balancing in a distributed request processing environment (servers and databases). When load must be distributed evenly, the modulo operation can be used, but it's an expensive process to re-allocate if a node suddenly goes offline.
Consistent hashing is an alternative approach to load balancing which assigns each node a unique identifier or "hash" value. When a new node is added or a node goes offline, only a few keys need to be re-mapped instead of rebalancing the entire system. This approach reduces the amount of work required to rebalance the system and makes it more efficient.
Let's imagine we are writing a python server to route traffic, using a consistent hashing algorithm:
```python
import hashlib
# Create a list of all the servers in the cluster- pretend they're objects with the methods used below
servers = [server1, server2, server3]
# Define a method to generate hashes of the server's IP address
def get_hash(data):
return int(hashlib.sha1(data).hexdigest(), 16)
# Define a method to map the server's IP address to a unit circle
def map_ip_to_circle(ip_address):
return get_hash(ip_address) % 360
# Define a method to find the server that the BLOB should be routed to
def get_server_for_blob(blob):
# Get the hash of the BLOB
blob_hash = get_hash(blob)
# Iterate through the servers in clockwise order
for server in servers:
# Get the hash of the server's IP address
server_hash = map_ip_to_circle(server.ip_address)
# If the server's hash is greater than the BLOB's hash,
# then the BLOB should be routed to this server
if server_hash > blob_hash:
return server
# Route the BLOB to the appropriate server
server = get_server_for_blob(blob)
```

This code example shows how to use consistent hashing to route traffic in a cluster of servers. The code first creates a list of servers, then it creates a method to generate hashes of the server's IP address. It then defines a method to map the server's IP address to a unit circle, and finally it defines a method to find the server that the BLOB should be routed to. Finally, the code routes the BLOB to the appropriate server.