What is lastApplied and matchIndex in raft protocol for volatile state in server?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the Raft consensus algorithm, which is central to the operation of distributed systems ensuring consistency across replicated logs, two crucial but often misunderstood volatile states on each server are lastApplied and matchIndex. These component parts play pivotal roles in managing and synchronizing the data among various servers in a cluster.
Understanding lastApplied and matchIndex
lastApplied
lastApplied is a volatile state on all servers involved in the Raft consensus algorithm, including followers, candidates, and the leader. It keeps track of the highest log entry index that has been safely applied to the state machine up to any given point in time. It starts at 0 (or -1, depending on the initial condition settings) and is incremented as entries are applied. This marker is crucial for ensuring that the state machine reflects only those changes that have been agreed upon by a majority of the cluster.
Simply put, lastApplied helps ensure that no entries that might still be contested or uncommitted influence the state of the server.
Example
Consider a sequence of logs being replicated across a cluster:
- The leader sends a log with index 1 to all followers.
- Once the followers confirm receipt, the leader marks these as committed.
- As each server (including the leader) applies this log to its respective state machine, they update
lastAppliedto 1.
matchIndex
matchIndex, on the other hand, plays a different yet complementary role. It is solely maintained on the leader and represents the highest log index known to be replicated on each server. This index is utilized to manage the replication process and also optimizes the consistency and coherence of logs across different servers. In essence, matchIndex aids in helping the leader understand up to what point each follower's log matches its own.
This indexing system is crucial during scenarios like log compaction, recovery from failures, and ensuring minimal data divergence during leadership changes.
Example
Following the earlier scenario:
- Assume a leader replicates log indexes 1, 2, and 3.
- If a follower confirms replication up to index 2, the leader will set that follower’s
matchIndexto 2. - If another follower successfully replicates all three logs, their
matchIndexwould then be set to 3.
The interaction between lastApplied and matchIndex
Understanding the interaction between these two indexes aids in diagnosing and resolving potential issues around data consistency and availability. For example, a large gap between lastApplied and matchIndex indicates delays in applying logs to the state machine, which could impair the performance of read operations needing up-to-date data.
Summary Table
| Term | Scope | Description |
| lastApplied | All servers | Index of the highest log entry actually applied to the state machine. Starts at 0 and increments as logs are applied. |
| matchIndex | Leader only | Highest log index known to be replicated on each server, used for managing log replication and consistency. |
Additional Subtopics
- Importance in Leader Election: These indices can also provide a metric for choosing a new leader, with a higher
lastAppliedpotentially indicating a better candidate. - Impact on System Performance: Delays in updating
lastAppliedin relation to receiving and committing new entries can lead to stale state data, affecting query responses and system trustworthiness.
Conclusion
lastApplied and matchIndex are integral to the functionality of Raft's distributed consensus approach, particularly in terms of ensuring data integrity and consistency across servers. Their correct implementation and management are crucial for the robustness and reliability of distributed systems operating under the Raft protocol.

