# Raft Vs Multi Raft
__*Raft*__
__*leader*__
__*follower*__
__*candidate*__
__*Raft Group*__
__*Multi Raft*__

![left](./student-guide/diagram/RaftAndMultiRaft_aa_001_20220511.png)

------------------------------------------------------------
First, let's synchronize a few terms in our Raft consensus algorithm:

1. Leader: As the manager in the cluster, all client traffic passes through the leader. Normally, there is only one leader, and it will periodically send heartbeats to the follower to maintain control. When I find out that I'm not a leader, I turn to a follower.

2. Follower: The manager in the cluster will only respond to commands from other servers. After not receiving a response from the leader for a long time, it becomes a candidate.

3. Candidate: The candidate of the cluster will initiate a vote to be elected as the leader.

In the previous course, we learned that TiKV uses the Raft consensus algorithm to achieve strong consistency of data in a distributed environment. The TiDB database can only be written through the leader, and the leader will copy the write instructions to followers in the form of a log. When most nodes of the cluster receive this log, we This log is considered committed and can be applied to the data persistence storage engine (state machine).

TiDB provides support for three replica by default, and these three replica form a Raft Group. Since one Raft Group processes a limited amount of data, we will split the data into multiple Raft Groups, which we call Multi Raft. The way to split is to split according to range, that is, we will sort the keys of the data in byte order, that is, a map data structure with an infinite key - value, and then split it into a (continuous) key range, each key range as a Region.

No gap is allowed between two adjacent Regions, that is, the end key of the previous Region is the start key of the latter Region. The range of Region uses the open mode [start, end). For key start, it belongs to this Region, but for end, it actually belongs to the next Region.

The TiKV Region will have a maximum size limit. When this threshold is exceeded, it will be split into two Regions, such as [a, c) -> [a, b) + [b, c). Of course, if there is no data in the Region, or there is little data, it will also be merged with the neighboring Region and become A larger Region, such as [a, b) + [b, c) -> [a, c).

As mentioned above, multiple Raft Groups formed by multiple Regions are called Multi Raft.

Next, we'll highlight a few important features of the Raft consensus algorithm for TiKV:
1. Log copy
2. Leader elect