Sharding vs Replication: How to Answer "How Would You Scale the Database?" in a System Design Interv
4 views
Almost every system design interview reaches the same moment. You have drawn clients, a load-balanced service tier and a single database box, and the interviewer asks: "Traffic grows 50x. What happens to the database?" Many candidates answer with one word, "shard", and move on. That is usually where points are lost, because the interviewer wants to see that you know the difference between scaling reads and scaling writes, and that you can reason about the cost of each option.
Step 1: Ask what is actually growing
Before naming a technique, ask for the read/write ratio and the data size. A product catalog might be 99% reads and fit comfortably on one machine. A chat or telemetry system might be write-heavy and grow by terabytes a month. Saying "let me check whether the bottleneck is reads, writes, or storage" shows structured thinking in a single sentence.
Step 2: Replication scales reads, not writes
With leader-follower replication, every write goes to one leader and is copied to several read replicas. It is simple, most managed databases support it out of the box, and it also gives you failover. The trade-off to mention is replication lag: a user who updates a profile and immediately reloads the page might read from a stale replica. Good answers name a fix, such as reading your own writes from the leader for a few seconds, or routing a session to one replica. Multi-leader setups exist, but bring them up only if the interviewer pushes on multi-region writes, and then mention conflict resolution.
Step 3: Sharding scales writes and storage
When a single leader cannot absorb the write volume, or the data no longer fits on one node, you split the data across shards. The interesting part is the shard key. Explain the options briefly:
- Range-based keys (for example by user ID range or date) are easy to reason about and support range queries, but can create hot spots when new data always lands on the newest range.
- Hash-based keys spread load evenly but make range queries expensive.
- Directory or lookup-based sharding gives flexibility at the cost of an extra component to keep highly available.
Then pick one for the system in front of you and justify it. For a messaging app, sharding by conversation ID keeps all messages of a chat together, so the most common query hits one shard.
Step 4: Say what gets harder
Interviewers reward candidates who volunteer the downsides: cross-shard joins become application logic, transactions across shards need careful design or should be avoided, and rebalancing when a shard fills up must be planned. Mentioning a celebrity or hot-key problem and a mitigation, such as splitting very large tenants, is a strong signal of experience.
A compact script you can reuse
"Reads are the bottleneck, so I will add read replicas first and handle replication lag with read-your-writes. If writes or storage outgrow a single leader, I will shard by X because our main query filters on X, and I accept that cross-shard reporting moves to an async pipeline."
Practicing this out loud matters more than memorizing definitions, because under time pressure it is easy to skip the trade-offs. Tools like Linkjob AI (https://linkjob.ai) give real-time help during live job interviews and technical interviews, which can be a useful safety net when a follow-up question on shard keys catches you off guard. The goal is the same either way: show the interviewer a clear chain of reasoning from the workload to the design.