Welcome again to another Logic Apps Best Practices, Tips, and Tricks. In my previous blog posts, I talked about some of the essential best practices you should have while working with Azure Logic Apps. Check out these Logic App tips and tricks!
Today I’m going to speak about another important Best practice, Tips and Tricks that you need to take into consideration while you are designing your business processes (Logic Apps): Foreach Parallelism.
#18 Foreach Parallelism
In programming languages, task parallelism divides tasks allocating separate threads for processing them, basically running a piece of code in parallel (more or less at the same time). Logic App is not different than traditional programming languages, and it also has this concept. One of the default tasks (shapes/actions) that have this capability out-of-the-box by default is the For each action.
In terms of looping inside Logic App, we can say that we have two types:
- Until loop: To repeat actions until a condition gets met or a state changes, we can and should use an Until loop. Your logic app first runs all the actions inside the loop and then checks the condition or state. If the condition is met, the loop stops or repeats.
- The Until loop is single thread execution – sequentially (non-parallel)
- Foreach loop: To process an array in your logic app, you can create a Foreach loop. This loop repeats one or more actions on each item in the array.
- Once again, the different interactions will be run in parallel. The default number of parallel executions (threads) is 20.
- Note: This behavior differs from Power Automate’s Apply to each loop where iterations run one at a time or sequentially.
To check the default number of parallel executions or to change this value, you need to:
- Click on the three dots in the top right corner of the Foreach action
- And then click Settings.
- On the Setting for the Foreach action, enable the Concurrency Control property.
- On the Degree of Parallelism property. You can now define more parallel operations (actions) to a maximum of 50 or less to a minimum of 1 (sequentially)
Note that even if you don’t enable the Concurrency Control property, by default, it runs in parallel 20 iterations of the array by default. The exception to the default behavior is nested loops, where iterations always run sequentially, not in parallel. To run operations in parallel for items in a nested loop, create and call a child logic app.
When to use parallelism or not?
- For processes that require first-in, first-out behavior, we want to set the Degree of Parallelism to 1.
- We are free to modify our Degree of Parallelism for processes that do not require in-order delivery.
However, it is essential to understand the connectors we are using may have rate limits imposed. It may mean that having a lower parallelism value will perform better than a more significant value as we may avoid rate limits that will force retries from Logic Apps. For this reason, it is essential to test with business scenarios to have the most predictable outcome.
Another two situations you need to take into consideration are:
- In the case of many ADO items, the Foreach block will DDOS ADO server. It will cause error 429 (too many requests). To reduce the number of requests, we can adjust the level of the parallelism for a loop.
- In cases of old systems or with a small number of resources that cannot handle many parallel actions. We may need to reduce the number of parallel processing we want to implement in our Foreach loop or, in some cases, set it to process sequentially.
You can reach this post here to get the full list of limitations. Shortly :
- The Foreach loop is limited to 100 000 iterations.
- Parallelism is limited to 50. The default value is 20.
- In general, setting variables within a Foreach loop is a mistake. The reason why is, in fact, because the parallelism existed by default in the Foreach loop action. You can’t control when it each iteration starts or ends, so avoid setting variables inside a “parallel” Foreach loop. For instance, you should prefer a Until loop if you need to increment an integer within the loop.
For each parallelism consequences
- Because the Foreach loop comes with its parallelism behavior, each iteration will execute as a separate thread. That means that you cannot terminate a Logic App execution instance within a for each statement nor stop the process if one iteration fails. That’s true even if you have set your degree of parallelism to 1. If you need to stop your process at any time, then think about using an Until action.
Triggers also have Concurrency control, but I will leave it to another tip and trick.
Stay tuned for the following Logic App Best practices, Tips, and Tricks.