SD System Design In Depth
0%
lesson system-designdatabasespartitioning

Sharding and partitioning

Module 2.2: NoSQL Architectures, Partitioning & IDs Updated Sep 17, 2026
📄 Cheat Sheet
Sharding in System Design Interviews w/ Meta Staff Engineer
Hello Interview Sharding in System Design Interviews w/ Meta Staff Engineer Watch on YouTube ↗

Thorough treatment of shard-key selection, range vs hash vs directory sharding, hot spots, cross-shard queries and resharding, framed the way senior interviews probe it.

0:00 / --:--

Concrete scenario

One tenant dominates writes and the primary no longer meets latency or recovery targets.

Mechanics

Shard when measured capacity, locality, or fault-domain requirements cannot be met safely by one write domain; there is no universal 2 TB or writes-per-second threshold. Range partitioning aids scans but can hotspot monotonic keys; hashing spreads keys but scatters ranges.

Failure mode

A poor shard key creates hot partitions, cross-shard joins, and distributed transactions. Rebalancing can consume network and disk while old and new routers disagree.

Trade-off

Sharding adds capacity and isolation at the cost of routing, rebalancing, global constraints, and operations. Vertical scaling or partitioning within one database is often simpler first.

Worked example: sharding a multi-tenant SaaS

Suppose there are 20k tenants and 12 TB of data, with a single primary at 70% CPU on writes. The dominant queries are all scoped to one tenant.

  • Shard key: tenant_id. Every dominant query becomes single-shard, and per-tenant transactions and foreign keys still work within the shard.
  • Logical shards: logical = hash(tenant_id) mod 1024, then a lookup table maps logical shards to 8 physical clusters (128 logical per cluster, about 1.5 TB each).
  • Big tenants: the largest tenant has 8% of the data. Pin it to its own cluster via a directory entry, so it does not overload a hashed neighbour.
  • Growth: to go from 8 to 16 clusters, move 64 logical shards off each cluster using logical replication or CDC, then flip the directory entry. No rows are rehashed.

Strategy comparison

StrategyRoutingRange scansHotspot riskRebalancingUsed by
RangeKey-range tableEfficientHigh for monotonic keysSplit and move rangesBigtable, HBase, Spanner, CockroachDB
Hash (mod N)ComputeScatter to all shardsLow (except hot keys)Painful: most keys moveSimple app-level sharding
Consistent hashing / vnodesRing lookupScatterLowAbout 1/N of keys moveCassandra, DynamoDB (internally), Riak
Directory / lookupMetadata serviceDepends on keyControllableMove individual entriesVitess (vindexes), Notion, Figma

What you lose after sharding

  • Cross-shard joins: push them to an analytics store, denormalize, or run scatter-gather with limits.
  • Global unique constraints: email uniqueness across shards needs a separate global table or service.
  • Cross-shard transactions: use 2PC (slow, blocking on coordinator failure) or sagas with compensation.
  • Globally sorted pagination: needs a merge across shards, so prefer per-shard cursors.
flowchart LR A["App"] --> R["Router: tenant_id to logical shard"] R --> D["Directory: logical shard to cluster"] D --> C1["Cluster 1: logical 0-127"] D --> C2["Cluster 2: logical 128-255"] D --> C8["Cluster 8: logical 896-1023"]
Check yourself Question 1 of 3
Range vs hash trade recall
From: Mechanics

You partition an orders table by an auto-increment order_id. What is the key difference between range and hash partitioning for this key?

Sharding & Hot Partitions
Simulation

Simulate request routing across 4 shard buckets. Adjust skew to see the impact of hot partitions.

Shard 0
0
Shard 1
0
Shard 2
0
Shard 3
0

Key Takeaways

No universal size or throughput threshold requires sharding.
Range and hash partitioning exchange scan locality for distribution.
Sharding adds routing, rebalancing, and cross-shard coordination.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue