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 bench | What it means in a real system |
|---|---|
| Box at the left | The client, for example your app. Its screen shows the write it waits for. |
| Nodes 1, 2, 3 | Three servers that each keep a copy of the data. |
| Flag on top | The leader. |
| Tiles on a tray | The node's log: the writes it has, in order. Full tiles are committed; pale ones are not yet. |
| Things on the lane | Messages: 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 node | The 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.