Azure Service Bus – A Brief Technical Overview

On my previous blog post, I shared my live tech talk experienced. I talked about an overview of Azure Service Bus. A cloud messaging service that can be used to connect or integrate applications, devices and services to another.

Azure Service Bus solves complex messaging problems that many developers have to solve. We aim to develop quality and reliable cloud messaging solutions in our applications. The service Service Bus guarantees reliability, scalability, and security in messaging delivery. Send and receive reliable asynchronous messages or data that you can scale and secure.

Azure Service Bus Key Features

  • Message Sessions through FIFO (First In, First Out) ordered unbounded sequence of messages
  • Auto-Forwarding chains a subscription or queue to another, can automatically remove messages from queue/topics and puts them to another
  • Dead Letter Queue (DLQ) holds undelivered messages, lets you inspect and handle them
  • Scheduled Delivery delivers your messages in a specific time
  • Message Deferral defers the message retrieval for a later time
  • Client-side Batching allows batching of messages in a time period
  • Auto-Delete On Idle feature lets you manage and set the idle interval time after which a queue is deleted
  • Duplicate Detection gives the ability to resend the same message for recurring tasks or discard any duplicate copies

Azure Service Bus Integrations and Libraries

Solving complex messaging problems is one of the main uses of Service Bus. How you build your architecture, do the implementation and choose the integration depends on the consumer needs. Each use-case scenario is unique. Maybe your application does not require the use of Service Bus. Therefore, it is important to clearly identify what your customers or users needs.

Service Bus in Microsoft Azure cloud platform supports different programming languages. You can develop apps with it using C# .NET/.NET Core, Java, JMS (Java Messaging Service) and other supported-languages. It can be integrated with other cloud services too. For example, integration with Event Grids, Logic Apps, Azure Functions, Dynamics 365, or Azure Stream Analytics in your applications.

Below shows an example of an integration with other cloud services using Event Hubs.

Azure Service Bus Namespace

Creating a Namespace is vital in the development and integration of your applications with Azure Service Bus. Queues and Topics/Subscription should be in a Namespace. It serves as a container for different resources for the Service Bus within your app.

Example of Azure Service Namespace with Topic with two subscriptions

Azure Service Bus Queue

So how does Azure Service Bus Queue work?

If you have worked with queues in programming, you are probably familiar of concept of FIFO (First In, First Out) message delivery.

So what is special with Service Bus Queues? How does it differ from a normal queue?

Service Bus Queue is an asynchronous message broker. It is an entity where all your messages are stored and then distributed to different receivers. These receivers can be a service or an application.

A common scenario is communication between worker roles and web in an Azure app that have multiple tiers. Even solving communication problems or creating features in a hybrid cloud solution between on-premises apps and Azure-hosted apps.

Example illustration of Azure Service Bus Queue
Here is an example on how a Sender sends messages to receivers asynchronously using a queue

Code Example for an Azure Service Bus Queue (The Sender)

Azure Service Bus Queue Code Sample  for Sender

Above code is part of a simple console app example. It has an asynchronous Main-method with a new instance of a QueueClient. The QueueClient then takes two parameters : the connection string of the Service Bus and the name of your Queue. These information can be retrieved in Azure Portal. Right after the successful creation of Service Bus Namespace and when the Queue is created in it.

There are several ways to create a Service Bus Queue. The easiest one (without too much coding) is to create it via the Azure Portal. However, if you want to learn more and develop your skills in cloud command lines, you can use Azure Cloud Shell or Azure Powershell. If you have an existing ARM template ready, you can also use it to easily create a queue.

It is important to keep your Service Bus credentials secured. Using Azure Key Vault or saving your connection strings, name and keys in a secured configuration file are highly recommended.

Code Example for an Azure Service Bus Queue (The Receiver)

Azure Service Bus Queue Code Sample  for Receiver

The above code shows an example console app that receives waiting messages sent by the Sender app through Azure Service Bus Queue.

You set custom parameters using MessageHandlerOptions. You can set how many messages you want to de-queue at a time and AutoComplete. For more detailed guide on parameters and this class, please check out the class library for MessageHandlerOptions.

So, how do you verify if the Receiver received all the messages?
You can verify it by logging it as output to your console. Save the logs to the database if necessary. Another way is to administrate and manage diagnostic logs of Azure Service Bus Queues in the Azure Portal. For code samples of Service Bus queues, check out Service Bus messaging samples.

Topics and Subscriptions

If you want to deliver messages in a subscription way (publish-subscribe), then use the Topics and Subscriptions. Topic serves as a message broker with filter options such as SQL Filters, Correlation Filters, and Boolean Filters.

Azure Service Bus Topic Example Image from Microsoft Documentation site
Image Source: Microsoft Azure Service Bus Documentation Website

A distributed application’s components exchange messages through Topic that acts as message broker. The topic then asynchronously distributes the messages to all receiver subscribed on every subscription.

Topic Sender Code Example – Sends Messages to TopicClient()

Topic Sender Code Example - Sends Messages to TopicClient()
A simple Console app example which sends messages to its Subscribers by calling SendAsync() method of the TopicClient.

Topic Receiver Code Example – Receives messages from Sender through SubscriptionClient()

Azure Service Topic Receiver Code Example
Azure Service Topic Receiver Code Example
Azure Service Topic Receiver Code Example with ProcessMessageAsync

Learn More By Coding + Studying!

The overview and code examples above are just basic samples. If you want to dig deeper into using Service Bus in your Azure applications, I recommend the following documentation and online courses.

I am preparing for an Azure Certification exam AZ-204. Service Bus is just a tiny bit one the many Azure services that will be covered in it. If you are preparing for it too or just want to learn develop with Service Bus, I recommend checking out the links above.

Azure Service Bus solves complex messaging problems.


About Author: Jonah Andersson is a Filipina-Swedish Software Developer who codes full stack system development in C# .NET/NET Core. She currently works as a Software Developer Consultant at Forefront Consulting in Sweden. Jonah is passionate about learning, developing and sharing knowledge about Microsoft Azure cloud technologies. A woman in tech advocating gender equality in the tech industry through mentorship and by being role model.



Share the knowledge

Leave a Comment

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