Full answer
What and why
Replication copies all data to every node, which scales reads but not writes or storage. Sharding (horizontal partitioning) gives each node a different slice of rows, so writes, storage and memory all spread out. You shard when one primary cannot handle the write rate or the data size, and only after cheaper steps (indexes, caching, read replicas, a bigger machine) are exhausted, because sharding makes queries and operations harder.
Strategies
Range sharding. Rows are split by key ranges: student IDs 1 to 1M on shard A, 1M to 2M on shard B. Range queries (WHERE created_at BETWEEN ...) touch few shards. The risk is hot spots: if the key is a timestamp or an increasing ID, all new writes land on the last shard.
Hash sharding. shard = hash(key) mod N, or better, consistent hashing or a fixed number of virtual partitions. Load is even, but a range query must ask every shard. With plain mod N, changing N moves most keys, so systems usually create many logical partitions (say 1,024) up front and move whole partitions between machines.
Directory (lookup) sharding. A mapping service stores tenant_id -> shard. Any placement is possible, a big tenant can get its own shard, and moving a tenant is a mapping update after copying data. The directory must be highly available and cached.
Choosing a shard key
Look at the most frequent queries. For a learning platform, most queries are "everything for one school" or "everything for one user", so school_id or user_id keeps those queries on one shard.
Check:
- Cardinality: many distinct values, so data can split finely.
- Even load: no single value with a huge share of traffic. One very large school can become a hot shard; give it its own shard or a compound key such as
(school_id, user_id). - Query locality: joins and transactions should stay within one shard where possible.
- Growth: avoid monotonically increasing keys for range sharding.
Costs to state
- Cross-shard queries become scatter-gather, slower and harder to paginate.
- Cross-shard transactions need sagas or two-phase commit.
- Global unique constraints (unique email) need a separate lookup table.
- Resharding means moving data online: double writes or log-based copy, then a cutover.
- Operations multiply: backups, schema migrations and monitoring per shard.
Tools such as Vitess for MySQL or Citus for PostgreSQL handle routing and resharding, and many distributed SQL databases shard automatically by key range with automatic splitting.
By level
Fresher: Explain that sharding splits data across machines and compare range and hash sharding with a simple example. A fresher should be able to name one problem sharding creates.
Mid: Choose a shard key for a given product, explain logical partitions and consistent hashing, and describe what happens to joins, uniqueness and pagination. Expect a defended shard key for the product in the question.
Senior: Plan online resharding, hot tenant isolation, cross-shard transactions, per-shard operations at scale, and the cost of choosing the wrong key, including how to migrate away from it. The senior answer includes a migration plan for a wrong shard key.
Key points
- Sharding splits rows across nodes to scale writes and storage
- Range keeps locality but risks hot spots; hash spreads load but breaks range scans
- Directory sharding is flexible but adds a lookup dependency
- Pick a shard key by query pattern, cardinality and even load
- Pre-split into many logical partitions to make resharding cheaper
Common mistakes
- Sharding before trying indexes, caching or replicas
- Using a timestamp or auto-increment ID as a range key and creating a write hot spot
- Using hash mod N and moving most data when N changes
- Forgetting global uniqueness and cross-shard queries
Follow-ups
- How would you move a tenant from one shard to another with no downtime?
- How do you enforce a unique email across shards?
- What would you do about one tenant that is 30% of all traffic?
Tips
- Say why you are sharding before how
- Pick a concrete shard key for the stated product and defend it
- List at least two costs of sharding unprompted
Evaluation rubric
- Clear definition contrasted with replication
- Correct trade-offs of the three strategies
- Shard key choice justified by query patterns and load
- Discusses resharding, hot spots and cross-shard costs