Sharding in System Design Interviews: When to Use It and What to Say
When an interview asks you to design a high-traffic service, the first thing they want to see is how you keep a single request fast as traffic grows. Sharding is the go-to technique for spreading load across many machines while keeping each request on a single path.
One-Sentence Definition
Sharding is the practice of partitioning a large data set into disjoint “shards” that are stored on separate machines, each responsible for a subset of the keys.
The Problem Sharding Solves
A monolithic database can become a bottleneck for three reasons:
- Storage limit – a single node may run out of disk.
- Throughput limit – write or read capacity saturates the node’s CPU, network, or IOPS.
- Hotspot – a skewed key distribution concentrates traffic on a single partition, raising latency.
Imagine you are building a URL-shortening service that creates 10 million new links per day. That translates to roughly 115 requests per second (10 M ÷ 86 400). While 115 RPS looks modest, a popular service often sees bursts an order of magnitude higher, and each write must be durable. A single relational instance can absorb that write rate, but at roughly 3.65 billion new links a year the table and its indexes eventually outgrow one machine, and it gets harder to keep the latency below 100 ms.
How Sharding Works – Step by Step
Below is a minimal worked example using a key-value store for a social-media feed.
- Choose a shard key – pick a field that distributes evenly. For a feed,
user_idworks because users are roughly uniformly distributed. - Define the hash function – e.g.,
hash(user_id) mod NwhereNis the current number of shards. - Create shard map – a small metadata table that maps hash ranges to physical nodes (Shard 1 → Node A, Shard 2 → Node B, …).
- Routing layer – a lightweight service (often called a proxy or router) computes the hash for each incoming request and forwards it to the appropriate node.
- Write path – the client sends
POST /feedwithuser_id. The router hashes the ID, forwards the request to Node B, which writes the entry to its local storage. - Read path – a
GET /feed?user_id=123follows the same hash, hits Node B, and returns the data. - Rebalancing – when traffic grows, you add a new node (Shard 4). The router updates the shard map, and a background job migrates the relevant hash range from existing nodes to the new node.
Small Worked Example
| Step | Input | Hash (user_id % 4) | Target Shard |
|---|---|---|---|
| 1 | user_id=7 | 7 % 4 = 3 | Shard 4 (Node D) |
| 2 | user_id=12 | 12 % 4 = 0 | Shard 1 (Node A) |
| 3 | user_id=5 | 5 % 4 = 1 | Shard 2 (Node B) |
The table shows that with four shards, each request lands on a deterministic node. Adding a fifth node would change the modulus to 5, which remaps most keys; that is why real systems use consistent hashing or a fixed number of logical shards.
Classic Interview Questions Where Sharding Appears
- Design a Twitter-like timeline – Sharding appears in the storage layer for user tweets. The shard key is
user_id; each node holds a subset of users’ tweet logs. - Design a global e-commerce catalog – Product data is sharded by
product_idor geographic region to keep lookup latency low for regional traffic. - Design a URL shortener – The short code is the shard key; each node stores a disjoint range of codes, allowing the service to generate millions of links per day without a single point of contention.
In each case, the interviewer expects you to explain where the partition occurs (usually the database or key-value store) and how the router decides the shard.
Trade-offs and the Alternative Interviewer May Probe
| Aspect | Sharding | Replication (Read-only) |
|---|---|---|
| Write scalability | Improves linearly with shards | Limited – all writes go to primary |
| Read latency | Low if request hits correct shard | Low if replica is close, but may be stale |
| Complexity | High – need routing, rebalancing, consistency handling | Moderate – simpler client logic |
| Fault tolerance | Depends on replication within each shard | Built-in if replicas exist |
| Operational overhead | Managing shard map, migrations, monitoring hot spots | Managing replica lag, failover |
Interviewers often ask you to compare sharding with replication or caching. Highlight that replication solves read-heavy workloads, while sharding solves write-heavy or storage-heavy workloads. Mention that a hybrid approach (sharded clusters each with replicas) is common in large systems.
Common Mistakes Candidates Make
- Choosing a poor shard key – Selecting a timestamp or sequential ID creates hotspots because recent keys all map to the same shard.
- Assuming sharding eliminates all scaling problems – Forgetting that each shard still needs its own capacity planning, and that cross-shard queries become expensive.
- Skipping the router discussion – Not describing how the system knows which shard to hit leaves a gap in the design.
- Over-engineering the rebalancing – Claiming automatic, zero-downtime migration without acknowledging the need for background copy and consistency checks.
- Treating sharding as a silver bullet – Ignoring the added operational burden and the need for monitoring key distribution.
30-Second Elevator Pitch
"Sharding splits a large dataset across independent nodes by hashing a well-chosen key, like user_id. The router computes the hash, forwards the request to the responsible node, and each node stores only its slice of data. This lets us scale writes and storage linearly, but it adds complexity: we need a shard map, a routing layer, and a plan for rebalancing when we add nodes. In interviews, you’ll often bring it up for designs such as a Twitter timeline, a URL shortener, or a global catalog, and you should be ready to compare it against replication or caching."
How to Practice This
- Study the worked design – In the System Design AI library, find the sharding design board and walk through each component’s notes.
- Sketch your own diagram – Use the canvas to draw the router, shard map, and a few shards for a sample service like a URL shortener.
- Run a mock interview – Explain the design out loud, then ask the AI coach to review your canvas and give feedback on the shard key choice and rebalancing plan.
Questions people ask
When should I suggest sharding in an interview?
When the problem involves high write volume, large data size, or a risk of hotspot keys. Typical triggers are a sustained write rate beyond what one well-provisioned primary can absorb, or data that will not fit on a single node; a few million writes a day is only tens per second and does not need sharding.
How do I pick a good shard key?
Choose a field that is immutable, uniformly distributed, and used in most queries. user_id, customer_id, or a hashed version of a natural key are common choices.
What is the simplest way to implement a router?
A lightweight stateless service that applies the same hash function used to assign shards. It can be a microservice or a library embedded in the API gateway.
Can I change the shard key after the system is live?
It is extremely painful because it requires moving all data to a new partitioning scheme. Most designs lock the shard key early and avoid changing it.