Build Different with Power Automate – Episode 3: Caveats with Parallelism

Variables

We love using variables in programming languages. They make your program readable (if used properly) and help with the debugging process. There are a few gotchas with variables in Flow.

Declare outside of the loop

Programming languages support declaring local variables almost everywhere, including within the loop.

The same approach can’t be applied in Flow of Power Automate. Declarations of variables must be at the top level. You cannot declare a variable within the loop.

If you are adding Initialise Variable action inside Apply to Each action, you will get the following error message

Initialise variable at the top level

Problem?

This creates a problem when concurrency control is turned on at Apply to Each action.

Concurrent processes will override the same variable and as a result, you end up reading the value from another concurrent process.

Let’s see the following example to see how parallelism will override the variable.

Set Value vs Compose

First, let’s make the following changes to the Flow from Episode 1.

First, add a new action to Initialise Variable before Apply to Each action.

Initialise Result variable

Second, add a new action to Set value to the variable and add another Compose action to check the variable value within Apply to Each action.

Update Result variable and check the value of the variable via compose

The expectation here is the variable value during the Set action should remain the same in the next step, Check the variable again. Make sure you turn on the concurrency at Appy to Each action and run it. Look at the result. They are not same!

The value is set to ‘seed’, but when its value is checked in the next immediate step, it shows ‘agriculture’.

Checking a variable value right after setting its value in the loop

Why? How could this happen?

It is because of the parallelism. If you are a developer, you will know the importance of Thread Safety in multi-thread programming. In short, setting variables inside Apply to Each action is not a thread safety.

In this example, you can use Current Item without using any variable or Compose action. However, if you need to write a complex expression of the current item, you can use Compose action as a read-only local variable.

Tips

Variables can help us in simplifying complex expressions, especially when it is repetitive. Use Compose action as a local read-only variable instead of a variable to hold the result from a complex expression inside Apply to Each action. Compose action is like a read-only local variable.

Number of Parallelism

The number of parallelism is NOT always a linear proportion. Configuring 2 parallelisms could give 2 times in performance. Increasing it to 4 parallelisms may not give another 2 times (total 4 times) in performance.

When you call external APIs such as Common Data Services, SharePoint, always pay attention to API limits.

Tips

Always find your sweet spot when you turn on concurrency control in Apply to Each action. It is a trial and error approach.

Resources

You can download the example flow here.