Data Engineering/Database Engineering · Part 7 of 14
·14 min read
Sharding and Consistent Hashing: Splitting Data Across Servers
What sharding is, how a hash ring decides where a key lives, why plain modulo moves nearly everything when a server is added, and what you give up: a real transfer across two shards lost 100 when one server died. With a working ring you can drive.
Partitioning splits a table within one database. Sharding goes further: each piece lives on a separate server. That removes the ceiling of a single machine, and it also removes things you've come to rely on.
This post builds the course's sharded URL shortener on three real Postgres servers, and explains the hash ring that routes keys to them. Then it does what the course only describes, and breaks a transaction across two shards. The money really did go missing.
#What sharding is
One server has one set of limits: memory, CPU, disk and connections. As a table grows, so do its indexes, and queries slow down. Sharding splits the rows across servers by a shard key. Each server holds a unique subset, so each has a smaller table and smaller indexes.
The layers stack, and that's how very large systems get a query down to something small:
The new question is the one partitioning never had: given a key, which server has it?
| Strategy | Example | Good for |
|---|---|---|
| Range | Users 1 to 500M on server 1 | Ordered numeric keys |
| List | West coast zip codes on one shard | Geography and tenants |
| Hash | hash(key) picks a server | Keys with no natural order, like a short code |
#A sharded URL shortener
The course's demo: three Postgres servers, each with the same url_table, and a small Node server in front. To shorten a URL, hash it with SHA-256, keep the first five characters as the code, run that code through a hash ring to pick a server, and insert the row there. Reads run the same code through the same ring.
Request or acknowledgmentMetadataData
I ran this on three real containers, with 10,000 URLs:
-- 10,000 URLs: sha256 -> first 5 characters -> hash ring -> INSERT on that server
server on port 55441: 2775 rows
server on port 55442: 3612 rows
server on port 55443: 3613 rows
-- read path for code 'khYaW': the ring says port 55443
port 55443: https://example.com/page/1234
port 55441 (the wrong server): 0 rows
-- not by shard key: SELECT count(*) ... WHERE url LIKE '%/page/99%'
had to ask all 3 servers and add up: 26 + 45 + 40 = 111, in 1.9 ms
The first block shows the spread across servers. The second shows the rule: the right server has the row, and the wrong one has nothing. The third shows the cost of a query that doesn't use the shard key: it has to ask every server and add up the answers.
The course also records a dead end worth keeping. The first consistent-hashing library it tried gave different servers for the same key, which broke reads. It switched libraries. The lesson is to verify that a mapping is stable before trusting it with data.
#From modulo to a ring
#Why hash % N isn't enough
The obvious way to pick a server is hash(key) % N. It's simple and spreads keys well. The problem appears when N changes: nearly every key's remainder changes too, so nearly every key now maps to a different server. To stay correct you'd have to move almost all your data.
#The ring
Picture the hash space as a circle. Hash each server onto it, and each key too. A key belongs to the next server clockwise.
Adding a server now only claims the keys in the arc just before it. Everything else stays put, and only one neighbour has to hand data over. Here's a working one, with real hashes. Add a server in each mode and compare the red dots:
Measured on 100,000 keys, adding one server:
With modulo, 67% of keys moved going from 2 to 3 servers, and 94% going from 16 to 17, so the bigger your cluster, the worse it gets. With a ring the share falls as the cluster grows, to about 1 in N + 1, which is the fair share for the new server to take.
#Points per server
One point per server doesn't spread keys evenly, because the arcs between hashed points vary a lot. Try the slider in the ring above, then see it measured:
With one point each, the busiest of five servers held 263% more than its fair share. With 64 or more points the worst server is only a few percent over. These extra points are usually called virtual nodes. They also spread a departing server's keys across many neighbours, rather than dumping them all on one.
The ring doesn't make resharding free. The course is clear about that: moving a server still means copying its arc's data, and a crash gives you no time to copy anything, which is why shards also need replicas. Consistent hashing was introduced in 1997, and systems like Cassandra use it. The arithmetic is simple enough to try in SQL:
The last preset shows Postgres's own hash partitioning, which is hash % N inside one server, where the same rule applies if you change the modulus.
#What you give up
| Partitioning | Sharding | |
|---|---|---|
| Where | One database | Many servers |
| Client | Doesn't know | Must know, or use a proxy |
| Table name | Differs behind the scenes | Same on each server |
| Transactions | Work | Not across shards |
| Joins | Work | Not across shards |
#Transactions
Each shard is a separate database, so nothing makes two commits atomic. To see what that means, I moved 100 between accounts on two real shards, and killed the second server between the two commits:
-- shard 1
BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- shard 2
BEGIN; UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- shard 1
COMMIT;
-- shard 2 dies before its COMMIT
$ docker kill -s KILL shard2
COMMIT on shard 2 failed: read ECONNRESET
-- after shard 2 restarts
account 1 on shard 1: 400
account 2 on shard 2: 100
total: 500, and it started at 600
The total fell from 600 to 500. Shard 1 committed, shard 2 never did, and neither knows about the other. Everything post 1 promised was per server. Two-phase commit and sagas are the usual answers, and post 14 runs one.
#Everything else
- Client complexity. The app is coupled to the shard layout, and the course's line for it is that software needs social distancing.
- Joins. Cross-shard joins are extremely hard, which is a strong reason to prefer partitioning if you need them.
- Schema changes have to go to every shard, and missing one is a risky failure.
- Queries off the shard key hit all shards, as the demo showed.
#When to shard
The course's answer is: as late as you possibly can. Find the real bottleneck first.
Reads scale easily, because copies don't conflict. Writes are the hard case, and they're the usual reason to shard at all. Its benefits are real: resources that scale past one machine, smaller indexes, and a way to isolate sensitive data on its own shard.
🎓 The course's example is YouTube, which ran MySQL with these techniques and only sharded at the application level when writes became unmanageable. That created painful coupling. Changing the shard size meant changing every client. The answer was Vitess, which sits in front of MySQL, parses SQL, and routes it to the right shard, so clients don't have to know. As with everything here, it's one more system to run.
#Things that can trip you up
- Picking a shard key you can't query by. Everything else becomes a scatter across all shards.
- Hot shards. A hash spreads keys, but a single very popular key still lands on one server.
- Using modulo and then adding a server. Nearly everything moves. Plan for growth first.
- One point per server on a ring. The load will be badly uneven.
- Assuming a client library is stable. The course's first one wasn't. Test that a key always maps to the same server.
- Generating ids per shard.
serialrestarts on each server, so ids aren't unique across shards. Use UUIDs or a key that includes the shard. - Cross-shard transactions. There aren't any, unless you build them.
#Reproduce this yourself
- ✅ Start three servers, on different ports:
docker run -d --name shard1 -p 5441:5432 -e POSTGRES_PASSWORD=postgres postgres:18 docker run -d --name shard2 -p 5442:5432 -e POSTGRES_PASSWORD=postgres postgres:18 docker run -d --name shard3 -p 5443:5432 -e POSTGRES_PASSWORD=postgres postgres:18 - ✅ Create the same
url_tableon each:id serial PRIMARY KEY, url text, url_id char(5) UNIQUE. - ✅ In Node, I used my own small ring:
md5ofserver#nfor 64 points per server, sorted, then a binary search for the first point at or abovemd5(key). 📘 The course uses thehashringpackage, which I didn't use. - ✅ For the failed transfer,
BEGINand update on both servers,COMMITon the first, thendocker kill -s KILL shard2. - 📘 The course's auto-init trick, copying an
init.sqlinto/docker-entrypoint-initdb.d/, is documented for the Postgres image. I created the tables from the client instead.
#Glossary
| Term | Meaning |
|---|---|
| Shard | A server holding a subset of a table's rows |
| Shard key | The column that decides a row's shard |
| Hash ring | Servers and keys placed on a circle, each key owned by the next server clockwise |
| Consistent hashing | Hashing where adding a server moves only a small share of keys |
| Virtual node | One of many ring positions for a single server |
| Resharding | Moving data to a new shard layout |
| Vitess | Middleware that routes SQL to MySQL shards |
#Summary
Sharding splits rows across servers, which raises every limit of a single machine, and makes routing your problem. A hash ring keeps that routing stable as servers come and go, moving about one key in N + 1 where modulo moves nearly all of them, as long as each server has many points on the ring. In exchange you lose joins and transactions across shards, which a real crash showed by losing 100 from a transfer. So it's the last step, after partitioning, caching and replication.
#Concepts at a glance
| Concept | In one line | Why it matters |
|---|---|---|
| Shard key | Decides where each row lives | Queries off it hit every server |
hash % N | Simple, and fragile | Changing N moves most keys |
| Hash ring | Next server clockwise owns a key | Only one arc moves |
| Virtual nodes | Many ring points per server | Even load, gentler failures |
| Lost atomicity | Each shard commits alone | Transfers can half-happen |
| Scatter query | Ask everyone, then add up | The price of an off-key filter |
| Vitess | A proxy that hides shards | Trades coupling for another system |
#Test yourself
You have 10 servers using hash % N and add an eleventh. Roughly what share of keys move, and why?
About 91%. A key stays put only if its remainder is the same for 10 and 11, which is rare. Nearly every key lands on a new server, and all that data has to move.
How does a hash ring limit what moves when a server is added?
A new server takes only the keys in the arc before it, which all came from one neighbour. Every other key still maps to the same server, because its next-clockwise server hasn't changed.
Why do real rings give each server many points?
With one point each, arc lengths vary a lot, so some servers get far more keys. Many points average that out. It also means a departing server's keys spread across many others.
A transfer updates rows on two shards, and the second server dies after the first commits. What happens, and why can't the database stop it?
The first shard keeps its change, and the second loses its own, so the data is now wrong. Each shard is a separate database with its own commit, and nothing ties them together unless you add a protocol like two-phase commit or a saga.
What should you try before sharding?
Partitioning, caching, read replicas, and multi-region replication, in roughly that order, after finding the real bottleneck. Reads are easy to scale, so sharding is mainly for write load that one server can't take.
#Open questions and gaps
- I used my own ring rather than the course's
hashringpackage, so I didn't see its behaviour or the buggy library it replaced. - The servers shared one laptop, so this shows correctness and data movement, not speedup.
- 🎓 The YouTube story comes from the course. I didn't check it against a primary source.
- 📘 Vitess is described, not run.
#Sources
- Hussein Nasser, Fundamentals of Database Engineering, including the talks on hash tables and consistent hashing
- Karger et al., Consistent Hashing and Random Trees (1997)
- Apache Cassandra docs: Dynamo architecture
- Vitess
- PostgreSQL docs: Table Partitioning