Microsoft Flow is designed to create automated workflows between apps and services to synchronize files, get alerts, collect data, among others. You can create automated flows, button flows, scheduled flows, and business process flows. Although Flow contains convenient features in building flows, Flow users have notable experiences.
A lot of Flow users, who are mostly developers, faced challenges while working with this tool. One of the common challenges is that their flows run slowly. If you have encountered this issue before or you just want to know why flows tend to run slowly, then you are in the right page.
Flow Studio founder John Liu shared his knowledge on the five design changes or tweaks that can make your flow run fast in session 18. Liu was among the speakers who discussed important topics during the Microsoft Flow Online Conference held this month.
The five ways to make your flows run faster are the following: Compose, Parallel, Select, Filter, and Batch. Let’s go over each one of them starting with compose.
“A Compose is a constant. You’re defining something that cannot be changed afterwards,” he said. In the sample below, the flow has two actions. The sample flow has a trigger (manually trigger a flow) and actions (const array and var array). See screenshot image below.
The actions carried the same values, zero to nine. Take note that the const array is the Compose, which is one of the key elements Liu suggested to run flows faster.
Try to run the flow. You will notice that it only takes zero second. To check how fast exactly the flow ran, Liu used the Flow Studio app. The app showed that the const array took 0.09 seconds to run, while the var array took twice (0.18 seconds) as long to run.
“It is by nature a lot slower. And this only gets worse the bigger your array is,” Liu said, referring to the var array.
The main difference is that in Flow, the flow engines designed to run things in parallel, looping all the elements of both the const array and the var array.
Remember that Flow is designed to run in parallel, so all these actions have the potential to be running at the same time.
In order for Flow to avoid overriding variables at two parallel threads running over each other, Liu said each time you use a variable or modify variable there is a process where flow reaches out, gets a global log on this global variable, modifies it, and then continues.
“Whenever that happens, you’re paying the cost of not just updating a variable, you’re also paying the cost of obtaining a log and then releasing that log. That’s why variables are always slower,” Liu said.
Liu shared a trick using the Peter Method to avoid the said scenario by doing the same process without using a variable.
The method involves taking the peek code of an expression (Compose 2 in the example given below) and dragging the action out.
Note: A warning will display that says the action cannot be dragged out of foreach as it depends on an action inside foreach. Click OK.
Delete the Output and drag the action out from Compose 2. Come back to the Expression, paste the peek code again, and click OK. It should appear that your Inputs field has been updated with the code.
Make sure you rename the action. In the example, Liu renamed it as log const array. See photo below.
The flow automatically saves.
When you are outside a foreach and you do a Compose referencing an element that’s inside the foreach, it will concatenate all the resources together and give you an array (combined array) of everything that is on the inside.
The second technique in making a flow run faster is Parallel. In the dashboard, simply click on the ellipsis (…) icon beside the apply to each const array. Select Settings and enable Concurrency Control, which says for loops execute sequentially by default.
You can also override the default setting to customize the degree of parallelism. In the demo, Liu increased to fifty the degree of parallelism. Click Done.
Do the same thing for the var results. See image below.
The main idea is to configure flow to run in the parallel doing the apply to each const array and now the left and right side will both execute up to fifty elements at the same time. You see left side is already finished at two seconds. It is also completely sorted.
The right side took a while to finish because of the global log. Each of the fifty threads is now competing for the same variable, so the apply to each var array will have to append the log, set the variable, and release it.
Although the right side took a little bit longer, Liu said he still considered it to be quicker because it is running in parallel. Because they are running in parallel, you will see that the output in the right side is randomize, which is a result of multiple threads running all at the same time.
“Whichever thread happens to grab the log gets to put the value in,” Liu said.
The third technique, Select, involves giving an array of items, and you can decide which properties you may author for each item. See image below to see what the method looks like in Flow.
This method is good for simplifying. For example, you have a list of SharePoint items—and each SharePoint has twenty columns—and you just want to simplify that by name, ID, email of the created user. In the Select action, you can input values. See image below.
Another way of using the select method is by running an expression for each element. For each element, run an expression and retain whatever that is. You will notice in the screenshot image below that it only took zero second to run the flow.
There are three tricks in using Select method.
“The quickest way to go through every object and mutate it to some other object (when you are talking about selections) . . . is use Select. You’ll get it done in zero second,” he said.
“The second quickest way is you use apply to each, run in parallel, and do not use variables inside. Just use const inside and then on the outside use a Peter method to combine them back together,” he added.
Lastly, the slowest way is to use variables.
The third method, Filter, involves reducing the size or number of the array that will make the subsequent runs quicker. Filter the array down as quickly as you can.
Here’s an example of this method.
In the screenshot above, the values are: random between zero and ten, greater than or equal to, and 5. The Filter method should look like the image below.
Filter is a good way if you received a last set of data and you are looking for one that has a particular ID, rather than looping through each one.
The fifth technique, Batch, has another beneficial to those who would like to build a series of rows and send that to SharePoint in fixed format so that SharePoint can quickly perform a whole bunch of series of actions in one core.
To perform this method, you need to create a series of actions. In the demo, Liu created actions in Flow (like a map) that contain the following: SharePoint (Get Items), Select 2, Filter array, const guid1, const guid2, Compose ChangeSet-Chunk, Select ChangeSet-Insert, and Send an HTTP request to SharePoint. It looks like the image below.
Once the flow is saved, your SharePoint shall receive the items. See photo below.
Liu said there are several connectors in the Flow that can do the Batch technique. The most notable ones are the Azure table storage batch, SharePoint’s batch (up to 100 batch requests), and Microsoft Graph’s batch (up to twenty-five batch requests).
Aside from Liu, the other speakers of the online conference were the following:
Shane Young, “Why I Love Microsoft Flow” (Session 1)
Melissa Hubbard, “Getting Started with Flow Approvals” (Session 2)
David Patrick, “Intro to the Common Data Service in Flow” (Session 3)
Anton Robbins, “Things to Know Before You Flow” (Session 4)
Sean Bugler, “Tell a Better Story with Flow” (Session 5)
Elaiza Benitez, “Delaying Emails Based on Time Zone” (Session 6)
Daniel Christian, “PowerApps and Flow for Social Media” (Session 7)
Tomasz Poszytek, “Scopes and Run After Actions” (Session 8)
Sandy Ussia and Daniel Laskewitz, “The Flow Pro Show” (Session 9)
Scott Shearer, “Flow for SP Designer Workflow Developers” (Session 10)
Sarah Critchley, “Flow, Model Driven Apps & Cognitive Services” (Session 11)
Gabriel (Galo) Corvera, “Flow + Teams + Adaptive Cards” (Session 12)
Devin Knight, “Power BI Streaming Data sets + Flow” (Session 13)
Haniel Croitoru, “You Have an Error in Your Flow? Let’s Deal with It!” (Session 14)
Vivek Bavishi, “Sending .vcf Contact Cards with Flow” (Session 15)
Joe Unwin, “Getting Started with Custom Connectors in Flow” (Session 16)
Geetha Sivasailam, “Microsoft Flow and Azure Dev Ops” (Session 17)
There were 29,109 people who registered for the online conference, according to Microsoft Flow senior program manager Jonathon Levesque.
If you would like to watch all the eighteen sessions, click here.