Module 04 · Lesson 05

Sharding, resharding, and quorums

Partitioning buys scale and takes away joins, uniqueness, and easy rebalancing.

Advanced 9 min read Updated 2026-11-01

What you will be able to do

  • Choose a shard key that distributes load and matches the query
  • Detect and fix a hot partition
  • Explain resharding strategies that avoid downtime
  • Compute quorum sizes for a desired consistency

The shard key decides everything

Sharding splits rows across nodes by a key. That key is in every request routing decision, so it must match how the application reads. Shard chat messages by channel id and a channel read hits one shard. Shard by message id and the same read fans out to all of them.

Candidate keyGood forBreaks when
User idPer-user data, profiles, settings, personal feedsA user with extreme activity; cross-user queries
Tenant idSaaS isolation, per-tenant backups and limitsOne tenant is orders of magnitude larger
TimeEvents, logs, metrics; drop whole old shardsAll writes hit the newest shard
Geo / regionData residency, locality, regulatory boundariesA region has a tiny or huge population
Hash of an idEven spread, no natural ordering neededRange queries across the hash
Composite keys save designs
Shard by user_id, but store messages with a sort key of (channel_id, message_id) so a channel's recent history is one contiguous range inside the user's shard. You keep locality at both levels without writing to every node.

Hot partitions

Even sharding still concentrates load if one key is popular: a celebrity's post, a flash sale on one product, a tenant with a hundred times the traffic. Replication of that partition to several nodes spreads reads, but every write still lands on one leader.

  • Salt the key — split one logical key into N sub-shards (user_id#3), spreading writes at the cost of N reads to gather.
  • Dedicate capacity — move the big tenant to its own shard with its own quota; noisy-neighbour isolation beats clever hashing.
  • Buffer the writes — accept the traffic into a queue and let one writer apply it in order, turning a burst into a smooth rate.
  • Cache the read — the hot key is usually the hot read; a CDN or cache absorbs it without touching storage.

Resharding without downtime

Double write
Write to both the old and new layout. Reads still come from the old one, so nothing is broken yet.
Backfill
Copy historical data in the background, throttled so it does not steal production capacity.
Verify
Compare counts and checksums per key range. Do not cut over on the assumption that it worked.
Cut reads over
Flip reads for one key range at a time, watching error rates, keeping the old path warm for rollback.
Retire
Stop double writes once the new layout has served every read for a full cycle, then drop the old data.

Consistent hashing on a ring plus virtual nodes means adding a node moves only its share of keys, not a modulo reshuffle of everything. That is why it shows up in caches and shard maps, and why you mention it when asked how you would add capacity.

Quorums in one table

In a leaderless store with N replicas, a write acknowledged by W replicas and a read served by R replicas intersect if R + W > N. The intersection is what makes the freshest write visible.

NWRProperty
322Strong-ish: quorum reads and writes, tolerates one node down
331Fast reads, brittle writes — a single failure blocks writes
311Fast and available, but a read may miss a recent write
533Tolerates two failures while keeping overlap

Lower R + W also lowers latency and raises availability, because fewer nodes must answer. Say which you chose and what it costs: with W = 1 a write can be lost if that single node fails before replicating.

Instead
Sharding by a key your main query does not filter on
Do this
Shard by the key in the hot query, even if it means denormalising another copy for the second access pattern
Instead
Auto-increment ids as global primary keys across shards
Do this
Per-shard sequences or a distributed id generator (snowflake-style, time plus node plus sequence)
Instead
Resharding by stopping writes and copying
Do this
Double write, backfill, verify, cut over incrementally, keep a rollback path

Worth remembering

  • The shard key must appear in your hottest query, or every read fans out to every shard.
  • Even distribution matters less than avoiding a single key that receives most traffic.
  • Resharding must be incremental and reversible — dual writes plus backfill plus cutover.
  • In a quorum store, R + W > N gives read-your-writes; smaller sums trade that for availability.

Checkpoint

Answer all 3 questions to finish the lesson.
1. You shard a chat system by message id. Users open a channel and request its last 50 messages. What goes wrong?
A read that does not include the shard key must query all shards and merge. Sharding by channel (or user plus channel sort key) keeps the read local.
2. With N = 3, which R and W pair guarantees a read sees the latest acknowledged write?
R + W = 4 > N = 3, so read and write quorums overlap on at least one node that has the newest value.
3. Why is double writing the first step of a resharding plan?
Without double writes, everything written during the backfill is missing from the new layout. Writing to both first makes the migration lossless and reversible.