Correlating messages over Logic Apps using Service Bus

When working with Azure Logic Apps, I like to have each Logic App do a single piece of work, as this allows us to mix and match these Logic Apps in various flows. For this demo, we will be using a very simple representation of this, where we have one Logic App which receives the message and send back a response to the original caller, another Logic App which does transformation of the message, and finally a Logic App which calls a backend system. To decouple these Logic Apps we will be using Azure Service Bus topics, providing us with routing capabilities and allowing us to handle downtime more easily.

Architecture

Architecture

Now the challenge we were running into, is that we needed to give the response which we received from the backend, back as a response to the client.

Requested architecture

Requested architecture

Of course, since we have implemented communication between the Logic Apps asynchronously and decoupled by using Service Bus in between, we don’t have a return channel on which we can send the response. In this post, I will show how we can solve this by using Service Bus sessions.

Service Bus Sessions

With Service Bus sessions, messages can be grouped together so they can be received in a first in, first out pattern. However, it also allows us to listen to specific messages from a Logic App. When creating your topic subscriptions, be sure to enable sessions to use this functionality.

Enable sessions

Enable sessions

For the subscriptions, we use a filter like the following:

Set filter

Set filter

Client Logic App

The client Logic App is where communication with the client application is done, and the incoming message is placed onto a Service Bus topic to be sent to the transformation Logic App. Currently the Response action just sends back a status code 200, but we will want to change this to use the response from our backend.

Message is sent to Service Bus

Message is sent to Service Bus

To be able to correlate our backend response message back to this Logic App, we will need to set a Session Id on the outgoing message. As this should be an unique identifier, we will create a GUID for this.

Create and set Session Id

Create and set Session Id

This Session Id will be passed on all the way to the backend, and finally be used to send the message back to the client Logic App. To be able to receive the correct message back in our Logic App, we will add an action to listen for messages on a Topic, with a certain Session Id (which will be the GUID we just created).

Correlate on messages with Session Id

Correlate on messages with Session Id

And finally we will use the message we received from the topic to set up our response, and complete the message from the queue.

Create response and complete

Create response and complete

Transformation Logic App

The next step in our process is a Logic App which does the transformation of the incoming message to the outbound message. Do note, that because our topic uses sessions, we have to use the peek-lock actions for Service Bus, setting the Session Id option to Next Available on the receive action, and setting the Session Id to the Session Id of the incoming message on the complete action. Also, because Service Bus messages are base64 encoded, make sure to convert it to back to a JSON string before parsing the contents.

Receive and parse message

Receive and parse message

Now to make sure we can continue our correlation, we have to send the Session Id from the incoming message along with the outgoing message.

Set Session Id for further correlation

Set Session Id for further correlation

Backend Logic App

And finally we have a Logic App which calls our backend service using the transformed message from our previous Logic App. In this sample, I am using RequestLogger, a clone of MockBin (as MockBin itself is not working at the time of writing this article). If you want to run this for yourself, just make sure to create your own Bin and replace the URL on the HTTP action. Please note we have to use the peek-lock actions here as well, and convert the content of the message from Service Bus from base64 to a JSON string, same as in the previous Logic App.

Receive message and send to backend

Receive message and send to backend

Now to get the response from our backend back to the client Logic App, we have to add an action which will post to the Service Bus topic, using the Session Id which has been passed along our various steps in the process.

Set Session Id for correlation and pass through response message

Set Session Id for correlation and pass through response message

Conclusion

We can now test our process, by sending the following message.

{
    "Customer":"Eldert Grootenboer",
    "Product":"Surface Book 2",
    "Amount":"1"
}

This will trigger the various Logic Apps, and pass the Session Id over the Service Bus topics, until finally the response from our backend is sent back to the client Logic App, which returns it as the response to the client.

Response contains backend's response

Response contains backend’s response

The code with this project can also be found here on GitHub, and you can deploy the complete solution with the following button.

Leave a Reply

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