Database Sharding, Partitioning, and Rebalancing
Master horizontal database sharding, partition key strategies, consistent hashing, and how to conquer the scatter-gather query penalty.
Replication scales read throughput by fanning queries out to multiple read-only replicas. However, replication does not scale write throughput: every write must still be processed and committed by the primary leader node.
Furthermore, when your total dataset grows from hundreds of gigabytes to tens of terabytes, storing the entire dataset on a single physical machine becomes economically prohibitive or physically impossible.
Sharding (horizontal partitioning) breaks a massive database into smaller, independent subsets called shards. Each shard runs as a separate database instance holding a unique slice of the total data, allowing both storage capacity and write throughput to scale horizontally.
01Horizontal Sharding vs Vertical Partitioning
Architects divide data across two distinct axes:
- Vertical Partitioning: Splitting a database by domain boundaries or individual table columns. For instance, moving
usersandbillinginto separate database clusters, or splitting a heavyuser_profilestable so that high-frequency columns (id,username,password_hash) live on fast NVMe drives, while large rarely accessed columns (bio,avatar_blob) live on cheaper storage. - Horizontal Sharding: Splitting rows of the exact same table across multiple distinct physical servers. Every shard shares the identical table schema, but Shard 1 stores users 1 through 1,000,000, while Shard 2 stores users 1,000,001 through 2,000,000.
Loading the rest of the lesson…