# Replication

> Keep copies of data on several machines for availability and read scaling, and understand leader-follower, multi-leader and leaderless replication and their...

Canonical: https://www.internhack.in/roadmaps/tracks/system-design/replication
Last updated: 2026-11-28

Keep copies of data on several machines for availability and read scaling, and understand leader-follower, multi-leader and leaderless replication and their lag problems.

Intermediate · about 4 hours · difficulty 3/5

Learn first: [Relational Databases and SQL for Design](https://www.internhack.in/roadmaps/tracks/system-design/relational-databases)

**Replication** keeps the same data on multiple nodes. It gives you fault tolerance (a node can die without data loss), read scaling (spread reads across replicas) and lower latency (replicas near users).

### Leader-follower (primary-replica)

All writes go to one **leader**, which streams changes to **followers**. Reads can go to followers. This is how most Postgres and MySQL setups work.

- **Synchronous** replication waits for a follower to confirm before acknowledging: no data loss on failover, higher write latency.
- **Asynchronous** replication acknowledges immediately: fast, but recent writes can be lost if the leader dies.
- Many systems use **semi-synchronous**: one sync follower, the rest async.

### Replication lag problems

- **Read-your-writes**: a user updates their profile, refreshes, and sees the old value from a lagging replica. Fix by reading from the leader for a short window after a write, or for that user's own data.
- **Monotonic reads**: a user sees new data, then old data on the next request. Pin a user to one replica.
- **Consistent prefix**: a reply appears before the question. Keep causally related writes in one partition.

### Failover

When the leader dies, a follower is promoted. Dangers: losing async writes, and **split brain** where two nodes both think they are leader. Use a consensus-based coordinator or managed service for safe promotion.

### Multi-leader and leaderless

- **Multi-leader**: each region accepts writes and syncs with others. Good for multi-region apps, but you must resolve **write conflicts** (last write wins, merge logic, CRDTs).
- **Leaderless** (Dynamo style, Cassandra): clients write to several replicas and read from several. With N replicas, write quorum W and read quorum R, **W + R > N** gives overlapping reads and writes.

### Pitfalls

- Sending reads to replicas without handling read-your-writes.
- Treating replication as a backup. A bad DELETE replicates instantly; you still need backups.

You are ready to move on when you can explain sync vs async replication, describe one replication lag bug and its fix, and compute whether N=3, W=2, R=2 gives overlapping quorums.

## Key concepts

- **Leader-follower replication**
- **Sync vs async replication**
- **Replication lag anomalies**
- **Failover and split brain**
- **Multi-leader replication**
- **Leaderless and quorums**
- **Conflict resolution and CRDTs**

## Mini project

Set up a PostgreSQL primary with one streaming replica in Docker. Write to the primary and read from the replica in a loop to observe lag, then stop the primary and promote the replica, documenting what was lost.

## Check yourself

A user changes their delivery address and the checkout page still shows the old one. What replication issue is this and how would you fix it?

## Free resources

- [PostgreSQL high availability and replication](https://www.postgresql.org/docs/current/high-availability.html) (PostgreSQL)
- [Amazon's Dynamo](https://www.allthingsdistributed.com/2007/10/amazons_dynamo.html) (All Things Distributed)
- [Designing Data-Intensive Applications](https://dataintensive.net/) (Martin Kleppmann)

Previous: [Indexes and Storage Engines](https://www.internhack.in/roadmaps/tracks/system-design/indexing) · Next: [Sharding and Partitioning](https://www.internhack.in/roadmaps/tracks/system-design/sharding-partitioning)

## More in Data Storage and Databases

- [Relational Databases and SQL for Design](https://www.internhack.in/roadmaps/tracks/system-design/relational-databases)
- [NoSQL Data Models and When to Use Them](https://www.internhack.in/roadmaps/tracks/system-design/nosql-data-models)
- [Indexes and Storage Engines](https://www.internhack.in/roadmaps/tracks/system-design/indexing)
- [Replication](https://www.internhack.in/roadmaps/tracks/system-design/replication)
- [Sharding and Partitioning](https://www.internhack.in/roadmaps/tracks/system-design/sharding-partitioning)
- [Consistency Models, CAP and PACELC](https://www.internhack.in/roadmaps/tracks/system-design/consistency-cap-pacelc)
- [Object Storage and Large Files](https://www.internhack.in/roadmaps/tracks/system-design/object-storage)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [System Design Roadmap](https://www.internhack.in/roadmaps/tracks/system-design)
