MediumExperience level: 3-5 years · 8-15 years1 min read

How does database sharding work, and what makes a good shard key?

Reviewed by Gurusankar M.

Asked inAmazonGoogleMicrosoft
#sharding#partitioning#shard key#hotspot#scaling
Report issue

Short Answer

Sharding splits data across independent databases by a shard key (hash or range), so each shard holds a subset — scaling writes/storage horizontally. A good shard key has high cardinality and even access; a bad one creates hotspots and forces cross-shard queries (which are slow and lose transactions).

On this page

Concept Made Simple

Coffee Chat Question

“How does database sharding work, and what makes a good shard key?”

Remember It Faster

Mind Map Answer

Hash sharding
even spread, no range scans
Range sharding
range scans, risk of hotspots
Good key
high cardinality, even access
Cost
cross-shard joins/txns are hard

Think Beyond the Expected

What If?

You shard orders by date and the current shard is overwhelmed — why?

Range-by-date puts all NEW writes on the 'current' shard — a hotspot — while old shards sit idle. Shard by a high-cardinality key (customer_id hash) to spread writes evenly. Range sharding suits time-range reads but concentrates recent writes.

Real World

Sharding is the last resort after replicas/caching; shard-key choice is the make-or-break decision — a bad key means hotspots, cross-shard fan-out, and painful resharding.

Interviewer's Expectation

Keywords they're listening for:

  • split by shard key
  • hash vs range
  • high-cardinality even key
  • hotspots
  • cross-shard query/txn cost

Common Mistakes

  • Low-cardinality or time-based shard keys (hotspots)
  • Frequent cross-shard joins
  • Sharding before exhausting replicas/caching

Best Practices

  • Pick a high-cardinality, evenly-accessed key
  • Avoid cross-shard transactions
  • Plan for resharding (consistent hashing)

Follow-up Questions

  1. How do you handle cross-shard queries?
  2. How does resharding work without downtime?
  3. Hash vs range sharding trade-offs?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Databases (System Design)
Interview question: "How does database sharding work, and what makes a good shard key?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.