Out-of-order AppendEntries in Raft
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Raft is a consensus algorithm widely used for managing a replicated log across distributed systems, ensuring all changes are made reliably and in a consistent order across multiple servers. One of the critical challenges Raft addresses is how to handle AppendEntries calls when they are received out of order by the follower. This phenomenon can occur due to network delays, reordering by intermediate routers, or other factors affecting message transmission.
Understanding Out-of-Order AppendEntries
In Raft, each node (server) may act as a leader, follower, or candidate. The leader handles all client interactions and log replication across followers. AppendEntries RPCs (Remote Procedure Calls) are used by the leader to replicate log entries and provide a heartbeat to other nodes.
However, due to network variability, AppendEntries may arrive at followers out of their original sending order. This out-of-order reception can potentially lead to issues with logs synchronization if not adequately handled.
How Raft Handles Out-of-Order AppendEntries
The Raft protocol includes several mechanisms to handle out-of-order AppendEntries effectively:
- Index and Term Matching: Before a follower appends entries received from the leader, it checks if the log entry’s index and term match its current state. Each entry in the log is tagged with a term number, which corresponds to the term during which the entry was received by the leader. If a follower receives an AppendEntries request that does not align with its log, it refuses the new entries.
- Overwriting Inconsistent Entries: If there’s a conflict (i.e., the entries do not match in term), the follower deletes the existing entry and all that follow it, replacing them with the new ones from the leader. This ensures that the logs remain consistent amongst all nodes.
- Leader Completeness Principle: Raft ensures that the leader elected has all committed entries. During the election, Raft uses the voting process where candidates must demonstrate that their logs are at least as up-to-date as those of any other node. This principle prevents a node with an incomplete log from becoming the leader.
Technical Example
Consider a situation where a leader is sending fast-paced updates to its followers, but the network causes some messages to arrive out of order. Here's a step-by-step description of how nodes handle this situation:
- Leader sends AppendEntries with log entries
{index: 4, term: 5}. - Leader sends another AppendEntries with
{index: 5, term: 5}. - The first request gets delayed, and follower processes the second request first, noticing a gap in the log. The follower does not find a matching entry at index 4.
- On receiving the earlier
{index: 4, term: 5}request, the follower can now append this entry at the correct position and subsequently accepts the next entries from the leader.
Summary Table
| Feature | Description | Impact on Cluster Stability |
| Index and Term Matching | Ensures entries are consistent before appending. | Enhances reliability and log consistency. |
| Overwriting Conflicts | Deletes conflicting entries in favor of the leader's version. | Prevents divergence in the log across nodes. |
| Leader Completeness | Ensures the leader has the most complete log. | Guarantees safe leadership transitions and data integrity. |
Additional Considerations
Optimizations: Modern implementations of Raft may include sophisticated mechanisms like pipelining and log compaction to enhance performance and reliability, especially when handling high loads or operating in high-latency networks.
Handling Failure Modes: Much of Raft’s design is predicated on handling partial or total system failures elegantly. Node crashes or network partitions are scenarios where out-of-order AppendEntries handling is crucial to ensure a quick recovery and data consistency.
Client Interaction: Clients typically interact only with the leader. The leader must, therefore, handle out-of-order problems transparently without impacting client operations.
Conclusion
The robustness of Raft in maintaining a consistent state across distributed systems despite out-of-order AppendEntries is a crucial aspect of its design. By ensuring that each node adheres to the protocol’s principles meticulously, Raft guarantees that all participating nodes can recover from errors and remain synchronized. This results in a system that can reliably process client requests even in the face of network delays and disruptions.
Related reading
- Parallelism behaviour of stream processing engines
- Parallel/Redundant Replication in CouchDB
- Partial ordering of events in distributed system in practice
- Partition re-balance on brokers in Kafka 0.8
- Passive Replication in Distributed Systems - Replacing the Primary Server
- Pattern for updating slave SQL Server 2008 databases from a master whilst minimising disruption
- Pause SQL server replication temporarily
- paxos algorithm - how does the propose stage work?

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.