Why can't Workflows contain non deterministic code? And how does using activity solve the problem?

  1. To recover workflow state the workflow function is reexecuted from the beginning. All Temporal APIs return recorded responses which are exactly the same as the original ones. If the workflow code is non deterministic then it can end up in a different state than the original execution.

For example, the original execution of the workflow can execute activity A. But during replay, it might execute activity B which is not going to match the execution history.

if (random() > 0.5) {
  ExecuteActivity("A");
} else {
  ExecuteActivity("B");
}
  1. Activity result is recorded in the workflow execution history. So during recovery the result is taken from the history without executing the activity.
1 Like