What is database sharding?

Why Interviewers Ask This

Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.

Answer

Sharding is a horizontal scaling technique that partitions a large database across multiple servers (shards), with each shard holding a subset of the data. Each shard is a separate database instance that can run on its own server, so the total capacity scales with the number of shards. Sharding strategies: (1) Range-based: shard by a value range (user IDs 1-1M on shard 1, 1M-2M on shard 2) — simple but can cause hot spots if one range gets all activity; (2) Hash-based: apply a hash function to the shard key to determine the shard — distributes data evenly, but range queries require hitting all shards; (3) Directory-based: maintain a lookup table mapping records to shards — flexible but the directory becomes a bottleneck. Challenges: cross-shard JOINs are impossible or very complex; transactions across shards require distributed transaction protocols (two-phase commit); rebalancing when adding shards is complex. Sharding adds significant operational complexity. Try vertical scaling, read replicas, caching, and query optimization before sharding. Tools: Vitess (sharding for MySQL, used by YouTube), ProxySQL. MongoDB and Cassandra have built-in sharding support.

Pro Tip

This topic has MySQL / SQL-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.