Why this matters
- VaultCommerce crossed 8K sustained order writes/sec during a flash sale — vertical scale and read replicas were no longer enough.
- Choosing the wrong shard key creates hot shards that negate distribution benefits.
- Interviews expect you to defend a partition key and explain what breaks when you add shards.
Vertical vs horizontal partitioning
Two partitioning styles
- Vertical — split wide tables (
usersvsuser_profiles) or move cold columns to archive storage. - Horizontal — split rows across nodes by hash or range of a key.
- When vertical helps — reduce row width, isolate LOB columns, separate read profiles.
- When horizontal helps — write QPS or storage exceeds one node's capacity.
VaultCommerce vertically split product descriptions into MongoDB while keeping SKU inventory in Postgres. Horizontal sharding arrived later when the orders table exceeded comfortable single-node write throughput.
Shard key selection
The shard key must match your hottest queries and spread load evenly.
# VaultCommerce order shard router
def shard_for_user(user_id: str) -> str:
bucket = hash(user_id) % NUM_SHARDS
return f"orders-shard-{bucket}"
Sharding by user_id co-locates a customer's order history on one shard — "my orders" never crosses shards. Global admin reports aggregate asynchronously from all shards.
| Strategy | Mechanism | Risk |
|---|---|---|
| Hash | hash(key) % N | Even spread; range queries fan out |
| Range | user_id 1–1M on shard A | Hot ranges if IDs cluster |
| Directory | Lookup service maps key → shard | Lookup service is critical path |
| Geographic | EU users on EU shard | Compliance win; cross-region queries hard |
Hash
Mechanismhash(key) % NRiskEven spread; range queries fan outRange
Mechanismuser_id 1–1M on shard ARiskHot ranges if IDs clusterDirectory
MechanismLookup service maps key → shardRiskLookup service is critical pathGeographic
MechanismEU users on EU shardRiskCompliance win; cross-region queries hard
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 2 sections
- Cross-shard challenges
- Migration ladder
Test yourself
Answer these before moving on — recall is what makes it stick.