# `Raft` vs. Multi `Raft`

![`smart`](RaftAndMultiRaft_aa_002_20220511.png)

------------------------------------------------------------
1. The Leader is the manager in the cluster, all the client traffic passes through the Leader. Normally there is only one Leader, it will periodically send heartbeats to Followers to maintain its rule. When it finds itself not being the Leader, it will turn into a Follower.

2. The Follower is the managed one in the cluster, only responds to commands from other servers. After a long time without response from the Leader, it will turn into a Candidate.

3. The Candidate is the cluster's Candidates will initiate a vote in an attempt to be elected Leader.

In the previous course, we learned that TiKV uses the Raft consensus algorithm to achieve strong consistency of data in distributed environments, TiDB can only write through the Leader, the Leader will replicate the write instruction in the form of log to the Followers, when the majority of the cluster nodes receive this log, we consider this log is committed and can be applied to the data persistence storage engine.

By default, TiDB provides three replicas that form a Raft Group. Because the data handled by a Raft Group is limited, we will split the data into multiple Raft Group, which we call Multi Raft. The way of splitting is based on Range, that is, we will sort the keys of data in byte order, which is an infinite key-value Map data structure, and then split it into sections of continuous key range, each key range is regarded as a Region.

Two adjacent Regions must not have any gaps, meaning the End Key of the previous Region is the Start Key of the next Region.

The range of a Region is left-closed and right-open, for the Start Key, it belongs to this Region, but for the End Key, it actually belongs to the next Region.

The Region of TiKV will have a limit, when it exceeds this threshold, it will split into two Region, for example [a, c) -> [a, b) + [b, c).

If there is no data in the Region, or only a small amount of data, it will be merged with the adjacent Region to form a larger Region, for example [a, b) + [b, c) -> [a, c).

What was said above, multiple Regions forming multiple Raft Groups, we call it Multi Raft.
