.NET Architecture & System Design Interview Question #60

How do you choose a shard key?

Database Scaling, Partitioning, NoSQL & Data Consistency Mid-Level Intermediate

Quick Interview Answer

A good shard key distributes workload and data reasonably evenly while aligning with common access patterns. It should avoid concentrating a large percentage of traffic on one shard.

Detailed Explanation

A good shard key distributes workload and data reasonably evenly while aligning with common access patterns. It should avoid concentrating a large percentage of traffic on one shard.

The key also affects query routing and transaction boundaries. A key that distributes perfectly but forces most business operations to query every shard can still be a poor choice.

Code Example

Good shard key:
- high cardinality
- balanced distribution
- common query boundary
- stable over time

Avoid:
one extremely hot tenant/key