Skip to main content
Sharding is the practice of splitting your rows across many machines so that no single server stores all the data. This pattern — also called horizontal scaling — distributes data by row across multiple machines rather than replacing one server with a more powerful one (vertical scaling). By adding more ordinary servers and partitioning rows among them, you scale out storage and write throughput. Each server holds a subset of the data; that subset is called a shard. A shard can be a physical machine, a virtual instance, or a logical partition. Typically, all shards use the same table layout and schema so application queries can target the same table shape on every shard. For example, the shards below all share the same schema. Leo’s record is stored entirely on one shard, while Chris’s record is stored on a different shard. Both shards expose the same columns (username, follower count, etc.), making the application logic uniform across shards.
An infographic showing a sharded database: four cylinder icons labeled Shard 0–3 with sample tables of user records (ID, username, followers) beneath them. Two user cards for "leo" and "chris_d" are highlighted to show how records map to shards.
How the system assigns a row to a shard depends on the sharding rule (the shard function). A simple, deterministic sharding rule John and Alan might use for four machines is:
  • Take the user’s numeric ID.
  • Divide it by 4 and take the remainder (the modulo).
  • Use that remainder to choose the shard (0–3).
The remainder of division by 4 is always one of 0, 1, 2, or 3. For example:
  • 1 % 4 == 1
  • 2 % 4 == 2
  • 3 % 4 == 3
  • 4 % 4 == 0
A compact implementation of this mapping in Python looks like:
If the remainder is 0, the row goes to shard 0; if it is 2, the row goes to shard 2, and so on. When Chirp (or any application) used a single primary database, that primary could only accept so many writes per second. By splitting users across four machines, Chirp effectively has four primaries — each machine handles writes for the shard(s) it owns. If four shards are insufficient, you can increase to eight, sixteen, or more, improving write throughput and storage capacity.
A diagram titled "Scaling Writes" showing an app at the top routing write requests to four separate database shards labeled Shard 0–3. Each shard is shown as a cylinder marked "Primary" and containing different row entries (row 1–8).
This horizontal distribution of writes is the main reason teams adopt sharding. However, splitting data across shards introduces operational and application-level challenges, including:
  • Routing: Determining which shard holds a particular row.
  • Cross-shard queries and joins: Combining data that lives on different shards can become complex and expensive.
  • Balancing and hot spots: Some shards may receive more traffic or grow larger than others.
  • Re-sharding (resizing the cluster): Moving data between shards without downtime is hard.
  • Backups, restores, and operational complexity: More machines means more components to manage.
Which of these trade-offs you face — and how hard they are to solve — depends on the sharding rule you choose. Deterministic functions like user_id % num_shards make routing simple (the application or a routing layer computes the shard), while other strategies (range-based sharding, consistent hashing, directory-based mapping) change the operational trade-offs for balancing and re-sharding.
A shard is not a different schema — it’s the same table layout stored on different machines or partitions. The sharding function (for example, user_id % num_shards) determines where each row lives. Proper shard selection simplifies routing and reduces cross-shard operations.

Quick comparison: Horizontal vs Vertical scaling

Watch Video