Skip to content
We areTOP 1%

What is sharding, and how do you choose a shard key?

MediumConceptDatabases: Replication & Sharding
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

Sharding (horizontal partitioning) splits a table's rows across several database nodes, each holding a subset, so that storage and write capacity grow with the number of nodes. The shard key determines which node holds each row.

  • Range-based: each shard owns a contiguous key range; range queries are efficient, but sequential keys such as timestamps create a hot shard.
  • Hash-based: a hash of the key picks the shard; load spreads evenly, but range queries must visit every shard.
  • Directory-based: a lookup service maps keys to shards; flexible, but it adds a dependency.
  • A good shard key has high cardinality, distributes load evenly, and appears in most queries so each request touches one shard. For example, shard a chat app by conversation ID, not by message timestamp.

More in Databases: Replication & Sharding

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account