Databases & StorageAdvanced17 min read2 questions

Database Replication and Sharding

How one database becomes many. Replication for read scale and availability, sharding for write scale, and the operational traps in both.

Covers: Leader-follower replication, replication lag, read-your-writes, partitioning strategies, hot shards, resharding

There are exactly two ways to make a database handle more: copy it (replication) or split it (sharding). Replication scales reads and buys availability. Sharding scales writes and storage. They solve different problems, they are usually used together, and confusing them is a reliable way to lose an interview.

Filter
0/2 mastered
Intermediatereplicationconsistencyread-your-writesAsked at Meta, GitHub

30-second answer

The write went to the primary and the subsequent read was served by a replica that had not yet applied it - replication lag. The fix is read-your-writes consistency, and there are four standard approaches: route reads to the primary for a short window after a user writes, pin that user's session to the primary, have the client pass the log position it wrote at and wait for a replica to catch up, or make the UI optimistic and render the value it just submitted. Route-to-primary-after-write is the usual pragmatic choice.

AdvancedshardingpartitioningscalingAsked at Uber, Discord

30-second answer

Split data across independent databases by a shard key. Range partitioning keeps ranges together - great for range scans, terrible for hotspots, since recent data all lands on one shard. Hash partitioning distributes evenly but destroys range queries. Directory partitioning uses an explicit lookup table, giving full flexibility at the cost of an extra hop and a component that must not become a bottleneck. Choose a key that is present on almost every query, has high cardinality, and distributes evenly - and accept that queries not containing the key must scatter-gather across every shard.

Showing 2 of 2 questions for database-replication-and-sharding.

Check your understanding

4 questions · no sign-up, nothing stored

0/4 answered
Question 1

1.A user updates their name and the next page load shows the old one. Most likely cause?

Question 2

2.You shard by `hash(user_id) mod 8` and need to add a ninth shard. What happens?

Question 3

3.Which is the strongest reason NOT to shard yet?

Question 4

4.A celebrity account makes one shard 50× hotter than the others. Which is NOT a valid mitigation?

Hands-on challenge

Build it - this is what you talk about in a deep-dive round.

Shard a messaging system's storage

A chat product stores 50 billion messages, ingests 100,000 writes/second, and must load any conversation's last 50 messages in under 50 ms.

Requirements

  • Choose a shard key and defend it against all four tests: query coverage, cardinality, distribution, colocation.
  • Specify the partitioning strategy and the logical-to-physical shard mapping.
  • Show how 'last 50 messages in a conversation' is served by exactly one shard.
  • Design globally unique, time-sortable message IDs.
  • Handle a 500,000-member channel that dwarfs every other conversation.

Stretch goals

  • Design full-text search across all shards without scatter-gathering on every query.
  • Write the runbook for splitting the hottest shard in two with no downtime.