Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

System Design the lesson

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.

namecupspaid?Anjali2 cupspaidRohan1 cupowesAarav3 cupspaida new lineone pen

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.

Step 1 of 4

two orders, same instantAnjaliA to MAnjalione penRohanN to ZRohanone pen

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.

Step 1 of 2

A to MN to Zsix regulars are coming

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.

Step 1 of 4

text in, number out, the same every timehashAarav52Aditya18Ananya33Anjali37Bhavya22Chetan41made up

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.

Step 1 of 5

one hot page: the machine meltsoriginnearby copies, caches, answer firstcachecachecacheoriginrarely asked

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.

Step 1 of 5

start and end meet hereABCDa ring of hashes, with 4 servers on it

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.

Step 1 of 6

far above fairfar below fairrange51685hash22121917ring2332519

fair share at 4 books: 70 ÷ 4 = 17.5the ring's idle book holds 3: 3 ÷ 17.5 = 17% of fair

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.

Step 1 of 3

ABCD6 points eachshown herereal rings run 100 to 200 per server

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.

Step 1 of 5

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.

Splitting the ledgerCopies scale reads. Cutting the book scales writes. Adding a book should not reshuffle them all.79.8%of 10,000 keys move: 4 shards become 5, by remainder18.1%move on a ring: only one arc changes handsOne point per server is a bad cut.Busiest server: 2.37x its fair share.Its neighbour inherits every key.Virtual nodes: 200 points each.Busiest server: 1.13x its fair share.A dead server's keys go to 15 others.The price: a shard key makes one question cheap.Every other question has to ask all the books.chalkbrain.com
Splitting the ledgerCopies scale reads. Cutting the bookscales writes.79.8%of 10,000 keys move when 4 shardsbecome 5, by hash remainder18.1%move on a ring: only the arc behindthe new shard changes handsOne point per server is a bad cut:the busiest holds 2.37x its fair share.Virtual nodes, 200 points each: 1.13x.The price: a shard key makes onequestion cheap. Every other asksall the books.chalkbrain.com

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 lesson