Video in progress

What problems come up if the sharding key is chosen badly?

Data and load spread unevenly — one "hot" shard fills up while the others sit empty. Another problem: many queries fan out to all shards (scatter-gather) and get slow.

📖 Answer

Typical bad choices.

By date (the month of created_at): all new records land on the last shard, which struggles with the writes, while the old shards barely work.

By country or city: if 60 percent of the users are in Tashkent, one shard ends up several times bigger than the others.

A low-cardinality field (status): only 4 values — you can't split into more than 4 shards.

A big tenant: in a B2B system split by tenant_id, one customer is bigger than all the others combined. It fills its own shard.

The other side is the queries. If orders are split by user_id, "a user's orders" comes from one shard. But "a seller's orders" requires asking all the shards and merging the results. If there are many queries like that, you need a second view (a separate table by seller, or a search index).

A good key:

  • has many values, evenly spread (a hash of user_id);
  • is what the most frequent queries come in with;
  • keeps data that changes together on one shard (an order and its lines).

Changing the key later is very expensive — it means moving all the data. So don't choose it without knowing the query patterns.