TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About ยท Labs ยท Cheat sheets ยท Learning paths

โ† Back to Sharding

๐Ÿ“• Sharding: cheat sheet

๐ŸŽจ The analogySplitting a phone book too big for one binding into volumes by surname, so one name means opening one volume.
๐ŸŽ“ The regular explanationSplitting one large dataset across several databases by a shard key, so storage and load scale beyond one machine. The shard key decides which queries stay fast and where hotspots appear.
One phone book, several volumesStep 3 of 3
Volume Aโ€“HAhmAhmed0123Volume Iโ€“PKimKim0NovNovak123Volume Qโ€“ZSilSilva0123๐Ÿง’Reader๐ŸงญVolume guide
  1. The phone book got too big for one binding, so it's split into volumes by surname.
  2. To add "Novak", the guide sends you to volume Iโ€“P.
  3. Looking up one person means opening just one volume. Several volumes can be read at once by different people.
The volume everyone wantsStep 2 of 2
๐Ÿ‘ฅReaders๐ŸงญVolume guide๐Ÿ“•January volume๐Ÿ“—February volume๐Ÿ“˜March volume
  1. Imagine splitting orders by month instead. Everyone writes this month's orders, so one volume does all the work.
  2. Better is to scatter orders by a scrambled version of the customer number, so every volume gets its share.
Questions across volumesStep 2 of 2
๐Ÿง’Reader๐ŸงญVolume guide2 shards!๐Ÿ“•Volume 1๐Ÿ“—Volume 2๐Ÿ“˜Volume 3
  1. A question like "everyone in Leeds" isn't sorted by surname, so you must open every volume and combine the answers.
  2. Changing two people in different volumes at the same time, all or nothing, is hard, because no single volume is in charge.
One volume goes missingStep 4 of 4
new route๐Ÿ‘ฅReaders๐ŸงญVolume guide๐Ÿ“•Volume A-Hโœ• โœ•๐Ÿ’ซVolume I-Punreachable๐Ÿ“˜Volume Q-Z๐Ÿ“—Spare copy of I-Pmay miss writes
  1. On a normal day the guide sends each reader to the right volume, and every volume answers.
  2. The I-P volume goes missing. Anyone looking for a name in it gets nothing, but the guide still sends the other readers to their volumes.
  3. Luckily there is a spare copy of the I-P volume, kept in another building. The guide is told to use it from now on.
  4. Kim's lookup works again. But the spare copy may be missing the very last pages written before the volume vanished.

๐Ÿงฉ Key questions

To find one person, how many volumes do you open?
One: the guide tells you which. Different people can use different volumes at the same time.
You split the book by month. What goes wrong?
Everyone writes in this month's volume while the others gather dust. Scattering by a scrambled customer number shares the work evenly.
What does "everyone who lives in Leeds?" cost when the book is split by surname?
You open every volume and combine the answers. And changing two people in different volumes, all or nothing, needs careful coordination.
The I-P volume goes missing and there is no spare copy. What happens to readers?
Only people needing names in that volume are stuck; the rest carry on. A spare copy of each volume, kept elsewhere, lets the guide switch over quickly.