Sub topic 2
What we need from the hash function
This chapter assumes you know what a hash function is, can read simple Python, and have seen the modulo scheme hash(key) mod N for picking a node. The hash ring in later chapters depends on one component more than any other: the hash function. A poor choice makes the whole scheme unreliable.
Two jobs for one function
Consistent hashing uses the hash function twice. It hashes each key to a number, and it hashes each node (or a node's virtual positions) to a number. Both numbers live in the same range. Lookup compares them, so the two hashes must come from the same function.
Property 1: determinism
A hash function is deterministic if the same input always gives the same output. Every client, on every machine, at every time, must compute the same number for user:42. If two clients disagree, they send the same key to different nodes, and cache hits turn into misses.
Determinism is easy to break by accident in Python. The built-in hash() on strings is salted per process. The salt changes on every start unless PYTHONHASHSEED is fixed. Two processes can therefore disagree about the same string. Use hashlib, which gives stable output.
import hashlib
def h(key):
return int(hashlib.md5(key.encode()).hexdigest(), 16)
print(h("user:42"))
print(h("user:42"))Both lines print the same 128-bit integer, on any machine and in any run.
Property 2: uniformity
A hash function is uniform if its outputs are spread evenly over the whole range. Evenness matters because the ring assumes that keys land at random positions. If outputs clump, some nodes receive far more keys than others, whatever else you do.
The next program compares h with a deliberately bad function that uses only the key length. It counts how many of 10,000 keys land on each of 4 nodes.
import hashlib
from collections import Counter
def h(key):
return int(hashlib.md5(key.encode()).hexdigest(), 16)
def bad_hash(key):
return len(key)
keys = [f"key{i}" for i in range(10000)]
print(sorted(Counter(h(k) % 4 for k in keys).items()))
print(sorted(Counter(bad_hash(k) % 4 for k in keys).items()))The first line shows four counts, each close to 2,500. The second prints exactly [(0, 10), (1, 90), (2, 900), (3, 9000)]. Key lengths run from 4 to 7 characters, and most keys have 7, so one node gets 90% of the load.
Property 3: avalanche behavior
A good hash function also has the avalanche property. Changing one bit of the input changes about half of the output bits. Real keys are often sequential, such as key1, key2 and key3. Without avalanche, such keys would sit next to each other on the ring and land on the same node. With it, neighbors in key space become unrelated positions in hash space.
What we do not need
Consistent hashing does not need a secure hash. We don't need resistance to collision attacks or the ability to hide the input. MD5 is broken for security purposes, but it is still fine here because we only need speed, determinism, and spread. Non-cryptographic hashes such as MurmurHash3 or xxHash are common in production and run faster.
| Property | Needed for consistent hashing? | Why |
|---|---|---|
| Deterministic | Yes | All clients must agree on a key's position |
| Uniform | Yes | Even spread gives even load |
| Avalanche | Yes | Similar keys must not cluster |
| Fast | Yes | A hash runs on every lookup |
| Wide output range | Yes | Fewer collisions between positions |
| Cryptographically secure | Usually no | No attacker model in the basic design |
Common errors
- Hashing keys with one function and node names with another. The positions then share no common space.
- Using a hash with a small output range, such as a 16-bit value. Many virtual nodes will then collide at the same position.
- Hashing only a short or patterned part of the key, such as a shared prefix. This brings back the clumping seen in
bad_hash.
Exercise
A teammate writes a ring in Python and uses hash(key) for lookups. Everything works in testing on one process, but two app servers send the same key to different cache nodes. What property is broken, and how do you fix it?
Show answer
Determinism is broken. Python's built-in hash() on strings is salted per process, so each server computes a different position for the same key. Fix it by using a stable hash such as hashlib.md5, MurmurHash3, or xxHash on the encoded key, in both the key lookup and the node placement.