When reading a raft paper, consider two questions:
- Why use replicated state machine? No other way?
- Why do I have to write a log before applying it to replicated state machine and apply it directly to the replicated state machine brush disk?
1 Replicated State machine
In the raft protocol, there is a replicated state machine in each server, and as long as each server receives the same input command, the final result of replicated state machine is the same, The final consensus was reached. Then we can consider if there are other ways of not using replicated state machine. In fact, there are two ways to replicate:
To make the process highly available, we can back up the process, and there are two main ways to implement a master-slave backup of the process
- State Transfer: The primary server transmits the full state content to the backup server
- Replicated state machine: The server that needs to be backed up is treated as a deterministic state machine-the master is started in the same state, the same input is imported in the same order, and eventually they enter the same state, giving the same output
The second kind is what we just said about replicated state machine. Assuming that the first, we need to replicate the entire data in the server to each other server, the performance cost is obviously not feasible, so in the case of multi-copy replication, replicated state machine is used, The only need to apply incremental commands to the state machine is to achieve the final consensus.
2 WAL
Wal,write-ahead Logging. The central idea of Wal is that changes to the data file must occur after the changes have been recorded to the log, that is, the log is written down and then the data is written. If you follow this procedure, you do not need to brush the data block back to disk every time you commit a transaction, because you can use the log to recover the data in case of a crash.
So what's good about that? Why not just drop the plate?
- the main benefit of using Wal is to significantly reduce the number of write disks, because the log file is submitted with only the log files flushed to disk, not all the data files modified by the transaction , and the log files are sequential, so the cost of the synchronization log is much smaller than the cost of synchronizing the data blocks. Here we answer the question just now, the key point is performance.
- Using logs also supports operations such as rollback
Therefore, in raft, leader logs the log to follower, which means that the operation has been persisted and replicated to most of the time, indicating that the message is not lost in the entire system (personal understanding)
3 references
- Https://www.postgresql.org/docs/9.2/static/wal-intro.html
- https://zhuanlan.zhihu.com/p/33816607
- Http://book.51cto.com/art/201201/313209.htm
Replicated state Machine and Wal