Home/Labs/Shard Key Hotspot Mapper
All 280 Labs
INTERACTIVE LAB🗄️

Shard Key Hotspot Lab (Interactive)

Split 600 customers by user_id or country_code and measure row skew, hot shards, and scatter-gather query cost. Real FNV hashing and weighted country data show how a low-cardinality shard key concentrates 55% of traffic on one server.

Shard Key Choice & Hotspot Distribution

Split 600 customer rows across shards and measure skew and scatter-gather cost per key choice.

Hottest Shard

25.2%

balanced target: 25.0%

Load Skew (CV)

0.00

0 = perfect, >0.5 = danger

Key Cardinality

600

distinct values in the key

Verdict

BALANCED

Shard 1
149 rows (24.8%)
Shard 2
150 rows (25.0%)
Shard 3
151 rows (25.2%)
Shard 4
150 rows (25.0%)
ROUTER TRACE:

Run a query to see how many shards the router must touch.

With country as the shard key, the US bucket alone holds 55% of all rows: one shard melts down while the rest idle, and queries by user_id degrade to full scatter-gather broadcasts. High-cardinality, query-aligned keys are the first rule of sharding.

How It Works Under the Hood

Sharding splits a monolithic table across independent servers, and the shard key decides everything. An ideal key has high cardinality, uniform distribution, and appears in the hottest query filters so the router touches exactly one shard. Choose a low-cardinality column like country_code and the US bucket swallows most rows while other shards idle; choose one that misses the query pattern and every lookup degrades into a scatter-gather broadcast that fans out to all shards, merges in memory, and pays network latency N times.

Core Architectural Principles

  • Hash of user_id across N shards yields near-uniform bucket sizes and single-shard point lookups.
  • Querying on a non-shard key forces scatter-gather: all shards scanned, results merged and re-sorted.
  • Skew metrics (max-shard share, coefficient of variation) expose hot partitions before production does.
Interview Round Script

Treat shard-key selection as the centerpiece of any sharding discussion: high cardinality, no celebrity hotspots, aligned with dominant query filters. Quantify the cost of missing the key: "A WHERE on a non-shard column becomes a broadcast across all fifty shards with in-memory merge." Mention Slack sharding by team_id to colocate 99% of queries.

Key Trade-Offs

Perfect key alignment buys single-shard fast queries; any deviation pays scatter-gather latency or hotspot meltdowns.

Related Curriculum Chapter

Sharding (Horizontal Partitioning) Concepts

Read Full Chapter Blueprint

Explore More Interactive Labs

View All 280 Labs