Workflow with a "state"

Hi all,
so I have a workflow with 3 activities
The first activity calls an API and returns a model.
Second’s + Thirds payload is the model from the First activity.
So second activity triggers an install.
The third one monitoring the installation (polling).

I saw that once the monitoring is failed for some kind of reason,
Temporal again runs all 3 activities, so it triggers another installation.
As I understood, we should not use “if/else” in workflows because it breaks the deterministic.
My question is, can I add an if statement for the payload? I mean if the payload is not null, just go straight to monitoring activity?


This is not expected behavior unless you use local activities.

As I understood, we should not use “if/else” in workflows because it breaks the deterministic.

This is an incorrect understanding. You can use any Java conditional, and other language constructs inside the workflows. Determinism means that they should produce the same result given the same values of the variables. So

if (a > 5) {

is deterministic. But

if (a > random(5)) {

is not, as the condition can take a different branch even if the value of a is the same.

So I can use a local variable inside a workflow? initiate it to null,
assign the result from activity1
create an “if” for the second/third activities.
As it is not really random, it is a question of null or not.
Or, once it can be null or not, based on the same input - it is random?

Hello. Code inside activity could do anything. Code inside workflow must be deterministic, what means that same workflow method call should produce same sequence of events.

void workflowMethod(int param) {
    int result = activity1(param)


when activity1 is completes temporal store its result in internal db. This means that it wont call that activity again, so the code in that activity can do anything

int activty1(int param) {
   return new Random().nextInt()

But you cant do something like that:

void workflowMethod(int param) {
    int result = activity1(param)

    int test = new Random().nextInt()
    // no deterministic error here
   activity2(result, test)


what will happen here:
temporal scheduled activity 1
temporal execute activity 1 (return random int) and store its result in db

next we generating random number inside workflow, for example it will return 4
nex temporal scheduled activity 2 with params: anything and random number = 4
temporal executes it and store result in db

next sleeping for 1 minute

and when workflow is awake temporal will reexecute a worflow methods with cached results from events:

it will schedule activity 1 and get result from cache,
next it will generate a random number again (it wasnt cached), for example it will be equal to 6
next it will schedule activity 2 with params anything and 6, and validation will be failed because params should be anything and 4, thas why NoDeterministic error would be thrown

1 Like

It will be assigned the result of activity1. So it cannot be null after that. So such code is absolutely deterministic.

1 Like