Database Sharding
A horizontal scaling architecture that partitions a large dataset across multiple independent database servers using a shard key to scale writes and storage
Table of Contents
Structure Diagram
What is Database Sharding?
A horizontal scaling architecture that partitions a large dataset across multiple independent database servers using a shard key to scale writes and storage. It is a core concept in Databases & Caching within Web Dev.
Core Principles
Read replicas and caches scale reads, but write throughput and multi-terabyte storage eventually hit the ceiling of a single database machine. Sharding horizontally partitions rows of a table across multiple independent database nodes (shards) using a Shard Key (such as user_id or tenant_id).
Each shard owns a disjoint subset of the data, allowing total write capacity and storage to scale linearly as nodes are added.
Practical Takeaways
- Treat sharding as a last resort: Cross-shard
JOINs, distributed ACID transactions, global uniqueness, and schema migrations become dramatically harder. Exhaust indexing, caching, read replicas, vertical scaling, and single-node partitioning first. - Pick your Shard Key with extreme care: Choose a high-cardinality key aligned with your primary access patterns to avoid hot shards (where one giant tenant overwhelms a single node) and scatter-gather queries.
- Plan for resharding: Naive
id % Nhashing forces massive data movement when adding nodes. Use Consistent Hashing, logical virtual buckets, or battle-tested systems like Vitess or Citus.
Notable Examples
- Slack & YouTube (Vitess): Sharding MySQL by workspace/channel ID to handle millions of concurrent writes.
- Geo-sharded SaaS: Partitioning customer data by region (US/EU/APAC) to satisfy data residency laws and lower latency.