A distribution model decides where copies of your data live and which machines accept writes. Replication keeps the same data on several nodes, for reads, durability and failover. Sharding puts different data on different nodes, for capacity. Most large databases combine the two. The model you pick decides where the bottleneck is, what happens when a node dies, and whether two writes can conflict.
The four models#
There are four common ways to distribute a database, from simplest to most distributed.
| Model | Who accepts writes | Main benefit | Main risk |
|---|---|---|---|
| Single server | One machine | Simple; no consistency problems | One failure takes everything down |
| Primary/secondary | The primary only | Read scaling and failover | Primary is a write bottleneck |
| Peer-to-peer | Any replica | No single point of failure | Concurrent writes can conflict |
| Sharding + replication | The owner(s) of each shard | Scales reads, writes and storage | Most complex to run |
Single server#
One machine handles every read and write. There is no cluster, so there is no replication lag and no conflict to resolve.
This is the right starting point for most systems and for every development setup. A single modern server handles a lot of traffic. Add backups and a standby before you add a cluster. Move on only when one machine runs out of capacity or you need to survive its failure.
Primary/secondary replication#
When one machine is no longer enough, the usual next step is to copy it. One node is the primary (also called leader). It is the authoritative copy and processes every write. The other nodes are secondaries (followers or replicas). They copy the primary's changes and serve reads.
Strengths:
- Reads scale out: add secondaries to handle more read traffic.
- Every write goes through one node, so writes never conflict with each other.
- If the primary fails, secondaries keep serving reads while a new primary is chosen.
Weaknesses:
- All writes go to one node, so write throughput is capped by one machine.
- The primary is a single point of failure for writes until failover finishes.
- With asynchronous replication, secondaries lag. A read from a secondary can be stale.
Failover can be automatic (the cluster elects a new primary) or manual (an operator promotes one). Automatic failover is faster, but a bad detector can promote a second primary while the first is still alive. This split brain lets two nodes accept writes at once.
Scaling Reads covers this model in depth: synchronous vs asynchronous replication, replication lag, read-after-write fixes and leader failover. PostgreSQL streaming replication, MySQL replication and MongoDB replica sets all follow this model.
Peer-to-peer (leaderless) replication#
Primary/secondary keeps writes simple by sending them all to one node. Peer-to-peer drops that rule: there is no primary. All replicas have equal weight, and any replica can accept a write. Losing one replica does not stop reads or writes, because the others carry on. Amazon's Dynamo paper made this design well known, and Apache Cassandra and Riak follow it.
Without a primary to order writes, the database needs other tools to keep replicas in step:
- Quorums: a write waits for W replicas and a read asks R replicas. With R + W > N, every read overlaps the latest write. See CAP Theorem and Consistency Models.
- Read repair: when a read sees an old value on one replica, it writes the newer value back to that replica.
- Hinted handoff: if a replica is down, another node keeps its writes and hands them over when it returns.
- Anti-entropy: a background process compares replicas (often with hash trees, which let two nodes find the ranges that differ by comparing hashes) and copies missing data.
Strengths: high write availability, no single point of failure, and writes can be served by the nearest replica.
Weaknesses: two clients can write the same key on different replicas at the same time. The database must detect and resolve these conflicts. Reads may also be stale unless you use quorums.
A related design, multi-leader replication, has a few leaders (often one per region) instead of one or all. It shares the conflict problem. See Scaling Writes for when to use it.
Sharding combined with replication#
Both models so far copy the whole dataset to every replica, so total storage is still capped by one machine. Sharding splits the data so each node owns a slice. On its own, sharding has no redundancy: if a shard's node dies, that slice is gone. So production systems replicate every shard.
There are two common layouts:
- Primary/secondary per shard: each shard is a small replica set with its own primary. MongoDB sharded clusters and most sharded PostgreSQL or MySQL setups work this way. Writes scale because each shard has its own primary.
- Peer-to-peer with sharding: data is spread around a ring of nodes, and each key is stored on N nodes. Every node holds replicas of several shards. Column-family stores such as Cassandra use this layout.
The shard key matters as much as the replication model. A poor key sends most traffic to one shard, and replication does not fix that hot spot. See Sharding for how to choose the key.
Conflicts#
Peer-to-peer and multi-leader setups both left one problem open. A write conflict happens when two writes to the same data are accepted by different nodes before either sees the other. Single-server and primary/secondary setups avoid this, because one node orders all writes. Peer-to-peer and multi-leader setups must handle it.
| Approach | How it works | Trade-off |
|---|---|---|
| Last write wins | Keep the write with the latest timestamp | Simple, but silently drops the other write; clock skew can pick the wrong one |
| Version vectors | Track which writes each version has seen, so the database knows if two writes are concurrent | Detects real conflicts, but someone must still resolve them |
| Keep both, merge later | Store both versions and let the app or user merge them | No data lost; more app logic |
| Conditional update | Reject a write if the data changed since it was read | No silent loss; the loser must retry |
Some data merges naturally. A shopping cart can take the union of two versions. A counter can add two increments. Other data, such as an account balance, needs a single order of writes, which pushes you back toward a primary or a conditional update. Keys, Relationships and Integrity covers version stamps and conditional updates.
Choosing a model#
Start simple and add distribution only when you need it.
- One server with backups, until it runs out of capacity or you need high availability.
- Primary/secondary when reads outgrow one machine or you need fast failover.
- Sharding with a replica set per shard when writes or data size outgrow one primary.
- Peer-to-peer when write availability matters more than a single order of writes, and the data can tolerate or merge conflicts. Typical fits are event logs, sensor data and carts.
Key takeaways#
- Replication copies the same data for reads and fault tolerance. Sharding splits data for capacity.
- Primary/secondary: one node orders all writes. Reads scale, but the primary caps writes and must fail over.
- Peer-to-peer: any replica accepts writes. No single point of failure, but concurrent writes conflict.
- Production sharding always replicates each shard, either as replica sets or on a peer-to-peer ring.
- Resolve conflicts with last write wins, version vectors, app-level merges or conditional updates.