Sub topic 12
Changing cluster membership safely
This chapter covers how a running Raft cluster adds or removes servers without risking two leaders. It assumes you know terms, RequestVote, AppendEntries, and the commit rule from earlier chapters. It also assumes you can read short Python.
Why membership is a special case
So far the cluster had a fixed set of servers, and every majority was computed from that set. Real clusters change. Hardware fails, you replace machines, and you grow from three servers to five for more fault tolerance.
The obvious approach is to push a new configuration to every server and let each one switch when it receives it. This is unsafe. Servers receive the new configuration at different moments, so for a short time some servers count majorities against the old set and others against the new set.
Take an old cluster of {S1, S2, S3} growing to {S1, S2, S3, S4, S5}:
- S1 and S2 still use the old configuration. Two of three is a majority, so
{S1, S2}can elect a leader. - S3, S4, and S5 have switched to the new configuration. Three of five is a majority, so
{S3, S4, S5}can elect a leader.
Those two majorities share no server. Both groups can elect a leader in the same term, and each leader can commit different entries. This breaks the central safety guarantee of Raft.
The rule that fixes this is that no point in time may exist at which two disjoint majorities can both make decisions. Raft has two ways to guarantee that.
Configurations live in the log
Raft treats a configuration as a special log entry. The leader appends it, replicates it with AppendEntries, and commits it with the usual rule. Two details differ from ordinary entries and cause many bugs:
- A server uses the latest configuration entry in its log as soon as the entry is appended. It does not wait for the entry to commit. A leader needs the new rules right away to know which majority to wait for when it commits the configuration entry itself.
- If a follower's uncommitted configuration entry is truncated during log repair, the follower must revert to the previous configuration. Your implementation should therefore derive the active configuration from the log by scanning for the last configuration entry. It should not store it in a separate variable that is only ever overwritten.
Because the configuration is in the log, it is also in snapshots. A snapshot must record the configuration that was in effect at its last included index.
Approach 1: joint consensus
The original Raft paper uses a two-phase change through a transitional configuration called C_old,new. In this joint configuration:
- Log entries are replicated to servers in both configurations.
- Any server from either configuration can become leader.
- Agreement for elections and for commitment requires a separate majority of
C_oldand a separate majority ofC_new.
The change proceeds in stages:
Here is why each step is safe:
- Before
C_old,newis committed, any server inC_oldmay still be running withC_oldalone. But nobody can make a decision underC_newalone, becauseC_newis not yet in any committed log. Decisions made underC_oldalone are fine, since noC_new-only majority exists. - After
C_old,newis committed, any leader elected must have that entry in its log (election restriction). Such a leader always requires both majorities. NeitherC_oldnorC_newcan act alone. - After
C_newis appended, the leader needs only a majority ofC_newto commit it. This is safe becauseC_old,newis already committed, so no leader can be elected usingC_oldalone.
The code below shows the two quorum checks. The first reproduces the unsafe direct switch. The second shows that the joint rule rejects both groups.
def has_majority(acks, members):
"""True if acks contains more than half of members."""
return len(acks & members) * 2 > len(members)
def config_quorum(acks, old, new=None):
"""Quorum check under a single config, or under the joint config old,new."""
if new is None:
return has_majority(acks, old)
return has_majority(acks, old) and has_majority(acks, new)
old = {1, 2, 3}
new = {1, 2, 3, 4, 5}
group_a = {1, 2} # servers still on the old config
group_b = {3, 4, 5} # servers already on the new config
print("Unsafe direct switch:")
print(" group A wins under old:", config_quorum(group_a, old))
print(" group B wins under new:", config_quorum(group_b, new))
print("Joint consensus:")
print(" group A wins:", config_quorum(group_a, old, new))
print(" group B wins:", config_quorum(group_b, old, new))Running it prints True for both groups in the unsafe case and False for both in the joint case.
What the leader does about its own removal
If the leader is not in C_new, it keeps leading while it commits C_new. During that window it replicates entries but does not count itself toward the C_new majority. After C_new commits, it steps down, and the remaining servers elect a new leader. Clients see a short availability gap at that point.
Approach 2: one server at a time
Joint consensus is general but complicated. Ongaro's dissertation describes a simpler alternative. It adds or removes only one server per change. Any majority of a configuration with n servers and any majority of a configuration with n ± 1 servers must overlap in at least one server, so two disjoint majorities cannot form. Larger changes are done as a sequence of single-server changes.
Many production implementations use this approach because there is only one configuration entry per step and no transitional state. Its rules are:
- The leader may start a change only when the previous configuration entry is committed. Two changes in flight at the same time would reintroduce the overlap problem.
- The new configuration takes effect on each server when the entry is appended, as described above.
- The leader must have committed at least one entry from its own term before it starts a configuration change. Without this, a new leader may start a change while an earlier configuration entry from a previous term is uncommitted and later overwritten, and the cluster can end up with disjoint majorities. This bug was found after the dissertation was published. A common fix is for a new leader to append a no-op entry on election and wait for it to commit before accepting membership changes.
| Aspect | Joint consensus | Single-server change |
|---|---|---|
| Entries per change | Two (`C_old,new`, then `C_new`) | One |
| Transitional state | Yes, needs two-majority logic | No |
| Arbitrary changes in one step | Yes | No, only ±1 server |
| Implementation difficulty | Higher | Lower |
| Main gotcha | Quorum code must handle both configurations | Must wait for the previous change and for a current-term commit |
Practical problems around membership changes
Catching up new servers
A new server starts with an empty log. If the leader adds it to the configuration immediately, the new server counts toward majorities but cannot help. A five-server cluster that includes two empty servers can stall until they catch up, because the three working servers form a majority only if all three are alive.
The fix is to add the server first as a non-voting member (often called a learner). The leader replicates to it, possibly with a snapshot, but does not count it in any majority. When it is close to the leader's log, the leader proposes the real configuration change.
Removed servers can disrupt the cluster
A server that has been removed does not receive heartbeats from the new leader. Its election timer fires, it increments its term, and it sends RequestVote to the remaining servers. Those servers see a higher term and step down. The leader is deposed even though the sender is no longer a member.
There are two common defenses:
- A server ignores RequestVote messages if it has heard from a current leader within the minimum election timeout. Such a server neither updates its term nor grants a vote.
- Pre-vote, from the election chapter, stops a server from incrementing its term unless it first learns that it could win.
Availability during the change
A change that adds a server to a three-server cluster raises the majority from two to three. If you add servers before they are healthy, the cluster can lose availability that it had before the change. Check that new servers are reachable and caught up before you propose the change.
Edge cases to check in your implementation
- Uncommitted configuration truncated. A follower with an uncommitted
C_newentry from an old leader must roll back to the previous configuration when that entry is deleted. - Leader crash mid-change. A new leader may find
C_old,newcommitted but noC_new. It must finish the change by appendingC_new. - Configuration inside a snapshot. A server restoring from a snapshot installs the configuration stored with it.
- Election with the joint configuration. A candidate must gather separate majorities from both configurations. A vote-counting function that uses one set is a bug.
- Duplicate change requests. A client that retries a change must not cause a second, different change. Treat membership requests like other client commands and make them idempotent.
Exercise
A cluster has servers {A, B, C}. An operator wants to move to {A, B, C, D, E} by telling each server to switch at once. Name a pair of server groups that can each elect a leader in the same term, and say which rule from joint consensus prevents this.
Show answer
Servers A and B can still use the old configuration, where 2 of 3 is a majority. Servers C, D, and E can use the new configuration, where 3 of 5 is a majority. The two groups share no server, so each can elect a leader in the same term. Joint consensus prevents this because in C_old,new a decision needs a majority of the old set and a majority of the new set. Neither {A, B} nor {C, D, E} satisfies both.
Exercise
In a single-server change protocol, why must a new leader commit an entry from its own term before it starts a configuration change?
Show answer
Without a committed entry in its own term, the leader may not know whether an earlier configuration entry is committed, and that entry may later be overwritten. The cluster could then end up with two configurations in use at once whose majorities do not overlap. Committing a no-op in the new term shows which earlier entries are committed, so the leader can safely build the next configuration on top of them.