Database Sharding
Definition
Database sharding is a horizontal scaling technique that distributes data across multiple independent database instances (shards), with each shard holding a non-overlapping subset of the total dataset.
Introduction to Database Sharding
Sharding is the approach used by the world's largest databases — social media platforms, financial systems, and global SaaS applications — when a single database server can no longer handle the data volume or write throughput. Unlike replication (which copies data), sharding splits data into distinct partitions stored on separate servers.
Key Takeaways
- Each shard holds a distinct subset of data — shards don't share data
- Shard key determines which shard a row belongs to
- Range-based sharding: shard by value ranges (user_id 1-1M on shard 1, 1M-2M on shard 2)
- Hash-based sharding: hash(shard_key) % num_shards — even distribution but no range queries
- Cross-shard queries (JOINs across shards) are expensive and complex
- Resharding (rebalancing) is complex and requires careful planning
Real-World Examples & SQL Schema
Hash-Based Shard Routing
-- Application-layer shard routing logic -- Determine which shard to query based on user_id -- Pseudo-code for shard selection: -- shard_id = user_id % num_shards -- connection = get_connection(shard_id) -- Example with 4 shards: -- user_id = 42: 42 % 4 = 2 → Shard 2 -- user_id = 99: 99 % 4 = 3 → Shard 3 -- user_id = 100: 100 % 4 = 0 → Shard 0 -- Query runs on the correct shard: SELECT * FROM users WHERE user_id = 42;
Hash-based sharding distributes data evenly but requires knowing the shard key for every query.
Run code in PlaygroundPrimary Use Cases
Databases that have outgrown vertical scaling limits
Write throughput beyond what a single primary can handle
Datasets too large to store on a single server
Multi-tenant SaaS applications (each tenant on a separate shard)
Geographic data distribution requirements (users in EU on EU shards)