# Raft Flashcards
## Metadata
**Status**:: #x
**Kind**:: #anki/deck
**Zettel**:: #zettel/literature
**Created**:: [[2023-04-25]]
**Topic**:: [[♯ Concurrency]]
### References
- [[Diego Ongaro et al. - Raft In Search of an Understandable Consensus Algorithm (Highlights)]]
## How Raft Elects Leader? #card
Candidates send RequestVote to gather votes. A candidate wins the election when it receives votes from a majority of the servers in the full cluster for the same term.
^1682423901984
## When to Initiate a Leader Election? #card
Leader keeps sending AppendEntries to followers as heartbeat. If a follower receives no communication over a period of time called the *election timeout*, then it assumes there is no valid leader and begins an election to choose a new leader.
^1682423901989
## How to Vote? #card
Servers grant votes by replying RequestVote. The candidate wins a vote when:
1. The receiver has the same or smaller term or smaller term;
2. The receiver has not voted other servers for that term yet;
3. The candidate has a log which is at least as up-to-date as the receiver.
^1682423901993
## How to Check the Candidate's Log Is up-to-Date? #card
If the last log term of A is larger than B's, or if the last log terms are equal but A has at least the same number of entries as B, then Log A is at least as up-to-date as Log B.
^1682423901998
## When Should a Candidate Aborts the Election? #card
A candidate aborts the election when:
1. It receives AppendEntries which has the same or larger term;
2. It does not receive enough votes in a period of time.
^1682423902002
## How a Leader Knows Which Entries to Send? #card
The leader will keep track of the follower's next entry index and send entries from that point. Once the follower receives the entries, it will notify the leader if it lacks the parent entry. Upon receiving the notification, the leader will attempt an earlier entry.
^1682424650036
## When an Entry is Considered Committed? #card
Upon the success response to AppendEntries, the leader save the last sent entry in matchIndex. A log entry is considered committed if a majority of servers have a matchIndex is at least as large as the index of the log entry.
^1682424650041