If you’ve ever had strange, unexplainable behaviour in a Logic App loop that uses variables chances are the cause will be the Concurrency Control setting, which is turned off by default.

What is Concurrency Control?

The default behaviour in Logic Apps is that for each loops will process multiple items concurrently (aka at the same time). This can be a great thing as if you have a Logic App processing many items, they can be processed faster by multi-tasking!

If you are using or manipulating variables within the loop it can have very unexpected results.

String builder illustration

To illustrate this inconsistent behaviour I’ve built a very simple logic app that does the following tasks:

  1. Creates an array of characters with the values [“a”,”b”,”c”,”d”,”e”,”f”,”g”]
  2. Creates an empty string variable
  3. Loops through the array of characters and adds each character to the end of the string

This seems really simple, you’d expect the string at the end of the app to have the value – abcdefg. But with a default Logic App setup it doesn’t!

Logic App steps

Here is a quick overview of the Logic App steps I created for this example.

1 – Initialise the character array and an empty string.

2 – Loop through the characters and append each one to the end of the String

3 – Finally so we can see the final value of the string I’m terminating the Logic App with the string value as the code.

Result with Concurrency control off

Whilst looking at the run history of the Logic App it appears the array items were processed in order, however here is the output. It’s not the output we expect and may want – in this example it generates abcegdf. The first 3 characters are good, but with the remaining 4 the order is jumbled.

Result with Concurrency control configured

Let’s now edit the advanced settings of the loop, and turn Concurrency Control on and set the Degree of Parallelism to 1.

The result is now what we want!

Implications

This is a very simplistic example, however demonstrates what unexpected behaviour can occur if you are using variables within a for each loop with Concurrency Control turned off (which is the default setting).

By Andrew

Leave a Reply

Your email address will not be published. Required fields are marked *