Optimistic State Machine Execution
Distributed Consensus algorithms play an essential role in any Distributed System in which a cluster of nodes must agree on a single value - usually an order of user requests. Once user requests are replicated and uniquely ordered on each node (in a replication log), a node can apply the agreed-upon request to its state machine. If each node has the same implementation of a deterministic state machine, the state on all nodes will be the same (up to the given user request). Voila, we have achieved redundancy in our system, and if some of the nodes fail, we still have the state available on other nodes for users to consume.
Idempotency
It is crucial that the state machine is idempotent and that re-executing the same request does not hurt its consistency and, after all, its determinism.
A convenient way of achieving state machine idempotency in a Distributed Consensus environment is to deduplicate requests from the replication log. Replication log entries are uniquely identified by their replication_log_index. Along with the request, the apply process gives the replication_log_index to the state machine. The state machine durably stores this value as its deduplication key. It uses it to identify requests it has already processed - "I don't process the request if its deduplication key is smaller or equal to the last one I have seen."
Pessimistic execution
The premise I want to take here is that replicating requests usually does not fail. I said this and stayed alive. In a pessimistic scenario, executing the request would first involve replicating it across the cluster of nodes and invoking the apply process once it has been replicated. The apply process takes the request and gives it to the state machine. The state machine executes it, and the apply_index gets updated. We are now free to move forward with the subsequent request.
Optimistic execution
If the replication is usually successful, why wait for it to complete before we apply the request to the state machine? Well, it is not necessary to wait. We could start executing the request against the state machine while it is being replicated. Once both processes are done, we can update the apply_index.
In the end, a little optimism wouldn't hurt :)
Comments
Post a Comment