GOAT EngineerPreparing your learning space…

databases · scaling · sharding

Partitioning vs sharding

Both split a table into pieces. Only one of them puts the pieces on different machines, and everything hard about sharding falls out of that.

Step by step

  1. A billion rows in one table. Now what?

    Two words, used interchangeably, describing two very different commitments. One is a setting you can change on a Tuesday. The other changes what your application is allowed to assume, permanently.

  2. Both start the same way: cut the table up

    You pick a key, usually a date or a customer id, and split the rows by it. So far the two ideas are identical. That is why the words get swapped.

  3. Partitioning keeps every piece on one machine

    The database still owns all the data. It uses the pieces to skip work: a query for last week reads one partition and ignores the other fifty.

  4. Same cut, same key. So what makes it sharding?

    Exactly one thing changes, and every difficult consequence in the rest of this follows from it. Worth a moment before reading on.

  5. The machines. Each piece gets its own.

    No single machine holds the whole table any more. That is the point of it, and it is also the moment every guarantee you were quietly relying on gets weaker.

  6. A join was a local read. Now it is a network call.

    Within one machine, joining two partitions is an ordinary local read. Across shards it is a network call to another database, which can be slow, down, or both.

  7. Transactions stop at the machine boundary

    One machine gives you a normal transaction across every partition. Across shards you need two-phase commit or you give up atomicity, and most teams give up atomicity.

  8. Slow table, still fits on one machine. Which one?

    The answer is decided by one question, and it is not how big the table feels or how many rows are in it.

  9. Partitioning. It buys speed, not capacity.

    Partitioning buys query speed and cheap bulk deletes. Sharding buys capacity beyond one machine. If capacity is not what hurts, sharding is not the fix.

  10. So reach for sharding last

    Partitioning is a configuration change. Sharding is an architecture, and it is very hard to undo. Do it when one machine genuinely cannot hold or serve the data, and not before.

The question that separates them

Ask one thing: can a single machine still see every row? If yes, you are partitioning. If no, you are sharding. Everything else follows from that answer.

Why the distinction is worth holding on to

Partitioning is a knob on a database you already run. Add it, change the key, remove it. Nothing outside notices.

Sharding changes what your application may assume. That a join works. That a transaction is atomic. That a unique constraint is unique.

Picking a shard key

  • Pick a key that appears in nearly every query. Otherwise every read fans out to every shard, and you have built a slower single database.
  • Pick a key with many roughly equal values. Sharding by country gives one enormous shard and forty idle ones.
  • Expect never to change it. Re-sharding a live system is among the most expensive migrations there is.

Most systems that think they need sharding need an index, a cache, or a read replica. Those are reversible in an afternoon.

All snippets