Sharding: Follow-Up Questions Interviewers Ask and How to Answer
When you drop the word sharding in a design interview, the interviewer instantly knows you’re thinking about scaling data. That alone isn’t enough; they need to see whether you understand the consequences of splitting data across many machines.
Interviewers therefore follow up with a series of probes. Each question digs deeper: from the basics of how you pick a shard key to the hardest parts like cross-shard transactions and operational monitoring. Answering them clearly shows you can move from a high-level idea to a production-ready system.
Choosing the Right Shard Key
1. How do you pick a shard key for a user-profile service?
I start by looking for a key that distributes writes evenly and keeps related reads together. For a profile service, the user ID is a natural choice because it’s unique, immutable, and accessed together with the user’s settings. It gives roughly uniform distribution if we hash the ID, which avoids hot spots. If we anticipate range queries by signup date, I would not put the date into the hashed key, because hashing destroys ordering; I would keep userId as the shard key and serve those date queries from a secondary index or an analytics store.
2. What if the key you chose creates a hot spot?
If a hot spot appears—say a small set of users generate most traffic—I would add a secondary dimension to the key. For example, prepend a random bucket number to the user ID, turning userId into bucket:userId. With ten buckets, traffic spreads across ten shards, reducing the per-shard load by roughly an order of magnitude. I’d also monitor write rates per bucket and adjust the bucket count dynamically if a new hot spot emerges.
Rebalancing and Scaling
3. How do you handle rebalancing when a shard grows too large?
I use a split-and-move strategy. When a shard crosses a threshold—say 150 GB of data—I split its key range in half and create a new shard for the upper half. The system then streams the affected partitions to the new node while keeping the original shard online. To avoid downtime, I route new requests to a load balancer that knows the new key ranges, and I use a background job to catch up any missed writes during the transfer.
4. Can you add a new shard without moving existing data?
Not entirely, but I can keep the movement small. With plain hash-modulo, changing the node count remaps most keys. I would use a consistent-hash ring so only about 1/N of the keys move when the Nth node joins. The system updates the ring metadata in a coordination service (e.g., ZooKeeper) and gradually redirects the affected keys, keeping the rest untouched.
Consistency and Transactions
5. How do you guarantee consistency for a transaction that spans two shards?
Cross-shard transactions are tricky. I would use a two-phase commit (2PC) orchestrated by a transaction manager. The manager first sends a prepare request to both shards; each shard writes a tentative record to a local log and replies with a vote. If both vote commit, the manager sends a commit message; otherwise it aborts. To limit the impact of failures, a shard that times out before it votes simply aborts; once it has voted yes, it must hold its locks until it learns the decision, so I make the coordinator’s decision log durable and replicated (or run the coordinator on a consensus group) so a crashed coordinator can be replaced and in-doubt shards resolved.
6. What if you need strong consistency but can’t afford 2PC latency?
In that case I would redesign the data model to avoid cross-shard writes. For example, denormalize related data into a single shard using a composite key that groups the entities together. If that’s not possible, I could use a saga pattern: break the operation into a series of local transactions with explicit compensation steps, which gives eventual consistency while keeping each step fast.
Operational Concerns
7. How do you monitor and detect a failing shard?
I instrument each shard with metrics like request latency, error rate, CPU, memory, and disk I/O. These feed into a monitoring system (e.g., Prometheus) that triggers alerts when any metric exceeds a threshold—for instance, latency > 200 ms for more than five minutes. I also expose health-check endpoints that a failover controller polls; a failed check promotes that shard’s replica, because no other shard holds its keys and simply removing it from traffic would make that slice of the data unavailable.
8. What’s your strategy for backup and disaster recovery?
Each shard writes its own write-ahead log (WAL) to durable storage (e.g., S3) and takes periodic snapshots of its data volume. I replicate logs to a different availability zone to survive zone loss. In a disaster, I spin up a new shard from the latest snapshot and replay the WAL to bring it up to date. Restoring a single shard takes roughly as long as copying its snapshot and replaying its log, and because shards restore in parallel, the whole-system RTO is close to that of the slowest shard rather than growing with the shard count.
Scaling the Sharding Layer Itself
9. How do you avoid the sharding layer becoming a bottleneck?
I keep the sharding metadata in a highly available, low-latency store like DynamoDB with strong read consistency. The metadata is cached in each application instance with a short TTL (e.g., 30 seconds) to reduce lookup traffic. For write-heavy workloads, I partition the metadata itself by using a secondary hash on the shard identifier, ensuring that metadata reads and writes are spread across many partitions.
Senior vs. Staff Answers
A senior-level answer shows solid knowledge of the mechanics: clear shard-key selection, practical rebalancing steps, and a sensible monitoring plan. A staff-level answer goes further. It connects sharding decisions to broader system goals—cost, latency SLAs, and team ownership. It also discusses trade-offs across the entire stack, such as how the choice of consistent hashing influences client SDK complexity, or how cross-shard transaction patterns affect data-model evolution. Staff candidates anticipate future growth, propose automation for rebalancing, and articulate how the sharding strategy aligns with product roadmaps.
How to practice this
- The worked design for write scale, Design for Write Scale: A Million Writes a Second, is part of Pro ($10/month): a live TV vote at a million writes a second. Note how it spreads writes by key, buffers in Kafka and splits the hot counter across shard items, and map those choices to the probes above.
- On paper or a whiteboard, sketch a full sharding solution for a service of your choice, labeling the shard key, rebalancing flow, and monitoring components.
- Check your sketch against the probes above and iterate on the answers until you can explain each probe in under two minutes.
Questions people ask
What is a good shard key for a time-series logging service?
Partition by a hash of deviceId and sort by timestamp within each partition. That spreads writes across devices while keeping one device’s logs together and in time order for range queries; a very hot device can add a time bucket to the partition key.
How often should you rebalance shards?
Rebalance when a shard’s size or request rate exceeds a predefined threshold, such as 150 GB or 2 k req/s. Regular checks (e.g., every hour) help catch growth before it becomes a hotspot.
Can sharding be applied to relational databases?
Yes. You split rows by a key column across separate database servers and route each query by that key, either in the application or through a middleware layer such as Vitess for MySQL or Citus for PostgreSQL. Built-in table partitioning splits a table within one server, so on its own it is not sharding.
What is the impact of adding a new shard on existing client code?
If you use consistent hashing, client libraries only need to refresh the hash ring metadata. With a small TTL cache, the impact is minimal and transparent to most request paths.