A Raft follower acknowledges an entry, then the machine loses power and comes back with a log missing its last few entries because the disk write was only in a cache. Can this break safety? How do you design against it?
Sketch your own answer first, then compare it with the one below.
A strong answer
Yes. Raft's safety argument assumes that a node never forgets what it acknowledged. Take three servers. The leader replicates an entry to S2, S2 acknowledges, and the leader commits it on {S1, S2}. S2 loses power and restarts without the entry. Meanwhile S1 is down. S3 never saw the entry. S2 and S3 now form a majority: S3 asks for a vote, and S2, whose log looks equal to S3's, grants it. S3 becomes leader and the committed entry is gone.
The defence is to treat persistence as part of the protocol. A server must flush its term, its vote and the new log entries to stable storage, and confirm the flush, before replying to the leader or granting a vote. That means an fsync or an equivalent, not just a write into the page cache, and it is a large part of the cost of each commit. Batching entries and grouping many requests under one flush recovers throughput.
Also assume the storage can be wrong. Use disks with power-loss protection, checksum log records so that torn writes are detected, and on a failed check treat the node as new: wipe it and rejoin as an empty member, not as a voter holding a corrupt log.
Sources
- Diego Ongaro, John Ousterhout (2014). In Search of an Understandable Consensus Algorithm.
- Leslie Lamport (2001). Paxos Made Simple.