Sub topic 3
Why and how to partition data
This chapter assumes you know hash maps and sorted lists, have a basic idea of hashing, and can read short Python snippets. It follows the single-node storage engine chapter. Here we move from one machine to many.
Why partition
One node has limits. Its disk fills up, its memory runs out, and its network card and CPU can only serve so many requests per second. Copying the same data to more machines (replication) helps with availability and reads, but every machine still holds everything.
Partitioning (also called sharding) splits the key space into pieces. Each piece lives on a different node. Capacity and throughput then grow as you add nodes, because each node stores and serves only part of the data.
A partitioning scheme has to answer one question quickly: given a key, which node owns it? Every client and every node must reach the same answer without asking a central server on each request.
Two basic strategies
Range partitioning assigns contiguous key ranges to nodes, such as A–H on node 1 and I–P on node 2. Range scans are cheap because neighbouring keys sit together. The risk is hot spots. If keys are timestamps, all new writes land on the last range.
Hash partitioning hashes the key and uses the result to pick a node. Keys spread evenly, and hot spots from sequential keys disappear. The cost is that neighbouring keys end up on different nodes, so range queries must contact many nodes.
A key-value store with a get/put/delete API rarely needs range scans, so hash partitioning is the usual choice.
The naive approach: hash modulo N
The simplest hash scheme is node = hash(key) % N, where N is the node count. It works until N changes. The code below measures how many keys move when a cluster grows from 4 to 5 nodes.
import hashlib
def h(key: str) -> int:
return int.from_bytes(hashlib.md5(key.encode()).digest()[:8], "big")
keys = [f"user:{i}" for i in range(10000)]
before = {k: h(k) % 4 for k in keys}
after = {k: h(k) % 5 for k in keys}
moved = sum(before[k] != after[k] for k in keys)
print(f"{moved / len(keys):.0%} of keys moved")The output is close to 80%. A key keeps its node only when h % 4 equals h % 5. That holds for 4 of every 20 hash residues, so about 20% of keys stay. The other 80% must be copied to a new node.
What a good scheme needs
The modulo example shows what we want instead:
- Adding or removing one node should move only about 1/N of the keys.
- Load should stay roughly even across nodes.
- Any participant should compute the owner locally, with no lookup service on the hot path.
Consistent hashing meets these goals, and the next chapter builds it. Virtual nodes, which smooth out uneven load, come after that.
Summary of choices
| Strategy | Strength | Weakness |
|---|---|---|
| Range | Efficient range scans | Hot spots on sequential keys |
| Hash modulo N | Simple, even spread | Resizing moves most keys |
| Consistent hashing | Resizing moves about 1/N of keys | More machinery to build |
Exercise
A cluster uses hash(key) % 10 and grows to 11 nodes. Roughly what fraction of keys keeps its node, and why?
Show answer
About 1/11, or 9%. A key keeps its node only when h % 10 equals h % 11. Over the 110 residues of h % 110, that holds for 10 of them (residues 0 to 9), so roughly 9% of keys stay and about 91% move.