Deep Read · DDIA · Chapter 6

Partitioning: When One Dataset Is Too Big for One Machine

Designing Data-Intensive Applications · Ch 6 · Martin Kleppmann · 2017

中文 →

What is this chapter about?

The last chapter was about keeping several copies of the same data (replication). This chapter tackles a different wall: the data itself is too big—too big for any single machine's disk, and too busy for its CPU. A social network's billions of messages, an online store's hundreds of millions of products—no one server can hold or serve all of it. The fix is blunt: chop the giant pile into many small chunks and give each chunk to one machine. That chopping-and-spreading act is called partitioning (also known as sharding).

A picture first

Imagine a huge library, far too many books for one shelf. You have two ways to split them. One: by the first letter of the title—A through F on shelf 1, G through M on shelf 2, and so on. Two: give every book a scrambled "code number"—the same title always gets the same code—and drop it on the shelf its code points to. The first makes "find all the D books" easy (they're together); the second makes every shelf equally full so none gets crowded. Databases split data with exactly these two moves.

Why the old world was hard

Not splitting is a dead end: one machine can't store that much, and can't keep up with that many requests. But the moment you split, a new problem appears—an uneven split causes traffic jams. If all the popular requests happen to land on the same shelf (a celebrity posts, and tens of millions arrive at once), that one machine gets crushed while the others sit idle. This "one cell overloaded, the rest empty" situation is the enemy the whole chapter keeps fighting.

Three real problems once you've split

1. How do you split it evenly? Splitting by first letter is simple and lets you grab a range in order—but it invites hot spots (everyone wants the newest thing). Splitting by scrambled code is the most even—but you lose the ability to "grab a range in order" (neighbors got flung to opposite ends). Each has its sweet spot and its sting.

2. When you add a new shelf, how do you move the books? When the library expands, you can't tear down and re-sort everything—that means closing for days. The smart move is to shift only a small fraction of books from each old shelf to the new one; move as little as possible.

3. To find one book, how do you know which shelf it's on? You need a "front desk" or a "directory" that tells you which machine owns the data you want. And when shelves shift, that directory has to stay current.

So is this the same as "keeping copies"?

No—but they're a golden pairing. Replication is "the same content, copied onto several machines" (survive failure, stay close, spread reads). Partitioning is "chop the whole big dataset into different chunks, each machine owns one chunk" (so it fits and keeps up). Real systems almost always do both at once: chop into many chunks, then keep three copies of each chunk on different machines—so it fits and it can survive failures.

Remember this one line

Partitioning = chop a dataset too big for one machine into many small chunks, each owned by one machine, so you can scale. Two ways to chop: in order (easy to grab ranges, but jam-prone) or by scrambled code (most even, but no range grabs). The real difficulty is keeping any one chunk from getting crushed (hot spots), moving little data when you add machines, and always knowing where each record lives. It's usually used together with last chapter's replication.

Want the actual mechanisms, secondary indexes, rebalancing strategies, and diagrams? → Switch to the deep read