← Lessons

quiz vs the machine

Gold1500

Databases

Sharding Key Choice

Picking the partition key that avoids hotspots and cross shard joins.

6 min read · core · beat Gold to climb

Why The Key Matters

When you shard a database, the sharding key decides which node holds each row. A good key spreads data and load evenly. A bad key creates a hotspot where one shard does most of the work while others sit idle.

What Makes A Good Key

  • High cardinality so values spread across many shards.
  • Even access so no single value dominates traffic.
  • It should match the query pattern so most queries hit one shard.

Common Mistakes

  • Sharding by a monotonically increasing timestamp sends all new writes to one shard.
  • A low cardinality field like country crams large groups onto few shards.
  • Keys that ignore queries force expensive cross shard joins and scatter gather.

Key idea

The sharding key must have high cardinality, spread load evenly, and align with query patterns, or you risk hotspots and costly cross shard operations.

Check yourself

Answer to earn rating on the learn ladder.

1. Sharding by an increasing timestamp tends to cause:

2. A good sharding key should:

3. Choosing a key that ignores query patterns leads to: