03. Data & Storage / Lesson 4 of 11

Replication

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

  • 10 min
  • Intermediate
  • 11 steps
  • Live copies1 (bad)
  • Busiest server100% (bad)
  • Replication lag—
  • Stale reads—
UsersDatabase⚠ only copy
  • Request
  • Write
  • Health check
  • Failed request
Step 1/11

Step 1 of 11: One copy of the data is a risk

Read every step as text
  1. Step 1: One copy of the data is a risk

    Our database gets 9,000 reads and 1,000 writes a second, and one server handles 10,000 operations a second. It is already at 100%, with no room to grow.

    Worse, the data lives on one disk in one machine. If that machine dies, the site is down until it is repaired, and if the disk is lost, the data may be gone for good.

    Replication means keeping the same data on several machines. It buys safety, spare read capacity, and data that sits nearer to users.

  2. Step 2: One leader takes writes; followers copy them

    The usual design has one leader (also called primary). Every write goes to the leader, which saves it and also sends it as a stream of changes, its replication log, to the followers.

    Each follower applies the changes in the same order, so it ends up with the same data. Now the data exists on three machines.

    One writer means no two machines can disagree about the order of writes. That keeps things simple.

  3. Step 3: Followers can answer reads

    Most traffic is reads, so send them to the followers and keep the writes on the leader. Each follower now serves 4,500 reads, and the leader only handles 1,000 writes (10% busy).

    Followers still apply all 1,000 writes, so each is at (4,500 + 1,000) / 10,000 = 55%. The busiest server went from 100% to 55%, and adding a third follower helps further.

    This scales reads, not writes: every write still goes through the one leader and every follower.

  4. Step 4: Does the leader wait for the followers?

    With synchronous replication the leader confirms a write only after the followers have it. A follower in another zone is 20 ms away (round trip), so a write takes about 2 + 20 = 22 ms, and no follower is ever behind. But if a follower is down, writes stall.

    With asynchronous replication the leader confirms after its own write, about 2 ms, and sends the change on afterwards. It is fast and never blocked by a follower, but a follower is behind by a little.

    A common middle way: one synchronous follower (so a second copy always exists) and the rest asynchronous.

  5. Step 5: Asynchronous copies fall behind

    Normally an asynchronous follower is about 100 ms behind. If it is slow, for example while running a long report, the lag grows. Say it reaches 2,000 ms.

    For a row updated once a second, a read from a follower 100 ms behind sees an old value about 10% of the time. At 2,000 ms behind, every read of that row is old (100%).

    This is eventual consistency: if writes stop, the followers catch up. In between, they can disagree with the leader.

  6. Step 6: Show people their own writes

    A user saves a new name and the page reloads. The read lands on a follower that is 100 ms behind, and the old name shows. It looks as if the save failed, which is the worst kind of lag.

    The fix is read-your-writes: for a short time after a user writes (say 10 seconds, longer than the lag) read that user's data from the leader. Everyone else can still read from followers.

    You do not need strong consistency for everyone. Fix the cases where a person would notice.

  7. Step 7: The leader crashes

    Followers are copies, so the data is safe, but nobody can take writes now. Every write fails until a new leader exists.

    Worse, with asynchronous copying the leader may have confirmed writes the followers never received. At 1,000 writes a second and 100 ms of lag, about 100 writes were still on their way.

    Reads from the followers can carry on during this time, with older data.

  8. Step 8: Failover: promote the best follower

    Failover has three parts. Detect: followers expect a heartbeat every second; after 3 missed ones, about 3 s, the leader is declared dead. Choose: pick the follower with the most complete log. Switch: make it the leader and point clients and the other follower at it, about 2 s. Writes were down for about 5 s.

    The old leader's last 100 unreplicated writes are lost, and if it comes back it must become a follower and drop them. Declaring a leader dead too early can also cause split brain (two leaders), which is why real systems use a majority vote (see Consensus).

    Automatic failover trades a few lost writes for a short outage. Synchronous copies avoid the loss but slow every write.

  9. Step 9: Several leaders, one in each region

    With one leader in region A, a user in region B pays the distance on every write: say a round trip of 80 ms, so a write takes about 82 ms. With a leader in each region, a write is accepted nearby in about 2 ms, and the leaders copy to each other in the background (say 150 ms to arrive).

    The catch is conflicts. If both regions change the same record before they hear from each other, the leaders disagree. Systems pick a winner (last write wins, which can lose data), merge the values, or ask the user.

    Multi-leader is hard to get right. Use it for multi-region writes or offline apps, and avoid it otherwise.

  10. Step 10: No leader: write to many, read from many

    In a leaderless design (the style of Dynamo and Cassandra) the client sends each write to all N copies and waits for W to confirm. A read asks R copies and takes the newest value.

    With N = 3, W = 2 and R = 2, any two copies that wrote and any two copies that answer must share at least one node, because W + R = 4 is more than N = 3. So a read sees the latest completed write, and one node can be down without any failover.

    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.

  11. Step 11: Replication in one picture

    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.