Where a key lives
Illustrative exampleNumbers here are illustrative: round figures to reason with, not measurements.
Hash keys onto a ring and give each machine many small arcs of it (virtual nodes), as the Dynamo paper does (Dynamo paper (opens in a new tab)); Cassandra defaults to 16 tokens per machine (cassandra.yaml (opens in a new tab)). A key belongs to the machine whose arc it falls in. A 31st machine takes a few arcs from many machines, about a 31st of the data; the other keys stay put. Never place keys by hash(key) mod the machine count when machines change: going from 30 to 31 moves about 30 of every 31 keys.
Fixed partitions mapped by a small table, rendezvous hashing, or ranges that split and move, as in Raft-based stores, work too. Every machine keeps the map, so any machine forwards a request to the key’s owners.
| Compared on | Hash mod N | Ring, many arcs each | Fixed partitions | Ranges that split |
|---|---|---|---|---|
| A 31st machine moves | About 30 of every 31 keys | About a 31st, from many machines | Whole partitions, about its share | The ranges moved to it |
| Finding a key | Arithmetic | The ring in memory | A small table | A range map, cached |
- A client writes cart:42 to any machine, which hashes the key, finds its arc and forwards the write to the owners.
- A 31st machine joins and takes a few small arcs from many machines.
- A retired machine’s arcs pass to many different machines, a little each.
Read:Learn: Partitioning, sharding and consistent hashingLearn: Apache Cassandra
Keep reading+1 min
Ranges for scans, hashes for spread
Hashing spreads keys evenly but loses their order, so range scans would touch every machine. Stores that need scans keep sorted ranges and split them when they grow, at the cost of watching for a range that everyone writes to. This brief has no scans, so hashing is the simpler fit.
Check yourself
Why many small arcs per machine rather than one?
Show answerHide answer