Lesson 3 of 15
15. Sharding

15.2 How do you choose the shard key?

3 min read+10 XP

At the exam centre: The rule decides everything. Split by roll number and every hall gets a fair share. Split by surname initial and the "S" hall overflows. Split by date of registration and all the late students crowd the newest hall.

In the system: The shard key (or partition key) is the column used to decide which shard a row lives on. It's the most important sharding decision, because it decides:

  1. Even spread: a good key has many distinct values and spreads reads and writes evenly. A bad key (country, status, today's date) piles traffic onto a few shards.
  2. Which queries stay on one shard: a query that includes the shard key goes to one shard; a query without it must ask all of them (15.6). So pick the key from your most frequent queries.
  3. What can be done together: transactions and joins are easy only within one shard. Data that's changed or read together should share a shard key.

For the bookstore's orders, user_id is a strong choice:

  • millions of users, so plenty of spread;
  • the hottest queries ("my orders", "my cart", checkout) all include the user;
  • one user's orders, items and payments can live together on one shard.

What it makes harder: "all orders for book 42" or "today's sales" now span every shard. That's fine for admin and analytics, which can use a separate copy built for them (15.6).

Memory code: "Pick the key your busiest questions already contain, with many values and even traffic."