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.