System design lab · Episode 6

How replication keeps data safe when a server dies

One server can break at any time. So important data is kept on several servers, called nodes. One node leads: it takes every write and copies it to the others. A write is safe once most of the nodes have it. Here three nodes keep their logs on trays. Follow the lessons under the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
123
The flag: the node that leadsA node's log: full tiles are committed, pale ones are not yetThe lane: writes, copies and answers travel on it

Controls

NodesPress to stop or start
Time

Want the details?

What is replication?

A server can break: its disk dies, its power goes, or it is restarted for an update. If your data lived only on that server, it would be gone or unreachable. So important data is kept on several servers, called nodes. Keeping copies on several nodes is called replication.

The hard part is keeping the copies the same. A common answer: one node is the leader. Every write goes to the leader, and the leader copies it to the followers, in the same order.

How to read the bench
Part on the benchWhat it means in a real system
Box at the leftThe client, for example your app. Its screen shows the write it waits for.
Nodes 1, 2, 3Three servers that each keep a copy of the data.
Flag on topThe leader.
Tiles on a trayThe node's log: the writes it has, in order. Full tiles are committed; pale ones are not yet.
Things on the laneMessages: a numbered tile is a write or a batch of copies; a light ball is an answer; a dark ball is a request for votes.
Lamps on a nodeThe first lamp shows the node runs. The row of four fills while it hears nothing from a leader.
Why a majority?

The leader does not wait for every node, because one slow or broken node would stop everything. It waits for a majority: 2 of 3, or 3 of 5.

The trick: any two majorities share at least one node. So when a new leader is chosen by a majority, at least one of its voters has every committed write. A node only votes for a candidate whose log is at least as long as its own. That is why a new leader never lacks a committed write.

Choosing a new leader
  • The leader sends a short heartbeat twice a second: “I am still here.”
  • A follower that hears nothing for a while starts an election and asks the others for their vote.
  • Each node has a slightly different waiting time, so usually only one asks at a time.
  • With a majority of votes it becomes the leader for a new term.
The price
  • Every write waits for a round trip to another node, so writes are slower than on one server.
  • Without a majority, writes stop. With 1 of 3 nodes running, the system refuses to commit rather than risk two different histories. This is the choice between consistency and availability that the CAP theorem talks about.
  • That is why clusters have an odd number of nodes: 3 nodes survive 1 failure, 5 survive 2.
In real systems
  • Raft is the method shown here, in a simple form. etcd (the memory of Kubernetes), Consul, CockroachDB and TiKV use it. Paxos is an older method with the same idea.
  • Database replicas. PostgreSQL and MySQL can copy every write from a primary to replicas. With synchronous replication, a write waits for a replica, like here; with asynchronous replication it does not wait, which is faster but can lose the newest writes when the primary dies.
  • Reading. Reads from a follower can be a little behind the leader. Systems that need fresh data read from the leader.
Words to know
Node
One server in a group of servers.
Replication
Keeping copies of the same data on several nodes.
Leader and follower
The node that takes the writes, and the nodes that copy them.
Log
The list of writes, in order.
Majority (quorum)
More than half of the nodes.
Committed
Stored on a majority, so it will not be lost.
Consensus
Nodes agreeing on one history, even when some of them fail.
Term
The period of one leader. A new election starts a new term.