Batching in Logic Apps

published in category technology on Aug 21, 2017

After getting to know how to use variables in Logic Apps, it is time to check out another new functionality: batching.

Batching is handy when you need to group messages before processing them. For example to save transaction costs you might want to group API calls and send objects as an array instead of sending individual requests for each object.

For now it is only possible to have a fixed number of messages to trigger the batch, but triggering based on timespans is coming. And using combination of the two. For example: trigger the batch if 10 messages have been received, but also trigger at least once every hour.

You can also have partitions within a batch. What it means is that each partition can collect messages and only messages within same partition count when evaluating the batch triggering. For example you could have orders coming in to different departments of your company and each deparment could have their own partition. Therefore the trigger would fire only after a department has received enough orders to trigger the batch.

To demonstrate the functionality, let’s create an imaginary warehouse. There are employees working in the warehouse that collect items according to received orders. To make collecting objects from the warehouse shelfs more effective, warehouse management wants to wait until certain number of items have been ordered from a certain section of the warehouse. To make this case a bit simpler, let’s assume that the location of the item within the warehouse (=section) can be decided from the item number coming in the order (first number of the item no).

Once the batch is triggered for certain section of the warehouse, information of the items that should be collected is sent via email to an address that is dedicated to employees working in that section of the warehouse (warehouse.section#@company.com).

Create receiver for batched messages

You need to create the receiving side first. Be carefull when you pick the name for your batch, I found it impossible to change the name afterwards without creating totally new Logic App. In my warehouse, the receiver is very minimalistic:

All you need to define is the name for your batch and number of messages to batch before triggering the workflow. As you can see, to keep things simple, I just send the whole body containing all batched messages as the email content. Not an example of userfriendliness, but will have to do for now. Receiving address has partition name included, which in this case is the section number and that is how the email is routed to right recipient.

Create sender for the bached messages

Logic App generating the requests for collecting items from the warehouse is not much more complicated than the receiving side:

Process is initialted by gettign the order as http-request. Request payload contains just three attributes:

{
  "itemno" : 12345,
  "itemname": "Screw",
  "quantity": 5
}

Warehouse section is then parsed from the item number and all that is left is to call the receiving application. Request body is inserted as message content and parsed warehouse section as the partition name. That way the receiving side will batch each section separately.

Let’s run the business

As I opened my warehouse for business, orders started to flow in. As a business manager I was happy. I had multiple customers ordering products from section 1 of my warehouse: screws and bolts, nails, etc. Once the batch size for that section was fullfilled, my receiving app triggered as expected and an email was sent indicating that products should be collected.

As my very handy warehouse employees parsed the email content to more readable format I was pleased to see that only orders from one section of my warehouse were included in the batch. Just like I planned it:

{
    "batchName": "warehouseItems",
    "partitionName": "1",
    "items": [{
        "messageId": "930d21a1-e5c2-44d6-8fd5-2943f654191e",
        "content": {
            "itemno": 12345,
            "itemname": "Screw",
            "quantity": 5
        }
    },
    {
        "messageId": "ad55e481-64a5-4075-822f-67f1d5675a5f",
        "content": {
            "itemno": 12299,
            "itemname": "Bolt",
            "quantity": 456
        }
    },
    {
        "messageId": "151b8dc5-b040-46ca-a8db-28d7033d37df",
        "content": {
            "itemno": 17777,
            "itemname": "Nails",
            "quantity": 789
        }
    },
    {
        "messageId": "79eccd17-6c32-410b-a671-603a488acbfc",
        "content": {
            "itemno": 17779,
            "itemname": "Really big nails",
            "quantity": 755
        }
    },
    {
        "messageId": "1287a583-79e7-4b83-9507-8385e1ce2546",
        "content": {
            "itemno": 15500,
            "itemname": "Plastic",
            "quantity": 1001
        }
    }]
}

As the day went on I started to get bit worried. I could see that there were orders also for other sections of my warehouse, but not quite enough for triggering the batch. For example there was an order for nails that got collected, but a nailgun from a different section was still waiting for additional orders to flow in. To make sure that my customers would still be happy with the delivery times, I would also need a maximum time limit for my batches to trigger. That way I could make sure that orders would not be waiting too long to be collected.

Summary

Batching of messages has been done really easy in Logic Apps. It is a matter of defining only couple of parameters and you are good to go. Can’t imagine it being any simpler. However, with the current implementation you can only trigger batches using fixed messagecounts. That is limiting the use cases for this fuctionality at the moment, but we should see time based triggers within near future. That will be a big improvement and would make me as a warehouse manager even happier!


My name is Juha Ryhänen. I’m interested in everything related to productivity, remote work, automation and cool gadgets. This is my personal website where I write about the things I find interesting. Maybe you do too? [More]

Contact:
+358 50 543 4543 | [email protected] | Twitter