Logic Apps foreach and variables

Sometimes we need to work with a variable inside a loop section. Whether it’s a precomputation or just a helper variable. Logic Apps allows us to do so. Yet the variable must be initialized on a global level (above all loops).

Here comes the problem:

By default, foreach runs in parallel, in 20 threads (instances). Now, because there is no such thing as mutex in Logic Apps, there is no way how to create a critical section. Critical section is a section only one thread at a time can enter. That results in dirty reads.

We can solve this problem by running the loop synchronously. You can do that by editing settings of the foreach block.

Now only one thread at a time will execute the foreach loop and no other thread will modify our variable while we work with it.

Leave a Reply