Search

Sharding

What Is Sharding?

Sharding distributes different subsets of a dataset across machines. Replication copies data; sharding divides it. A system may use both, with replicas protecting each shard.

Choosing a Shard Key

The key affects where data lives and which servers a query must contact. A useful key distributes load while keeping common queries targeted.
•
Range-based: neighboring values stay together. Range queries can be efficient, but growing values or busy ranges can create hotspots.
•
Hash-based: hashing can spread values more evenly, but a range query may need to contact several shards.

Example

Sharding orders by customer_id can keep one customer’s orders together. A query for all orders in a date range may still need several shards. A single unusually busy customer can also concentrate traffic. Choose the key from actual access patterns, not only the number of rows.

Partitioning vs. Sharding

Partitioning divides a logical dataset into pieces; sharding usually refers to distributing those pieces across servers. PostgreSQL table partitioning alone does not distribute a table across independent database servers.
Before sharding, consider routing, rebalancing, backups, and cross-shard operations. More machines do not remove the need to preserve application rules.

Reference