Subscription filtering in Azure Service Bus

Anupam Chand
6 min readMay 6, 2021

Hi there.

We had a requirement recently of implementing queue logic to allow for loose coupling. As part of that research, I did some testing on how we could use Service bus for this and thought I can share what I have discovered.

Service bus is one of the queue PaaS services available on Azure. It comes in 3 tiers : basic, standard and premium. Some good initial reading about what a service bus is can be found here https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview

Pricing and functionality of the different tiers can be found here : https://azure.microsoft.com/en-in/pricing/details/service-bus/

For today’s demo, we will use a standard service bus with a topic and 2 subscriptions. We will have a different function bound to each subscription. We will then feed messages into the service bus and check if the subscription filtering is happening as per what we have setup. A simplistic diagram is shown below.

Topics and subscriptions are not available in the basic tier of service bus. So for this demo , let’s create a standard service bus. Go to the Azure Home page and click on “Create Resource”.

Enter “service bus” in the search box and click on “Service Bus”. Click on Create.

Select your resource group. Enter a ‘name space’ name(I have selected ‘anusbns’).

Select a location(I have selected ‘Central India’) and select the ‘standard’ tier.

Click on ‘Review + create’ and then click ‘Create’.

After the deployment completes, click on ‘Go to resource’ to navigate your new service bus namespace.

Click on ‘+ Topic’ to create your first topic. Enter a name for your topic, leave the remaining fields as default and Click on ‘Create’.

Once the deployment completes, you should see your topic at the bottom part of the portal. Click on your new topic (in my case, ‘mytopic’).

We will now create 2 subscriptions, ‘sub1’ and ‘sub2’.

Click on ‘+ Subscription’.

Set the name of your first subscription to ‘sub1’ and set the ‘Max delivery count’ to 5. Leave the remaining options as default and click on ‘Create’

Repeat the same process to create another subscription called ‘sub2’. Once you create both the subscriptions, you should see both of them as shown below.

Click on your first subscription ‘sub1’. Click on ‘+ Add Filter’. Give a name to your filter and click on the ‘CorrelationFilter’ tab. Under system properties, select ‘label’ under the Key dropdown. For the corresponding value, enter ‘Fruit’. Once done, click on ‘Save changes’.

Similarly, for your 2nd subscription ‘sub2’, create a new filter. Give it a name, select ‘label’ from the Key dropdown under System properties, enter ’Veg’ in the corresponding value field and finally click ‘Save changes’.

You can refer to the screenshot below if you have any confusion.

You have now setup your service bus. It is time to create some functions which will be triggered by the respective subscriptions.

Now let’s create our function app. Go to the Azure Home page and click on “Create Resource”.

Enter “function” in the search box and click on “Function app” in the drop down list.

Click on Create.

Select your resource group. Enter a function name (I’ve selected mysbfunc).

Set the runtime stack to .NET. Select your region (I’ve selected Central India).

Set the other options as shown below.

Click on “Review + Create” Once validation is complete, click “Create” and wait for the deployment to be completed.

*The function app will automatically be created on consumption plan which is the cheapest plan available.

*The benefit of using .NET is that you can directly edit the code in the portal. There is no need to Visual studio.

Now go to your function app and click on “Functions” on the left side pane. Click on “+Add”. As a template click on “Azure Service Bus Topic Trigger”. Give your function a name. Under ‘Service Bus connection’ click on ‘New’. Select the service bus which you just created. Verify the topic name and set the Subscription name to your first subscription ‘sub1’. Then click on ‘Add’.

When the function is created, click the ‘Code + Test’ to enter some code.

Replace the code in the screen with the following code.

using System;

using System.Threading.Tasks;

public static void Run(string mySbMsg, ILogger log)

{

log.LogInformation($”This function is to process fruits and the message body is : {mySbMsg}”);

}

Click the ‘Save’ button.

Now repeat this process to create one more function. Give it a different name and make sure you enter the 2nd subscription ‘sub2’ as shown below. Click on ‘Add’.

When the function is created, click the ‘Code + Test’ to enter some code.

Replace the code in the screen with the following code.

using System;

using System.Threading.Tasks;

public static void Run(string mySbMsg, ILogger log)

{

log.LogInformation($”This function is to process vegetables and the message body is : {mySbMsg}”);

}

Click the ‘Save’ button.

The setup for our demo is complete. Time to see it in action.

Keep both function screens open so you can see the logs and any activity on them as they happen as shown below.

For this, we will manually enter some messages into our service bus. Navigate to your service bus topic and click on ‘Service Bus Explorer’.

Select the ‘Application/Json’ from the Content Type dropdown. In the message box, enter the following JSON data.

{

“item”: “Apple”,

“weight”: “5kg”

}

Click on ’Expand Advanced Properties’ and enter ‘Fruit’ against the Label field. Finally click ‘Send’.

Now, let us revisit each of our functions and see if there was any activity.

So we can see that when we pushed a ‘fruit’ message into our service bus, only the 1st ‘Fruit’ function was triggered. Let’s try and push in a ‘Vegetable’ message.

When you click on ‘Send’ check the activity on the functions once more.

In this case, only the 2nd function was triggered.

This shows us that we can have a listener on one subscription and another listener on another subscription. Even though you push data to the same endpoint/service bus, the different listeners can pick up different data depending on the filtering logic. This is exactly what is meant by pub-sub (publisher-subscription) model/pattern.

I hope this demo was useful. As always, if there are any questions or feedback, feel free to drop a comment.

On a parting note, don’t forget to delete all the resources which were built as part of this demo to avoid unnecessary billing.

--

--

Anupam Chand

IT Solution architect with an interest in cloud computing and anything geeky and techy.