Sub topic 4
The hash ring
This chapter assumes you know why hash(key) mod N remaps most keys when N changes, and that you can read short Python programs. It introduces the data structure that fixes the remapping problem. The next chapter covers the lookup procedure in detail.
The idea: one hash space for keys and nodes
Modulo hashing ties a key's location to the number of nodes. Consistent hashing removes that dependency. It hashes both keys and nodes into the same fixed range of numbers. A node's position then depends only on the node's own name, not on how many other nodes exist.
Take the output of a hash function such as MD5. It is a 128-bit integer from 0 to 2^128 - 1. Now bend that range into a circle so that the largest value sits next to 0. The circle is the hash ring.
import hashlib
RING_SIZE = 2**128
def position(value):
return int(hashlib.md5(value.encode()).hexdigest(), 16)
for name in ["node-a", "node-b", "node-c", "key1", "key2"]:
print(f"{name}: {position(name) / RING_SIZE:.3f}")The program prints each name's position as a fraction of the ring, between 0 and 1. The same name always gives the same position, on every machine and in every process. Hash function properties matter here:
- Determinism lets every client compute positions on its own, with no coordination.
- Uniformity spreads names evenly around the ring.
Python's built-in hash() fails the first requirement for strings, because it is randomized per process. Use a stable hash such as MD5, SHA-1, or MurmurHash.
Placing nodes and keys
Each node is placed at the position position(node_name). Each key is placed at position(key). Both live on the same circle. The ownership rule is simple: a key belongs to the first node you meet when you walk clockwise from the key's position.
In the diagram, key1 and key2 sit between node A and node B, so node B owns them. Key3 sits between B and C, so node C owns it. Keys past node C wrap around the top of the circle and belong to node A.
Arcs, not buckets
Each node owns the arc that ends at its position and starts just after the previous node's position. The arcs cover the whole circle with no gaps and no overlaps, so every key has exactly one owner. A node's share of the keys is roughly the length of its arc divided by the circumference.
With a ring of three nodes, adding a fourth node at one new position splits only one arc. Keys on every other arc keep their owner. The next chapters work through this in detail. The point here is that ownership is local: a key's owner depends only on its nearest clockwise neighbor, not on the total node count.
| Property | Modulo hashing | Hash ring |
|---|---|---|
| Key position | `h(k) mod N`, depends on N | `h(k)`, fixed |
| Node identity | Index from 0 to N-1 | Position from hashing its name |
| Owner rule | Index equals the remainder | First node clockwise |
| Effect of changing N | Most keys move | Only neighboring arcs change |
Common mistakes
- Hashing nodes and keys with different hash functions. They must share one space, or "clockwise" has no meaning.
- Naming nodes by volatile values such as a process ID. If the name changes after a restart, the node lands somewhere else on the ring and its keys move. Use a stable name or address.
- Forgetting the wraparound. Keys whose hash is above the last node position must go to the first node.
Exercise
A ring has nodes at positions 0.10, 0.40, and 0.75 (fractions of the circle). Which node owns a key hashed to 0.90, and which owns a key hashed to 0.40?
Show answer
The key at 0.90 has no node after it before the ring wraps, so it goes to the first node past 0, which is the node at 0.10. The key at 0.40 sits exactly at a node position. A key at or after a node's position is conventionally owned by the first node at or clockwise of it, so the node at 0.40 owns it.