raft icon indicating copy to clipboard operation
raft copied to clipboard

Applied commands get reapplied after restart

Open evanxg852000 opened this issue 1 year ago • 2 comments

I have developed and key-value store with Bbolt. when testing, I noticed the previously applied commands get applied again after restart. I also tested this with the basic example in the repo. For an in-memory (volatile) key-value store this is fine. But for a persisted one this is creating duplicated values. I mean my raft commands are not idempotent so I want to make sure it only gets applied once. Please, could you tell me how I do this?

Is there a way to tell to set the current node appliedIndex right after applying? As I can see in the log, the apply index is always zero.

Note: I am just starting with a single node for now.

I have read this (https://github.com/shaj13/raft/issues/7), it's related but could not understand its relation to the snapshot

On a design note, how are you handling errors during apply? My intuition was that the StateMache.Apply(data []byte) should return an error that informs the engine that we could not apply and therefore need to retry the same entry sequence. if no error appliedIndex gets updated forward. I may be missing key understanding here, just trying to solidify my intuition around raft and this implementation. Thanks

evanxg852000 avatar Jan 09 '25 13:01 evanxg852000

@kotyara85 Maybe your experience from this (https://github.com/shaj13/raft/issues/7) issue can help here

evanxg852000 avatar Jan 09 '25 13:01 evanxg852000

Go it resolved here https://github.com/hashicorp/raft/issues/523

evanxg852000 avatar Jan 10 '25 19:01 evanxg852000

When Raft restarts, it re-applies all replicated logs, either from the write-ahead log or from a snapshot. Therefore application should clear its persistent database on startup, if it maintains one, to avoid replay conflicts or duplication.

shaj13 avatar May 05 '25 17:05 shaj13