When a single database server, even with replicas, can no longer store all your data (terabytes or petabytes), or when write traffic overwhelms a single master, you must split the database into smaller, faster, more manageable pieces. This is called Sharding (or Horizontal Partitioning).
Sharding breaks up a large database table into smaller chunks called Shards, and distributes them across multiple database server instances. Each shard holds a subset of the total data and operates independently.
Users table with 1 billion rows is split across 10 shards. Shard 1 holds users with IDs 1-100M, Shard 2 holds 100M-200M, and so on.A hash function is applied to a key attribute (like UserID). The result determines which shard the data belongs to.
shard_number = hash(UserID) % number_of_shardsData is divided based on ranges of a key value.
A lookup service (directory) maps each entity to its shard.
Sharding introduces significant operational complexity:
JOIN between two tables that live on different physical servers. The application must query both shards separately and merge the results in application code.Unlike horizontal partitioning (sharding), Vertical Partitioning splits a table by columns.
Users table might be vertically partitioned into UserProfile (id, name, bio) on one server and UserMedia (id, profile_picture_blob, video_blob) on another server optimized for large binary storage.