When I add a server, how much data has to move?
The ring and hash mod N, animated and compared.
Press Play, or step with the arrows. Compare with hash mod N to see why the ring exists.
Once data is split across servers, every read and write has to know which server holds the key. The obvious answer, hash the key and take the remainder by the number of servers, works until you add a server.
| Engine | How | Adding a node |
|---|---|---|
| Cassandra | Murmur3 token ring; num_tokens ranges per node; replicas are the next nodes, per data center | Streams only the ranges the new node takes |
| Redis Cluster | 16,384 fixed hash slots: CRC16(key) mod 16384; each node owns a set of slots | Move slots one at a time; {hash tags} keep related keys in one slot |
| MongoDB | Ranged or hashed shard key, split into chunks; the balancer moves chunks | The balancer migrates chunks in the background |
| DynamoDB | Partitions by hash of the partition key, managed for you | Split and moved automatically |
| Citus, Vitess | A distribution column hashed into a fixed number of shards placed on nodes | Move whole shards |
The common idea: never place data with mod of the server count. Either hash onto a ring, or hash into a large fixed number of slots or shards and move those. Then adding a server moves a share of the data, not almost all of it.
# Cassandra: which nodes own a key nodetool getendpoints shop orders 42
# Redis Cluster: which slot a key is in
CLUSTER KEYSLOT user:42
CLUSTER KEYSLOT {user:42}:cart # same slot, thanks to the hash tagThe same mechanism, sharding and partitioning, hot keys and hot partitions, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. Going from 3 to 4 servers with hash mod N, about how much data moves?
A key stays only when h mod 3 equals h mod 4: 3 values in every 12.
2. With a token ring and four equal nodes, adding the fourth moves about:
Only the arcs the new node takes over.
3. How does Redis Cluster place a key?
Slots are assigned to nodes and moved between them. Hash tags in braces put related keys in the same slot.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.