Course contentsShow
← System Design
Lesson 233 of 1,9197. Database Scaling: ShardingFree lesson

Shard Key Properties

Evaluating shard keys for cardinality, even distribution, query patterns, and immutability requirements.

Shard Key Properties

What you'll learn: The four critical properties to evaluate when choosing a shard key to ensure balanced, efficient database sharding.

You've learned that selecting the right shard key is crucial. But what makes a shard key "good"? There are four essential properties to evaluate:

1. Cardinality (Variety of Values)

Your shard key must have enough unique values to distribute across shards effectively. A boolean field (true/false) would be a terrible shard key—you'd only get two shards maximum! Think of it like sorting mail: using only "North" vs "South" gives you two bins, but using full zip codes gives you thousands of distribution options.

2. Even Distribution

Values should spread evenly across your data. If 90% of users live in one state and you shard by state, one shard gets overwhelmed while others sit idle. Ideal shard keys create roughly equal-sized shards, preventing "hot spots" where one shard handles disproportionate load.

3. Query Pattern Alignment

Your most frequent queries should target specific shards, not scatter across all of them. If you shard by user_id but always query by order_date, every query hits every shard—defeating the purpose. The shard key should appear in your WHERE clauses regularly.

4. Immutability

Once a row is assigned to a shard, you don't want to move it. Changing a shard key value requires migrating the entire row to a different shard—expensive and complex. Choose fields that rarely or never change, like user_id rather than email_address.

Key Takeaway: A good shard key has high cardinality, distributes data evenly, aligns with your query patterns, and remains immutable—balancing these four properties determines sharding success.