Overview
Sharding (horizontal partitioning across nodes) assigns each row to one of several independent servers using a partition-key. Treat it as a last resort after indexing, vertical scaling, caching, and read replicas run out, because cross-shard joins, transactions, and rebalancing add lasting operational cost. Most applications never need it.
Rules
- Pick a shard key that matches the dominant query filter and spreads load evenly; a poor key creates hot shards. A query with the key hits one shard, and one without it fans out (scatter-gather).
- Cross-shard joins and transactions are expensive. Denormalize, or co-locate related tables on the same key such as
tenant_id. - PostgreSQL does not shard natively. Citus, an open-source extension, distributes tables across nodes (Citus 14 supports PostgreSQL 18).
- Each shard needs its own replicas and pool, so total connections multiply by shard count (connection-pool).
- Legitimate triggers: write throughput beyond one node, data volume where vacuum and index maintenance are impractical, or regional data-residency rules. Single-node partitioning solves many “table too big” problems first.
Related
- partition-key: the shard key.
- eventual-consistency: cross-shard reads often relax to it.
- replication-lag: each shard’s replicas lag independently.
- connection-pool: connections multiply per shard.
- postgres: Citus and alternatives.