Search code examples
javaconsistencyraft

How to safely remove history log in raft when all nodes log entries have been committed


Recently, I'm using the RAFT to build a distributed system, the realization of a simple function is to replicate log entry to each server to keep the data consistency, so my question is how to safely remove history log in RAFT when all nodes log entries have been committed.


Solution

  • I’m not sure your question is complete enough to give a full answer, but generally this question is asked in terms of persistent state machines. If Raft is simply being used to linearize and replicate client requests and the entries are being persisted separately (e.g. stored in a database) once committed, the correct approach is to periodically persist the lastApplied term and index for each node and delete all entries up to that point.

    However, note that when a node restarts there will still be some replay of logs which is technically unavoidable since applying entries and persisting the lastApplied index cannot be done atomically, so the replay of log entries still needs to be accounted for in the persistent state machine.

    Another complication is with catching up new nodes or nodes that have fallen behind the lastApplied index. In that case, you must send the persistent state as a snapshot to catch up the node.

    See the section on persistent state machines in the Raft dissertation.

    Regardless of whether this is precisely the use case you’re encountering, the general approach to preserving the safety of the system for entries that can be immediately discarded is the same.