Splitting the ledger (sharding) a show in 11 chapters
Goal: write far more orders a minute than one pen can, and keep the books fair when one is added or lost
1 of 11 Your guess
Before we start
The chai stall keeps a ledger, a book where every order is one line. The last lesson gave it copy clerks: one master book and five clerks holding copies, so a hundred people can check their tabs at once.
Now the stall is busier, and the new orders are the part that is stuck. Which picture is closest to how a store can write far more orders a minute than one pen can?
Pick the closest. You find out which fits at the end.
100 readers ÷ 5 copies = 20 readers at each clerk
1 order × 5 copies = 5 writes1 order × 10 copies = 10 writes
2 of 11 The problem
One pen, one hand
The stall keeps a ledger. Every order goes in as one line: a name, how many cups, paid or not. One clerk writes each line, with one pen.
Your goal: write far more orders a minute than one pen can.
Reading was solved last time. Five clerks hold five copies, so a hundred customers check their tabs at once and nobody queues. Any copy can answer a read.
A new order is not a read. It goes into the master book first and then out to the clerks, so it has to land in every copy. Five copies means the line is written five times. Ten copies, ten times.
Add a sixth clerk and the writing does not get faster. Add a tenth. The hand writes at the speed of a hand, and at peak hour the queue is waiting for the pen, not for chai.
The number that matters is orders written per minute, the write throughput. Copying does not raise it.
The only move left is to stop copying and start cutting.
3 of 11 The fix
The cut: two books, two pens
Cut the ledger in half. Names from A to M go in the left book, N to Z in the right. Two books, two people writing, two orders landing at the same instant.
Nothing is copied. Anjali's line lives in one book and Rohan's in the other.
To write an order, look at the name, decide the book, write it there. To read Anjali's tab, make the same look.
The field you look at is the shard key. Each book is a shard. Splitting one store this way is sharding, and the rule that turns a shard key into a shard is the whole design.
Pick the key badly and you have not removed the wall. You have moved it.
Lock in your guess and the books fill.
2 books: 57 + 13 = 70, fair share 35, the first holds 57 ÷ 70 = 81.4%6 books: 49 + 4 + 4 + 6 + 4 + 3 = 70, fair share 70 ÷ 6 = 11.7worst case: 49 ÷ 11.7 = 4.2 times a fair share
4 of 11 The cost
The A names pile up
The stall's regulars are Aarav, Aditya, Ananya, Anjali, Bhavya and Chetan. Names here start with A far more often than with W. Cut the ledger A to M and N to Z, then watch the six come in.
Press Play: six regulars, two books.
Two pens, one busy. A book carrying far more than its share while the others sit idle is a hot shard. The counter is still one queue behind one pen, and now you pay for two books.
The lab's seventy fixed names are the same story at scale: two thirds start with A to D.
Of 70 names, two thirds start A to D. Cut A to M and N to Z: how many land in the first book?
More books do not help. At six books one holds 49 of the 70 names and the other five hold four, four, six, four and three. Every new book took a slice of the quiet end of the alphabet, so the A pile never split.
Cutting by the key's own order, A to M then N to Z, or January to March, is range partitioning. It has a real virtue: neighbours stay together.
Imagine the stall cut by date instead. Every order from Monday sits in one book, so one look answers it.
Google hit this pile at a size nobody could fix by hand. Bigtable, the storage system it described at a systems conference in 2006, range-partitions every table. Its word for one piece is a tablet.
When a tablet fills to around 100 to 200 megabytes, the server holding it splits it in two, with nobody involved. A piling range becomes two, then four, and the busy end is carved thinner.
One answer to the hot shard is to keep the order and keep splitting. The other is to destroy the order.
9 × 4 = 3637 − 36 = 1 left over
Aarav: 52 = 13 × 4 + 0, so book 0Aditya: 18 = 4 × 4 + 2, so book 2Ananya: 33 = 8 × 4 + 1, so book 1Anjali: 37 = 9 × 4 + 1, so book 1Bhavya: 22 = 5 × 4 + 2, so book 2Chetan: 41 = 10 × 4 + 1, so book 1
6 books, fair share 70 ÷ 6 = 11.7range's biggest: 49 ÷ 11.7 = 4.2 times fairhash's biggest: 16 ÷ 11.7 = 1.4 times fair
5 of 11 The fix
Scramble the names
Run the name through a hash: a function that turns text into a big number. It looks random, but the same text always gives the same number.
A real hash is a huge number. These are small and made up, so the sums can be done by hand.
Now take the remainder by the number of books. Lay Anjali's 37 tiles in rows as wide as the number of books, four here. 9 full rows fit and 1 tile is left over.
37 ÷ 4 leaves 1, so Anjali goes in book 1.
Do it for all six regulars. Aarav and Anjali both start with A and land in different books. The alphabet stops mattering.
This is hash partitioning, the flattest split there is.
Same seventy names. At two books range gave 57 and 13, hash gives 41 and 29. At six books range piled 49 into one book and hash's biggest is 16.
Seventy names is a small sample, so read the shape, not the last digit. The worst case is the number to watch, not the average.
The cost lands on a different question. "Every regular whose name starts with A to C" used to be one page of one book. Hash scatters those names, so the answer means asking every book and merging the answers yourself.
That trade is permanent. A shard key makes one question cheap, the look-up by that key, and every question cutting across it expensive.
page 7 ÷ 4 = 1 × 4 + 3, so it leaves 3 and goes to cache 3
2 → 3 books: rule 2 ÷ 3 = 66.7%, counted 43 of 70 = 61.4%4 → 5 books: rule 4 ÷ 5 = 80%, counted 60 of 70 = 85.7%
6 of 11 The cost
1997: hot spots on the web
Back to the mid-1990s. Every page on the web lives on one machine, its origin. Link one page from somewhere popular and every browser asks that machine at once, and it melts. People called it a hot spot.
The fix was to keep copies nearer the readers. A nearby copy kept for that purpose is a cache, and most requests stop there.
But a browser has to know which cache holds which page. So hash the page's address and take the remainder by the number of caches. It is the stall's rule exactly.
Twenty page numbers, 0 to 19. Made up: a real address hashes to a huge number.
Now one cache is added, so there are five. The count changed, so every remainder is worked again, this time ÷ 5. Watch each page, one at a time.
Press Play: twenty pages, a fifth cache.
What changed
- Pages that keep their cache: 20 → 4
Only pages 0 to 3 agree, because ÷ 4 and ÷ 5 only line up again at 20. In general N ÷ (N + 1) of the pages move.
Every cache now holds pages nobody asks it for, and is asked for pages it does not have. The whole layer goes cold in an instant, and the flood it existed to absorb lands on the origin.
Growing the layer was as dangerous as the hot spot itself.
It is not just 4 to 5. Adding one cache to N moves about N ÷ (N + 1) of the pages: two thirds from two to three, four fifths from four to five. The lesson's lab counted it on the seventy names.
The counted numbers wobble around the rule, because seventy names is few.
remainder: 7,975 ÷ 10,000 = 79.8% movedring: 1,806 ÷ 10,000 = 18.1% moveda fair guess: one new server of 5 takes 1 ÷ 5 = 20%
4 → 5 books, out of 70: range 11, hash 60, ring 28
7 of 11 The fix
The ring
At MIT in 1997, David Karger, Daniel Lewin and four colleagues published the fix at a theory conference. The idea is one move.
Take the line of hashes, smallest to biggest, and bend it into a circle so the end meets the start. Then hash the servers onto the same circle. Here are four, A to D.
The paper's title names the enemy: hot spots on the World Wide Web.
A key belongs to the first server point you meet walking clockwise from its own point.
Press Play: two keys walk to their servers.
Now D leaves. Its point disappears, and only the keys that pointed at D walk a little further, on to A. Rohan moves to A.
Anjali's walk is unchanged, because nothing else moved.
Adding a server is the mirror. E joins between D and A. Only the arc behind E changes hands, from A to E. Zoya's key, inside that arc, moves. Nobody else's does.
The server count is no longer baked into every answer.
The lesson counted it on 10,000 keys, with four servers becoming five. The remainder rule moved 7,975. The ring moved 1,806, and those are exactly the keys the new server now holds. Nothing moved between A, B, C and D.
What changed
- Keys moved when a server joins: 7,975 → 1,806
That is consistent hashing: the answer changes as little as it can when the set of servers changes. Daniel Lewin and Tom Leighton incorporated Akamai on it in August 1998.
The same comparison on the lab's seventy names, one more book each time. Range moved 11 whichever book it was. Hash moved 43, then 60. The ring moved 26, then 28.
The ring moves under half of hash's counts. But movement is not the only thing to check.
fair share at 4 books: 70 ÷ 4 = 17.5the ring's idle book holds 3: 3 ÷ 17.5 = 17% of fair
162 + 306 + 3,981 + 5,551 = 10,000 keysone server holds 5,551 ÷ 10,000 = 55.5%, where a fair share is 1 ÷ 4 = 25%
fair share: 8,000 ÷ 16 = 500 keys eachbusiest server: 2.37 × 500 = 1,185 keys765 ÷ 8,000 = 9.6% moved, all onto one server
8 of 11 The cost
One point per server is a bad cut
Look at the ring's loads at four books: 23, 3, 25, 19. One book holds 3 of the 70 names and sits nearly idle. The lab gave each book twelve points, and no more.
Range piled up, hash was even, and the ring moved least but is not fair.
Why? Drop one point per server on the circle and you cut it at random, and random cuts are not equal. In the lesson's run the four servers got 1.6%, 3.1%, 39.8%, 55.5% of the keys.
That is a hot shard again, by luck this time instead of by the alphabet.
Worse, when a server dies, the one neighbour clockwise of it inherits all of its keys, alone. In the lesson's ring of sixteen servers and 8,000 keys, server 4 died: 765 keys moved, every one onto a single server.
The ring kept its promise about how many keys move. It broke everything else.
D's 6 points: 2 + 2 + 2 = 6 to A, B and C
200 points: 536 ÷ 8,000 = 6.7% movedhash % N: 7,513 ÷ 8,000 = 93.9% moved
Lock in your guess and the points are counted.
16 × 200 = 3,200 points3,200 × 12 B = 38,400 B, about 38 KBat one point each: 16 × 12 B = 192 B
9 of 11 The fix
Many points per server
So stop giving each server one point. Give it a hundred or two hundred, scattered round the circle under different labels, like A#0, A#1 and A#2. Each point owns a small arc, and a hundred small arcs average close to a fair share whichever way the hashes fall.
Those extra points are virtual nodes. Here each server has six, drawn as its own shape, so you can see them.
Placed by a fixed rule for the picture, not measured.
Now D dies. Each of its small arcs goes to the next live point clockwise, and those belong to different servers. Here D's six points split 2, 2 and 2 over the three survivors.
In the lesson's run with sixteen servers and 200 points each, fifteen survivors shared the dead server's keys.
Same sixteen servers and 8,000 keys. Pick how many points each server gets and read the three bars.
One run of the lesson's own hash, not a benchmark of any real system. Read the order of the bars, not the decimals.
The ring's promise holds at every setting. Server 4 dying moved 765, 592, 521, 536 keys, 6 to 10% of 8,000. Plain remainder moved 7,513.
What changed
- Keys moved when a server dies: 7,513 → 536
Plain hash % N is the most even of all, with a spread of 4.5%. Its problem is movement, and now the ring has both.
Nothing is free. Two hundred points on each of sixteen servers is a table the ring has to keep.
Sixteen servers at 200 points each, 12 bytes a point: about how many KB is the ring?
38 KB against 192 bytes buys a spread of 5.6% in place of 67.5%. That is the answer to why two hundred virtual nodes.
10 of 11 Your guess
Your picture now
More copies left write throughput where it was (chapter 2). Cutting by name piled 57 of 70 names into one book (chapter 4). Hashing spread them, but a fifth book moved 79.8% of 10,000 keys (chapter 7). The ring moved 18.1%, and 200 points each cut the busiest server from 2.37 to 1.13 times its fair share (chapter 9). Which picture is closest now?
Pick one.
The answer is c: separate books, a scrambled number, a circle, and many small arcs for each book. a is more copies, which leaves writes where they were. b is the alphabet cut, which piles into a hot shard. Real rings run 100 to 200 points per server.
11 of 11 Keep this
Keep this
Copies scale reads, because any copy can answer one. Writes need the data split, not copied. Pick a shard key, scramble it with a hash, put books and keys on a circle, and give every book many small arcs, so losing or adding a book moves one arc and keeps the loads fair.
The price: a shard key makes one question cheap, the look-up by that key. Every question cutting across it has to ask all the books.
Read the full lessonComing up Now several books hold writing that is meant to agree, joined by a wire. Next: what you are allowed to do when that wire is cut and they cannot check with each other.