How can leader replicate logs when a follower recovers with a large term number in RAFT?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In the RAFT consensus algorithm, effective log replication is crucial for ensuring that all nodes in the cluster (called "followers") maintain a consistent state with the leader. The process becomes particularly interesting and technically demanding when a follower, after being down or partitioned, recovers with a term number that is larger than the current leader's term. This scenario requires specific handling to maintain the integrity and correctness of the RAFT protocol. Here we explore how RAFT addresses this issue and ensures seamless state consistency across all nodes.
Understanding RAFT Basics
RAFT is a consensus algorithm that is popular for its simplicity and effectiveness in managing a replicated log across distributed systems. The key components include:
- Term numbers: Monotonically increasing numbers that represent time in a logical sense. Each term begins with an election where one of the nodes may become the leader.
- Leader: The node elected to manage the log replication and commit process during its term.
- Followers: Nodes that replicate logs provided by the leader and grant votes during elections.
- Log entries: Composed of a command to be executed by the replicated state machines and the term number when the entry was received by the leader.
Log Replication Process
The leader replicates its logs to all follower nodes. If a follower is down or partitioned and later recovers, it might have missed some log entries or may have logs that do not align with the current leader's log. Such a follower might even return with a higher term number (due to a network partition causing it to increment its term through failed elections, for example).
Handling Larger Term Numbers on Recovery
When a follower node recovers and contacts the current leader with a higher term number, the following steps occur in RAFT:
- Follower’s Higher Term Update: The follower notifies the leader of its current, higher term.
- Leader Steps Down: On receiving a term number higher than its own, the leader steps down and reverts to a follower state. This occurs because the RAFT protocol dictates that the node with the highest-known term should be given preference for leadership to ensure no old leader can make conflicting decisions.
- New Election Triggered: A new election is triggered because the cluster recognizes the presence of a higher term number.
- State Synchronization: If the recovered follower does not win the election or if another leader is chosen, the new leader will then initiate log replication to synchronize the state of the recovered follower. This includes deleting inconsistent entries in the follower’s log and replicating missing entries from the leader’s log.
Log Matching and Consistency Check
Before any new log entries are accepted by the follower, the leader ensures that the logs match. This is done by:
- Index and Term Match Checking: The leader checks if the last log index and term of the follower match its own. If not, it finds the last point of agreement to start sending missing logs from that point.
- Log Append Mechanism: After finding the point of alignment, the leader sends all subsequent logs from that point to the follower.
Recovery Scenarios and Their Impact
Consider a scenario where a follower node recovers with a significantly higher term number:
- The cluster must ensure that the most up-to-date node (in terms of log entries) eventually assumes leadership, even if temporary leadership is handed to the recovered node due to its higher term.
- Comprehensive log matching and synchronization are critical to avoid conflicts and ensure the state machine’s commands are executed in the correct order.
Summary Table
| Term | Description |
| Term Number | Used for identifying the current logical "time" or operational period. |
| Follower | Replicates data and follows commands from the leader. |
| Leader | Manages replication and ensures state consistency across all followers. |
| Log Entries | Contains commands for state machines; linked with the term received. |
| Recovery Stage | Post-recovery actions (higher term handling, re-elections). |
Conclusion
In RAFT, handling a follower's recovery, especially with a larger term number, entails steps to ensure robust leadership transitions and log consistency. RAFT's disciplined approach to term handling, log replication, and election facilitates resilience and robustness in distributed systems, providing a fault-tolerant environment crucial for many applications.
Related reading
- How can MySQL Cluster 7.3 achieve 99,999% Availability? Antithesis to CAP Theorem
- How can we create service dependencies using kubernetes
- How customer offsets are maintained in mirrored cluster in Kafka?
- How do I add files to distributed cache in an oozie job
- How do I create a realtime copy of my SQL Server 2005 database?
- How do I effectively design my application where most classes depend on ILogger?
- How do I force a favicon refresh?
- How do I get logs from all pods of a Kubernetes replication controller?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.