AI Hints
Home›System Design Tutorial›Correct and Copied
System Design · Lesson 41 of 120

Replication

Copies of your data.

⏱ 2 min read▶ Video at 48:11🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

If Shoply's only database server dies, every order is at risk. Replication keeps copies of the data on several machines, so one failure doesn't lose anything.

Replication
From the video: Replication
  • One database, one risk
  • Keep copies
  • Survive a failure

Leader and followers

The most common setup is leader and followers. All writes go to the leader. The leader records each change in a log, and sends the log to its followers. Each follower replays the changes, so it holds the same data. If the leader dies, a follower can be promoted to take its place.

  1. Writes
  2. Leader
  3. Change log
  4. Followers
  5. Promote if needed
Leader and followers
From the video: Leader and followers

Sync vs async copies

Copies can be made in two ways. With synchronous replication, the leader waits until a follower confirms, before telling the user it's saved. That's safe, but slower. With asynchronous replication, the leader answers straight away, and followers catch up a moment later. It's fast, but a crash can lose the last few changes.

Synchronous

  • Wait for the followerNo data lostSlower writes

Asynchronous

  • Answer straight awayFastCan lose recent writes

Summary

Replication copies data to followers, for safety and for scale. Synchronous is safe, asynchronous is fast. Next, using those copies to handle more reads.

Key takeaways
  • Leader takes writes
  • Followers copy the log
  • Sync vs async
Next up: Read Replicas and Lag — scaling reads