Raft Commit Index

medium · distributed-systems, consensus, raft

Raft Commit Index

A Raft leader can advance commitIndex to the largest index N such that:

  • A majority of matchIndex values are >= N
  • logTerms[N-1] == currentTerm

Given the match indices for all nodes (including the leader), the log terms, and the current term, compute the commit index.

If no index in the current term satisfies the majority rule, return 0.

Function signature

func CommitIndex(matchIndex []int, logTerms []int, currentTerm int) int

Example

matchIndex = [5,5,4,3,2]
logTerms   = [1,1,2,2,3]
currentTerm = 2
output = 4

Constraints

  • len(matchIndex) >= 1
  • len(logTerms) >= 0
  • matchIndex[i] may be 0
Run tests to see results
No issues detected