Sub topic 1
Why We Need to Spread Keys Across Nodes
Prerequisites
This chapter assumes you know the following:
- How a hash function and a hash table work.
- What caches, shards, and replicas are in a distributed system.
- How to read simple Python code.
Binary search is not needed yet. It appears in later chapters, when we build the ring.
The problem: one key space, many machines
A single machine cannot hold all the data or serve all the requests for a large system. So we split the data across several machines. Each machine is a node. Each item has a key, such as a user ID or a URL, and we need a rule that maps every key to exactly one node.
This rule has to meet three needs:
- Every client agrees. Any client or server that looks up a key must get the same node. If two clients disagree, one writes to a node and the other looks in a different one.
- Load is even. If one node gets most of the keys or traffic, it becomes a hot spot and the other nodes sit idle.
- No central directory. A lookup table that lists every key and its node would become a bottleneck. It would also need its own copy and its own failure handling. We want to compute the node from the key alone.
Systems that need this include distributed caches, sharded databases, and object stores. In a cache, a wrong node means a cache miss. In a database, a wrong node means the data cannot be found.
The simplest rule: hash and take the remainder
A hash function turns a key into a large number. If we have N nodes, numbered 0 to N-1, we can take the remainder of that number divided by N:
import hashlib
def h(key):
return int(hashlib.md5(key.encode()).hexdigest(), 16)
def node_for(key, num_nodes):
return h(key) % num_nodes
for key in ["alice", "bob", "carol", "dave"]:
print(key, "->", node_for(key, 4))This is modulo hashing. It meets all three needs. Every client that knows N and the hash function computes the same answer. A good hash function spreads keys evenly over the numbers, so the remainders are even too. No directory is needed.
Where the simple rule breaks
The rule depends on N. A cluster does not keep the same N forever. You add nodes when traffic grows, and nodes fail or get retired. When N changes, the remainder changes for most keys.
Take a key whose hash ends in 7. With 4 nodes, 7 mod 4 is 3, so it lives on node 3. With 5 nodes, 7 mod 5 is 2, so it moves to node 2. The key did not change, but its home did.
Measure this on real keys by comparing the node under 4 nodes with the node under 5:
import hashlib
def h(key):
return int(hashlib.md5(key.encode()).hexdigest(), 16)
keys = [f"key{i}" for i in range(10000)]
moved = sum(1 for k in keys if h(k) % 4 != h(k) % 5)
print(moved / len(keys))The output is close to 0.8. A key keeps its node only when h(k) mod 20 is 0, 1, 2, or 3. That is 4 of the 20 possible values, so about 20% of keys stay and about 80% move.
Why mass movement is costly
What happens after a remap depends on the system:
- Cache: Most lookups miss at the new node. All those requests fall through to the database at the same moment, which can overload it. This is called a cache stampede.
- Sharded database: Most records must be copied to a different machine. That uses network and disk bandwidth, and the data may be unavailable or stale while it moves.
- Failure of one node: Losing one node of N should only affect that node's keys. With modulo hashing, it reshuffles almost everything.
Scaling out is supposed to be a routine operation. Under modulo hashing it is a risky event that you schedule for quiet hours.
What we want instead
The goal for the rest of this tutorial is a rule with this property: when a node joins or leaves, only the keys that must move do. For a cluster growing from 4 to 5 nodes, that is about one fifth of the keys, and all of them go to the new node. The other keys stay where they are.
Consistent hashing gives this property. First, the next chapter looks at what we need from the hash function itself.
Exercise
A cache cluster uses hash(key) mod N to pick a node. It grows from 9 nodes to 10. Roughly what share of keys keeps its node, and why?
Show answer
About 10%. A key keeps its node only if h(k) mod 90 is less than 9, because that is when h(k) mod 9 equals h(k) mod 10 (the values 0 to 8). That is 9 of 90 residues, so about 10% of keys stay and about 90% move.