Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is lastApplied and matchIndex in raft protocol for volatile state in server?

I am using the following pdf as reference.enter image description here

It says that lastApplied is the highest log entry applied to state machine, but how is that any different than the commitIndex?

Also is the matchIndex on leader just the commitIndex on followers? If not what is the difference?

like image 868
Jal Avatar asked Oct 17 '25 09:10

Jal


1 Answers

Your observation is reasonable: most of the time, nextIndex equals matchIndex + 1, but it is not always the case.

For example, when a leader is initiated, matchIndex is initiated to the 0, while nextIndex is initiated to the last log index + 1.

The difference here is because these two fields are used for different purposes: matchIndex is an accurate value indicating the index up to which all the log entries in leader and follower match. However, nextIndex is only an optimistic "guess" indicating which index the leader should try for the next AppendEntries operation, it can be a good guess (i.e. it equals matchIndex + 1) in which case the AppendEntries operation will succeed, but it can also be a bad guess (e.g. in the case when a leader was just initiated) in which case the AppendEntries will fail so that the leader will decrement nextIndex and retry.

As for lastApplied, it's simply another accurate value indicating the index up to which all the log entries in a follower have been applied to the underlying state machine. It's similar to matchIndex in that they both are both accurate values instead of heuristic "guess", but they really mean different things and serve for different purposes.

like image 195
Lifu Huang Avatar answered Oct 20 '25 00:10

Lifu Huang