Medium👤 3-5 years👤 8-15 years 1 min read

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

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).

Coffee Chat Question

Concept Made Simple

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

🧠Mind Map Answer

Remember It Faster

Hash shardingeven spread, no range scans
Range shardingrange scans, risk of hotspots
Good keyhigh cardinality, even access
Costcross-shard joins/txns are hard

🔥What If?

Think Beyond the Expected

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 keyhash vs rangehigh-cardinality even keyhotspotscross-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

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

🧩Related Technologies

consistent hashingVitessCitusread replicas

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions