Sub topic 12
Changing cluster membership safely
This chapter assumes you have read the chapters on elections, log replication, and commitment. You should know that a majority quorum is more than half of the servers in the cluster, and that configuration changes travel through the replicated log like any other entry.
Why a direct switch is unsafe
Sooner or later you replace a failed machine, grow the cluster, or shrink it. The cluster configuration is the list of servers that count toward elections and commit decisions. Servers cannot all learn about a new configuration at the same instant. That gap is where the danger lies.
Suppose a cluster of three servers (A, B, C) grows to five (A, B, C, D, E). Servers switch to the new configuration at different times:
- A and B still use the old configuration. Two votes are a majority of three, so A and B can elect a leader.
- C, D, and E already use the new configuration. Three votes are a majority of five, so they can elect a different leader in the same term.
Two leaders in one term break the central safety guarantee. The old majority and the new majority do not have to share a server.
Two safe approaches
Raft offers two ways to make sure old and new majorities always overlap.
| Approach | How it works | Trade-off |
|---|---|---|
| Joint consensus | Move through a transitional configuration where decisions need a majority of both the old and the new set. | Handles any change, including replacing many servers at once. It is more complex. |
| Single-server changes | Add or remove one server at a time. | Simple to implement. Larger changes take several steps. |
Joint consensus
The leader drives the change in two log entries:
- The leader appends a joint entry, C_old,new, to its log and replicates it.
- Each server uses a configuration as soon as it appends the entry, without waiting for it to commit. While Cold,new is in effect, an election or commit needs a separate majority from Cold and from C_new.
- When Cold,new is committed, the leader appends the Cnew entry.
- When Cnew is committed, the old configuration no longer matters. Servers not in Cnew can shut down.
No server can decide alone during the joint phase, so Cold and Cnew never act independently. The quorum rule looks like this:
def majority(acks, members):
return len(acks & members) * 2 > len(members)
def has_quorum(acks, old_members, new_members=None):
# With one configuration, a plain majority is enough.
if new_members is None:
return majority(acks, old_members)
# In the joint phase, both configurations must agree.
return majority(acks, old_members) and majority(acks, new_members)
old = {"A", "B", "C"}
new = {"A", "B", "C", "D", "E"}
print(has_quorum({"A", "B"}, old)) # True
print(has_quorum({"A", "B"}, old, new)) # False: only 2 of 5 in new
print(has_quorum({"A", "B", "D"}, old, new)) # True: 2 of 3 and 3 of 5Single-server changes
If only one server is added or removed, any majority of the old set and any majority of the new set must share at least one server. Going from three servers to four, for example, changes the majority from two to three, and any two old servers and any three new servers overlap. So no joint phase is needed. The leader appends one configuration entry, and the server takes effect on append, as above. Larger changes are done as a sequence of single steps, and each step must commit before the next starts.
Common errors
- Starting a change too early. A new leader must commit an entry from its own term before it begins a configuration change. Without that, a leftover uncommitted configuration entry from an earlier term can lead to unsafe overlaps.
- Adding a server with an empty log as a voter. It cannot help form a quorum until it catches up, so availability can drop. Add new servers as non-voting members first. Promote them once their logs are close to the leader's.
- Letting removed servers disrupt the cluster. A removed server stops receiving heartbeats, times out, and starts elections with ever higher terms. This can force a healthy leader to step down. A common defense is to ignore RequestVote messages that arrive soon after hearing from a current leader. Pre-vote also helps.
- Forgetting the leader may be removed. A leader not in Cnew keeps replicating until Cnew commits, without counting itself in the majority. Then it steps down.
- Running several changes at once. Allow only one in-flight configuration change at a time.
Exercise
A cluster of three servers (A, B, C) is in the joint phase Cold,new, where Cnew is {A, B, C, D, E}. Candidate D has votes from D and E only. Can it become leader? What if it also gets C's vote?
Show answer
No. D and E are no majority of Cold (they hold 0 of 3 old servers), so the old side has not agreed. With C's vote, the votes are C, D, E. That is 1 of 3 in Cold, which is not a majority, so D still loses. D needs at least two of A, B, C and three of the five servers in C_new.
Exercise
Why is it safe to add one server at a time without a joint phase?
Show answer
Any majority of the old configuration and any majority of the new one must share at least one server, because the two sets differ by only one member. That shared server can vote only once per term, so two leaders cannot be elected in the same term.