Sharding and Replication

11 min read Β· Updated 2026-09-30

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.

Single database instance serving every read and write

Vertical scaling β€” a bigger CPU, more memory, more disk β€” is the only move left, and it ends. Past a point the next machine size does not exist, or it costs more than a second node would. The thing you keep, and cannot get back from any of the diagrams below, is that one node has a single order of writes and nothing to fail over to.

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.

Single-leader replication: writes to the primary, reads from replicas

Every follower holds a full copy, so the only routing rule is β€œwrites go to the leader.” The leader ships changes as a write-ahead log or as logical commands. That buys read capacity. It does not buy write capacity: every mutation still lands on one node, and a write-heavy workload stays blocked on that node no matter how many followers you add.

Writes go to leader
Single point of write coordination. Easy to reason about ordering β€” natural linearizability.
Reads can scale
Spread read load across followers. Great for read-heavy workloads.
Replication modes
Synchronous (slower writes, no data loss) vs. asynchronous (faster, possible staleness).
Failover
Manual or automatic (via Raft, Paxos). Automatic = faster recovery; manual = avoids split-brain.

Examples: Postgres streaming replication, MySQL master-slave, MongoDB replica sets, Redis Sentinel.

Synchronous replication
No data loss, slower writes
Leader waits for at least one follower to acknowledge before confirming the write. Strong consistency but increased latency and reduced availability if followers are slow or unreachable.
Asynchronous replication
Fast writes, possible staleness
Leader confirms writes immediately, replicates in background. Better performance, but readers can see stale data on followers β€” and recent writes can be lost if leader crashes before replication.

Multi-leader replication

Multiple nodes accept writes simultaneously. Each leader replicates to others; each leader can also have its own followers.

Multi-leader replication across regions

Each leader is a follower of the other leaders, and usually keeps followers of its own. How changes move between leaders is its own trade-off: all-to-all (simple, chatty, fine for a handful of leaders), circular (less bandwidth, higher latency, a break in the ring stalls propagation), or star (one coordinator, which is simpler and is also a bottleneck). Prefer this over leaderless replication when each region should have a real leader, when writes already split cleanly across leaders, or when you want conflicts resolved by a known policy instead of at read time.

When you need it:

The cost: conflict resolution. When the same data is written at two leaders concurrently, you must decide whose change wins.

Last-write-wins (LWW)
Pick the most recent timestamp. Simplest, but clock skew can cause data loss when "wrong" write wins.
Version vectors
Track causality between updates. More accurate; more complex to implement.
Application-specific
Let the app decide based on business semantics (merge two carts, sum two counters).
CRDTs
Conflict-free Replicated Data Types that automatically merge. Math-backed, but limited shape options.

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.

Leaderless replication: any node accepts reads and writes

This is multi-leader taken to the end: every node is a leader, so there is no role to fail over and no node whose log is β€œthe” order. Clients (or a coordinator the client picks at random) have to run the quorum. Conflicts are not settled when the write arrives. They are discovered on read, when the responses disagree, which makes the read path heavier than in a leader system.

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
Read repair
When clients read from multiple replicas, they detect and fix stale values inline.
Anti-entropy
Background process that compares replicas and propagates differences.
Sloppy quorum
When some target nodes are unavailable, write to other nodes temporarily.
Hinted handoff
When original nodes recover, they receive writes that were temporarily held elsewhere.

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.

Data split across shards by key

Sharding does not imply replication. A shard can be one node, or it can be its own single-leader, multi-leader, or leaderless group. The question the placement strategy does not answer is how a request finds its shard. A directory (MongoDB’s mongos, Citus) looks it up and becomes a dependency. A smart client computes the shard from the key and skips that hop, which only works when the function is deterministic and every client knows it. Or any node accepts the call and forwards it (Cassandra’s coordinator), which needs no special router and costs an extra hop whenever the client guessed wrong.

Sharding strategies

Range-based
Order-preserving
Split by key range (e.g., A-F on shard 1, G-M on shard 2). Range queries are efficient. Risk: hot spots if traffic skews to one range.
Hash-based
Even distribution
Hash the key, mod by shard count. Even traffic distribution. Range queries become expensive (must scatter-gather).
Directory-based
Lookup table
A directory service maps keys to shards. Maximum flexibility, easy to rebalance. Cost: directory becomes a bottleneck and SPOF.
Consistent hashing
Minimize remapping
Adding/removing a node only relocates a fraction of keys. Used by Cassandra, DynamoDB, modern caches.

The hot-spot problem

A single popular key (a celebrity user, a viral product) can overwhelm one shard. Mitigations:

Resharding

Adding or removing shards is operationally expensive. Strategies:

Combining Sharding and Replication

Most production systems use both:

Each shard is its own replicated set

You now operate two failure domains: a shard can be wrong or unreachable, and a replica inside it can too. Storage multiplies by the replication factor times the shard count, and so does the runbook. MongoDB’s usual shape is a replica set per shard. Kafka’s is the same idea with different names: a partition is the shard, and each partition has a leader plus followers. The combination is worth it when one machine cannot hold the data or the write rate. It is not worth it for a dataset that still fits, because you pay the complexity before you need the scale.

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:

One shard per tenant
Strong isolation, easy GDPR compliance, simple per-tenant operations. Operational overhead grows with tenant count.
Many tenants per shard
Most efficient. Hash tenants to shards. Need to handle hot tenants (one big tenant overwhelming a shard).
Hybrid
Big enterprise tenants get dedicated shards; smaller tenants share. Common pattern at scale.
Region-per-tenant
For data residency compliance. Each tenant's data stays in their geographic region.

Recap