System Design · 03. Data & Storage · 10 min · Intermediate

Replication

Keep copies of the data on several machines, and handle what goes wrong.

In short

Replication keeps copies on several machines. A leader takes the writes and followers copy them; reads can come from followers, which cut the busiest server from 100% to 55% here.

The price is lag: asynchronous copies are a little behind, so fix the cases people notice with read-your-writes, and plan failover for the day the leader dies.

Key ideas

  • Replication means keeping the same data on several machines. It buys safety, spare read capacity, and data that sits nearer to users. (step 1)
  • Automatic failover trades a few lost writes for a short outage. Synchronous copies avoid the loss but slow every write. (step 8)

Good to know

  • One writer means no two machines can disagree about the order of writes. That keeps things simple. (step 2)
  • A common middle way: one synchronous follower (so a second copy always exists) and the rest asynchronous. (step 4)
  • You do not need strong consistency for everyone. Fix the cases where a person would notice. (step 6)

Watch out

  • This scales reads, not writes: every write still goes through the one leader and every follower. (step 3)
  • This is eventual consistency: if writes stop, the followers catch up. In between, they can disagree with the leader. (step 5)
  • Reads from the followers can carry on during this time, with older data. (step 7)
  • Multi-leader is hard to get right. Use it for multi-region writes or offline apps, and avoid it otherwise. (step 9)
  • Quorums are not magic: concurrent writes, sloppy quorums and clock choices can still return old data. Tune W and R: W = 1 is fast but weaker. (step 10)
UsersLeaderFollower 1Follower 2

Key numbers

Live copies
3
Busiest server
55%
Replication lag
100 ms
Stale reads
10%

Terms

Replication
Keeping the same data on several machines for safety, spare read capacity and nearness to users.
Leader and follower
The leader takes all writes and streams them to followers, which apply the same changes in the same order.
Read replica
A follower that answers read queries to take load off the leader.
Synchronous replication
The leader confirms a write only after the followers have it, so there is no lag but writes are slower.
Asynchronous replication
The leader confirms a write first and copies it afterwards, which is fast but leaves followers a little behind.
Replication lag
How far a follower is behind the leader, which can make reads from it return old data.
Read-your-writes
A guarantee that a user always sees their own recent writes, often by reading them from the leader.
Failover
Detecting a dead leader and promoting a follower to take its place.
Split brain
Two machines both believing they are the leader, which can corrupt data.
Multi-leader replication
Several leaders accept writes, usually one per region, and copy to each other, which makes conflicts possible.
Quorum (W + R > N)
Writing to W and reading from R of N copies so that the sets always overlap and a read sees the latest write.