Sharding and Replication
Sharding and replication are the two foundational approaches to building distributed data systems. They solve different problems β but most production systems combine them.
Single Instance β The Starting Point
Everything happens on one node. Strong consistency, simple reasoning, but no horizontal scalability. Examples: single-instance Postgres, Redis without clustering, monolithic app servers.
The only option for handling more load is vertical scaling β bigger CPU, more memory, more storage. Has physical and economic limits.
Replication
Single-leader replication
The simplest replication setup: one leader (a.k.a. primary, master, write replica) accepts all writes. Followers (read replicas, hot standbys) receive a stream of changes from the leader and serve reads.
Examples: Postgres streaming replication, MySQL master-slave, MongoDB replica sets, Redis Sentinel.
Multi-leader replication
Multiple nodes accept writes simultaneously. Each leader replicates to others; each leader can also have its own followers.
When you need it:
- Geographic distribution. Each region has a local leader for low-latency writes.
- Collaborative editing. Mobile devices that can be offline for days are essentially leaders that sync.
- High write availability. No single write bottleneck.
The cost: conflict resolution. When the same data is written at two leaders concurrently, you must decide whose change wins.
Multi-leader systems generally cannot guarantee linearizability. Google Spanner uses atomic clocks (TrueTime) to approximate it, but thatβs specialized hardware and serious operational complexity.
Leaderless replication
Every node accepts both reads and writes. No special coordinator. Used by Cassandra, DynamoDB, Riak.
Quorum consistency. With N replicas, if you read from R replicas and write to W replicas where R + W > N, youβre guaranteed to read the latest value.
N = 3 replicas
W = 2 (write to 2)
R = 2 (read from 2)
W + R = 4 > N = 3 β guaranteed consistent reads
Sharding
Sharding (also called horizontal partitioning) splits data across nodes. Each node holds only a slice. Writes scale horizontally because different shards can accept writes in parallel.
Sharding strategies
The hot-spot problem
A single popular key (a celebrity user, a viral product) can overwhelm one shard. Mitigations:
- Salting keys β append random suffix to spread writes across shards.
- Read replicas of hot shards β let reads scale even if writes are concentrated.
- Caching layer β most hot data should never reach the database.
Resharding
Adding or removing shards is operationally expensive. Strategies:
- Pre-split shards β start with more shards than you need, redistribute as you grow.
- Logical shards over physical β many logical shards on fewer physical nodes; rebalance by moving logical shards.
- Online migration tools β Vitess for MySQL, Citus for Postgres handle this with minimal downtime.
Combining Sharding and Replication
Most production systems use both:
- Each shard is replicated for fault tolerance and read scaling.
- Different shards live on different nodes for write scaling.
Shard A (users a-h) Shard B (users i-p) Shard C (users q-z)
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
β Leader A β ββreplicationββΊβ Leader B β β Leader C β
β Follower A1 β β Follower B1 β β Follower C1 β
β Follower A2 β β Follower B2 β β Follower C2 β
βββββββββββββββββ βββββββββββββββββ βββββββββββββββββ
Each shard is independent. A query for user βkβ routes to Shard B; cross-shard queries (e.g., βgive me all usersβ) fan out to all shards.
Multi-Tenant SaaS Patterns
For multi-tenant SaaS, shard-by-tenant is often the right model:
Recap
- Replication = same data on multiple nodes (availability + read scaling).
- Sharding = different data on different nodes (write scaling).
- Production systems combine them β each shard replicated for fault tolerance.
- Three replication models: single-leader (simple, scales reads), multi-leader (writes scale, conflicts), leaderless (symmetric, quorum-based).
- Sharding strategies: range-based, hash-based, directory-based, consistent hashing. Each has trade-offs.
- For multi-tenant SaaS, shard-by-tenant (with hybrids for big customers) is usually the right model.