Mathematical induction is a method of proof, when the first step is to prove a statement for a simple case. In the second step, we accept the statement true for some case X. Based on this, we try to prove the statement for some neighboring case X + 1. Together, these two steps help to prove the statement for all cases.
In our situation, a simple case is empty logs. There are no records, therefore there is nothing to violate the property.
Now let's try to assume that there are some entries in the logs that correspond to our property. Raft has a mechanism that prevents the property from breaking when any log changes. This mechanism is called
Consistency check . Let's look at the examples right away.
Good example . There is a leader, for example, of the 4th term, there is a follower. They both have matching logs from three entries.
A request from the client comes to the leader, he adds an entry to his log.
The leader sends AppendEntries to the follower. But, in addition to the most added record, the leader also indicates in the request that the record must be added at index 4, and at index 3, before it, there must be a record from term 2.
The log entry at index 3 in the follower log matches the one specified in the request, so the follower adds the record to his log and responds to the leader with success. The end.
Also a good example, but with a tragic beginning. Now the follower's log is different from the log of the current leader.
When the leader receives a request to add an entry to the log, he will send the same AppendEntries as in the previous example.
However, this time, since the follower does not match the previous record, the follower fails.
What does the leader do in this case? The leader simply rolls back a little and tries to feed the follower the record that he himself considers standing at index 3. He also includes the previous record in the request.
Now the follower responds with success and overwrites the entries in his log, starting from index 3.
The follower’s log may differ from the leader’s log as you wish. There may not be enough entries in it, there may be extra entries in it. In any case, the consistency check ensures that the logs of followers will sooner or later coincide with the log of the leader.