๐ 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.
- The phone book got too big for one binding, so it's split into volumes by surname.
- To add "Novak", the guide sends you to volume IโP.
- Looking up one person means opening just one volume. Several volumes can be read at once by different people.
- Imagine splitting orders by month instead. Everyone writes this month's orders, so one volume does all the work.
- Better is to scatter orders by a scrambled version of the customer number, so every volume gets its share.
- A question like "everyone in Leeds" isn't sorted by surname, so you must open every volume and combine the answers.
- Changing two people in different volumes at the same time, all or nothing, is hard, because no single volume is in charge.
- On a normal day the guide sends each reader to the right volume, and every volume answers.
- 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.
- 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.
- 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.