How to handle reordered RPC 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 designed to manage a replicated log across multiple servers. It ensures all participating servers agree on the order and content of the log entries in a reliable and fault-tolerant manner. One critical challenge in implementing the Raft algorithm is handling reordered RPCs (Remote Procedure Calls). Reordering can occur due to network delays, retries, or concurrent transmissions, which might cause the receiving server to process RPCs in a different order from which they were sent.
Understanding RPCs in Raft
RPCs in Raft are primarily of three types:
- AppendEntries: Used to replicate log entries and to provide a heartbeat.
- RequestVote: Requested by candidates during an election to gather votes.
- InstallSnapshot: Sent to ship a snapshot of data to other servers if a follower is significantly behind.
Importance of Handling Reordered RPCs
Proper handling of reordered RPCs is crucial because:
- It ensures consistency despite network anomalies.
- It preserves the invariant that the log on each server is a coherent and accurate replica of the leader’s log.
- It prevents unnecessary re-elections triggered by stale term numbers.
Technical Approach to Deal with Reordered RPCs
Term Validation
Each RPC includes the term number, which reflects when the message was sent. The term number enables the receiving server to assess the timeliness of the message. Below are steps that are typically executed when an RPC is received:
- Term Comparison: The server compares the term in the RPC with its current term.
- Discard Stale RPCs: If the RPC term is older, the RPC is considered stale and is discarded.
- Update Current Term: If the RPC term is newer, the server updates its current term and switches to a follower state if it was a candidate or leader.
Log Consistency Checks
For AppendEntries RPCs, additional checks are conducted to ensure log consistency:
- PrevLogIndex and PrevLogTerm: Before appending new entries from the leader, the follower checks if the log entry at
PrevLogIndexhas the termPrevLogTerm. If not, it rejects theAppendEntriesRPC and requests the leader to decrement thePrevLogIndexand resend the RPC. This mechanism ensures that the logs are consistent and match up to where entries start to diverge.
Commit-Index Handling
Even if RPCs arrive out of order, Raft relies on the commitIndex, which is a stable index agreed upon by the majority, to preserve log correctness. Followers update their own commitIndex based on the value received in AppendEntries from the leader and apply entries up to this index to their state machines.
Snapshot Consideration
In cases where a follower is far behind, handling of InstallSnapshot RPC should be made cautiously:
- If an
InstallSnapshotRPC arrives out of order (after newerAppendEntries), the follower should detect overlap and possibly disregard it if already covered by the existing entries.
Example Scenario
Here's a typical example considering the RPC handling:
Summary Table
| Component | Description | Importance |
| Term Number | Identifies the recency of the message. | Critical for message relevance. |
| Log Consistency | Ensures that all appended entries are consistent across servers. | Key to maintaining state machine integrity. |
| Commit Index | Stable index agreed upon by majority; used to apply entries. | Central to consistency and leader-followers sync. |
| Snapshot Handling | Deals with large state transfer when followers lag significantly. | Important for efficiency and reducing bandwidth. |
Conclusion
Handling reordered RPCs in Raft involves a combination of term checking, log entry consistency confirmation and efficient handling of snapshots. By carefully managing these aspects, Raft ensures data integrity and robustness of the consensus even in the face of network-induced challenges.
Related reading
- How to handle unique indexes with MySQL master master replication
- How to have multiple cache manager configuration in spring cache java
- how to implement a distributed system for a monitoring platform
- How to implement a distributed system using multiple ports with Java CORBA?
- How to handle timeout in Async Socket?
- How to identify if the OAuth token has expired?
- How to implement a Least Frequently Used LFU cache?
- How to implement a microservice Event Driven architecture with Spring Cloud Stream Kafka and Database per service

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.