## Leader election
Imagine you and your friends are playing a game where you need to choose a leader. You all have equal votes, and the person with the most votes wins. But there's a problem - sometimes some of your friends aren't able to play, so they're not around to cast their votes.
To solve this problem, you and your friends decide to use the Raft consensus algorithm. The algorithm works like this:
1. You and your friends elect a leader, who will be in charge of keeping track of the votes and making sure everyone is up-to-date.
2. The leader sends out a message to all of the other players, asking them to vote for the new leader.
3. If enough players (more than half) vote for the new leader, they become the new leader. If not, the process repeats until a new leader is elected.
4. Whenever anyone has a new vote, they send it to the leader, who keeps track of all the votes.
5. If the leader ever goes offline, the other players will realize this and hold a new election to choose a new leader.
## Log replication
1. The client sends a command to the leader: When a client wants to execute a command (such as adding or removing a user, or updating a piece of data), it sends the command to the leader.
2. The leader appends the command to its own log: The leader adds the command to its own log, and marks the entry as "uncommitted" because it hasn't yet been replicated to a majority of servers.
3. The leader sends the command to all followers: The leader sends the command to all followers in a heartbeat message, which is a periodic message that the leader sends to all followers to let them know it's still alive. The heartbeat message contains the latest commit index (the index of the last log entry that has been committed by the leader), as well as any uncommitted log entries that the followers don't yet have.
4. The followers append the command to their own logs: When the followers receive the heartbeat message, they check to see if the leader's log is more up-to-date than their own. If it is, they append the new log entries to their own logs, and send an acknowledgement message back to the leader to confirm that they have received and appended the new log entries. If the followers already have the same or a more up-to-date log than the leader, they ignore the heartbeat message.
5. The leader waits for acknowledgements from a majority of servers: The leader waits for acknowledgement messages from a majority of servers (including itself) to confirm that the new log entries have been replicated. Once it has received acknowledgement messages from a majority of servers, it considers the new log entries to be "committed".
6. The leader sends a commit message to all servers: Once the new log entries are committed, the leader sends a commit message to all servers in a heartbeat message, telling them which log entries have been committed. The followers then apply the new log entries to their state machine, which is the component of the server that actually performs the commands.
7. The client receives a response: Once the new log entries have been committed and applied to the state machine on a majority of servers, the leader sends a response to the client, letting it know that the command has been successfully executed.
## In more detail
See a very detailed explanation
<iframe width="560" height="315" src="https://www.youtube.com/embed/IPnesACYRck?start=985" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/IPnesACYRck?start=985" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>