Home›Learn›Many servers 5: split the data
Course · Spread the dataLesson 19 of 330 read

Consistent hashing at the database

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.

How each engine places keys

Go deeper
EngineHowAdding a node
CassandraMurmur3 token ring; num_tokens ranges per node; replicas are the next nodes, per data centerStreams only the ranges the new node takes
Redis Cluster16,384 fixed hash slots: CRC16(key) mod 16384; each node owns a set of slotsMove slots one at a time; {hash tags} keep related keys in one slot
MongoDBRanged or hashed shard key, split into chunks; the balancer moves chunksThe balancer migrates chunks in the background
DynamoDBPartitions by hash of the partition key, managed for youSplit and moved automatically
Citus, VitessA distribution column hashed into a fixed number of shards placed on nodesMove 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 tag
Next · chapter 6Across servicesSagas and the outbox, no lost events← Chapter 4: Unique IDs
Connected

Where this shows up

The same mechanism, sharding and partitioning, hot keys and hot partitions, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 3 questions

Check yourself

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?

2. With a token ring and four equal nodes, adding the fourth moves about:

3. How does Redis Cluster place a key?

One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 pagesAll pages

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.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect