Skip to main content
PreparationZone Logo
PrepZone

Partitioning and Sharding

Vertical vs horizontal splits, hash vs range keys, and rebalancing without downtime.

Pass checkpoint below

Read these first

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

Vertical
usersid, name, email
user_profilesbio, avatar
Horizontal
Shard Auser_id 0–999
Shard Buser_id 1000–1999
Vertical splits tables by column group. Horizontal splits rows across nodes by key.

Two partitioning styles

  • Vertical — split wide tables (users vs user_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.

Java
# 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.

StrategyMechanismRisk
Hashhash(key) % NEven spread; range queries fan out
Rangeuser_id 1–1M on shard AHot ranges if IDs cluster
DirectoryLookup service maps key → shardLookup service is critical path
GeographicEU users on EU shardCompliance win; cross-region queries hard
  • Hash

    Mechanismhash(key) % N
    RiskEven spread; range queries fan out
  • Range

    Mechanismuser_id 1–1M on shard A
    RiskHot ranges if IDs cluster
  • Directory

    MechanismLookup service maps key → shard
    RiskLookup service is critical path
  • Geographic

    MechanismEU users on EU shard
    RiskCompliance 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.