Skip to main content
Scalingchapter 4 of 4 · 4 lessons

Database Sharding

Horizontally partition data across multiple DB instances by shard key.

One database machine, well tuned, takes somewhere around 10,000 writes a second before it stops keeping up. You are doing 9,000.

Read copies do not help, because every write still lands on the one leader. Caching does not help, for the same reason. A bigger machine buys you one more step, and there is no step after the biggest machine your cloud sells.

Sharding splits the rows across separate databases so ten machines take ten times the writes. It is the answer, and it is also a migration measured in quarters that complicates every feature you build afterwards, which is why the rest of this chapter is mostly about waiting as long as you possibly can.

Lessons

4 in this chapter
  1. Why Shard, and Why WaitReplicas scale reads. Nothing scales writes on one box forever. Shard late, on purpose.2 min
  2. Picking a Shard KeySpread the load evenly and keep common queries on one shard. Those two goals fight.3 min
  3. Resharding Painhash mod N moves everything when N changes. Logical shards make it a move, not a rehash.3 min
  4. Living with Cross-Shard QueriesQueries that name their shard stay fast. Everything else needs a plan, not a hope.3 min