Raft (computer science)

From Wikipedia, the free encyclopedia
  (Redirected from Raft consensus algorithm)
Jump to: navigation, search

Raft is a consensus algorithm designed as an alternative to Paxos. It was meant to be more understandable than Paxos by means of separation of logic, but it is also formally proven safe and offers some additional features.[1] Raft offers a generic way to distribute a state machine across a cluster of computing systems, ensuring that each node in the cluster agrees upon the same series of state transitions. It has a number of open-source reference implementations, with full-spec implementations in Go, C++, Java, and Scala.[2]

Basics[edit]

Raft achieves consensus via an elected leader. A server in a raft cluster is either a leader or a follower, and can be a candidate in the precise case of an election (leader unavailable) . The leader is responsible for log replication to the followers. It regularly informs the followers of its existence by sending a heartbeat message. Each follower has a timeout (typically between 150 and 300 ms) in which it expects the heartbeat from the leader. The timeout is reset on receiving the heartbeat. If no heartbeat is received the follower changes its status to candidate and starts a leader election.[1][3]

Approach of the consensus problem in Raft[edit]

Raft implements consensus by a leader approach. The cluster has one and only one elected leader which is fully responsible for managing log replication on the other servers of the cluster. It means that the leader can decide of new entries placement and establishment of data flow between it and the other servers without consulting other servers. A leader leads until it fails or disconnects, in which case a new leader is elected.

The consensus problem is decomposed in Raft into three relatively independent subproblems listed down below.

Leader Election[edit]

When the existing leader fails or when you start your algorithm, a new leader needs to be elected.

In this case, a new term starts in the cluster. A term is an arbitrary period of time on the server on which no new leader needs to be elected. Each term starts with a leader election. If the election is completed successfully (i. e. a single leader is elected) the term keeps going with normal operations orchestrated by the new leader. If the election is a failure, a new term starts, with a new election.

A leader election is started by a candidate server. A server becomes a candidate if it receives no communication by the leader over a period called the election timeout, so it assumes there is no acting leader anymore. It starts the election by increasing the term counter, vote for himself as new leader, and send a message to all other servers requesting their vote. A server will vote only once per term, on a first-come-first-served basis. If a candidate receives a message from another server with a term number at least as large as the candidate's current term, then its election is defeated and the candidate changes into a follower and recognize the leader as legitimate. If a candidate receives a majority of votes, then it becomes the new leader. If neither happens, e.g., because of a split vote, then a new term starts, and a new election begins.[1]

Raft uses randomized election timeout to ensure that split votes problem are resolved quickly. This should reduce the chance of a split vote because servers won't become candidates at the same time : a single server will timeout, win the election, then become leader and sends heartbeat messages to other servers before any of the followers can become candidates.[1]

Log Replication[edit]

The leader is responsible for the log replication. It accepts client requests. Each client request consist in a command to be executed by the replicated state machines in the cluster. After being appended to the leader's log as a new entry, each of the requests is forwarded to the followers in AppendEntries messages. In case of unavailability of the followers, the leader retries AppendEntries messages indefinitely, until the log entry is enventually stored by all of the followers.

Once the leader receives confirmation from the majority of its followers that the entry as been replicated, the leader apply the entry to its local state machine, and the request is considered committed.[1][3] This event also commits all previous entries in the leader's log. Once a follower learns that a log entry is committed, it applies the entry to its local state machine. It provides consistency for the logs between all the servers through the cluster, insuring that the safety rule of Log Matching is respected.

In the case of leader crash, the logs can be left inconsistent, with some logs from the old leader not being fully replicated through the cluster. The new leader will then handle inconsistency by forcing the followers to duplicate its own log. To do so, for each of its followers, the leader will compare its log with the log from the follower, find the last entry where they agree, then delete all the entries coming after this critical entry in the follower log and replace it with its own log entries. This mechanism will restore log consistency in a cluster subject to failures.

Safety[edit]

Raft guarantees each of these safety properties :

  • Election safety: at most one leader can be elected in a given term.
  • Leader Append-Only: a leader can only appends new entries to its logs (it can't overwrite neither delete entries).
  • Log Matching: if two logs contain an entry with the same index and term, then the logs are identical in all entries up through the given index.
  • Leader Completeness: if a log entry is committed in a given term then it will be present in the logs of the leaders since this term
  • State Machine Safety: if a server has applied a particular log entry to its state machine, then no other server may apply a different command for the same log.

References[edit]

  1. ^ a b c d e Ongaro, Diego; Ousterhout, John (2013). "In Search of an Understandable Consensus Algorithm" (PDF). 
  2. ^ "Raft Consensus Algorithm". 2014. 
  3. ^ a b "Raft: Understandable Distributed Consensus". Retrieved 2015-03-14. 

External links[edit]