Reputation: 2401
What I should consider in a design regarding to workflow reset?
Upvotes: 2
Views: 2453
Reputation: 2401
Workflow Reset is probably the most powerful & unique feature in Cadence/Temporal. It allows you reset any workflow execution to any previous point of history, with all signals preserved(by default, but can be skipped if specified) to be re-applied.
Here is the best practice to design your workflow to be resettable:
Activity(or State API in iWF) should be idempotent and no side effects when re-executing. Although activity should be idempotent already disregarding reset, activity won't be re-executed after result is recorded into history. But with reset you should expect the activity could be re-executed even after it has been executed and recorded into history because workflow could be reset anytime.
Workflow should be able to handle signal reapplication -- this is the default reset behavior but you can also skip signal-reapplication in some rare cases. Signal reapplication should be preferred by default, as signals are usually used as external data input and could be hard to be re-attrieved.
If 1. cannot be implemented, consider sending signal to self to avoid re-execute some logic(eg activity) that is not idempotent or has side effects when re-executing. Signals are specially handled for reset. All the signals are preserved by default. For example, if you have a workflow that read some data and then write some data, after reset, the workflow may read different data to write to DB which could cause some side effects. To fix this, you can send a signal to self with the read result and process the signal in the workflow.
Alternatively, consider using a different workflow to perform actions on some logic that is not reset safe — eg emit some metrics may not be safe to be reset. So that the reset can be performed on the workflows that are resettable safe.
Don't use childWorkflow feature. Use activity to start other workflow as "child workflows". See Should I use child workflow or use activity to start new workflow for more. (iWF doesn't let you use childWF)
Expect to reset the workflow to very beginning(firstDecisionTask/firstWorkflowTask). Though it's allowed to reset a workflow to any point of the history, it's generally super hard to find the reset point(eventId) as it's hard to interpret the history events with your workflow code. Resetting to beginning makes your life easier for resetting thousands of workflows. Although, expect to pay for some perf penalty for resetting to beginning. (finding reset point will be a lot easier with iWF as iWF will directly correlate your business logic with the history eventId by using stateId)
Upvotes: 5