Understanding Azure Service Bus Topic and Subscription Management

Author

Reads 769

Top view of azure ocean with foamy waves washing sandy coast near green bushes on summer day in tropical resort
Credit: pexels.com, Top view of azure ocean with foamy waves washing sandy coast near green bushes on summer day in tropical resort

Azure Service Bus Topics are a great way to decouple applications and services, allowing them to communicate with each other in a scalable and reliable way.

A topic is essentially a container that holds multiple subscriptions, each of which can receive messages from the topic. This allows for multiple applications or services to subscribe to the same topic and receive messages independently.

Each subscription has its own unique name and can be configured to receive messages from the topic based on specific criteria, such as the message's content or sender.

In Azure Service Bus, topics are created as a part of a namespace, which is a logical container for related resources.

Service Bus Fundamentals

Service Bus provides all three options for application communication: simple queues, queues with publish-and-subscribe mechanisms, and connections between applications without queues.

Each user creates a namespace and defines the communication mechanisms needed within it. This is a multi-tenant cloud service, shared by multiple users.

Credit: youtube.com, How to use Azure Service Bus Topics | Azure Tips and Tricks

Queues allow one-directional communication, storing sent messages until received by a single recipient. Topics provide one-directional communication using subscriptions, with each subscription optionally using a filter to receive specific messages.

Relays provide bi-directional communication, passing messages directly to the destination application without storing in-flight messages. Event Hubs offer event and telemetry ingress to the cloud at massive scale, with low latency and high reliability.

Each object, whether a queue, topic, relay, or Event Hub, has a unique identifier created by combining its name with the namespace name. Applications can use this identifier to communicate with one another.

Windows applications can use Windows Communication Foundation (WCF) to access these objects, while non-Windows applications can use Service Bus-defined messaging APIs or SDKs for languages like Java, Node.js, and others.

Applications can run anywhere, and Service Bus can connect them to the cloud, or to other applications running on Azure, in a datacenter, or even on mobile devices or household appliances.

Creating a Topic

Credit: youtube.com, AZURE SERVICE BUS TOPICS & SUBSCRIPTIONS- Getting Started | Azure Series

Creating a Topic is a straightforward process. You can create a topic by clicking on the "Topic" button after upgrading your pricing plan to Standard. This is because the Topic button becomes available once you've upgraded.

To create a topic, simply choose its name and some optional information. It's a simple process. Once you've created your topic, you can navigate to its page and manage the subscriptions related to it.

You can have multiple subscriptions linked to a single topic. For example, in one scenario, a topic called "pizzaorderstopic" had two subscriptions: "PizzaChefSubscription" and "PizzaInvoicesSubscription". Each subscription can receive messages that match its filter.

Here's a quick rundown of the steps to create a topic and subscription:

  • Create a topic by clicking on the "Topic" button after upgrading your pricing plan to Standard.
  • Choose the topic's name and optional information.
  • Navigate to the topic's page and click on "Subscriptions" to manage the subscriptions related to it.
  • Create multiple subscriptions linked to a single topic, each with its own filter.

This approach, called publish and subscribe, is useful whenever multiple applications are interested in the same messages. By defining the right filter, each subscriber can tap into just the part of the message stream that it needs to see.

Sending and Receiving

Credit: youtube.com, 23. Send Message to topic in Azure Service Bus

To set Azure Service Bus metadata when sending a message, you can set query parameters on the HTTP request or the gRPC metadata. This allows you to specify properties such as the MessageId, CorrelationId, and SessionId.

The metadata.MessageId property does not set the id property of the cloud event returned by Dapr and should be treated in isolation. If the metadata.SessionId property is not set but the topic requires sessions, an empty session id will be used.

You can send a message to a Topic using the same code as for a Queue, as the operation is transparent to the client. Simply instantiate a new Sender and send a message through it, using the Topic Name instead of the Queue name.

To receive a message from a Topic, you need to instantiate a Receiver and specify the Topic name and the Subscription name. This allows you to access the message metadata, including DeliveryCount, LockedUntilUtc, and EnqueuedTimeUtc.

Here are the settable metadata properties when sending a message:

  • metadata.MessageId
  • metadata.CorrelationId
  • metadata.SessionId
  • metadata.Label
  • metadata.ReplyTo
  • metadata.PartitionKey
  • metadata.To
  • metadata.ContentType
  • metadata.ScheduledEnqueueTimeUtc
  • metadata.ReplyToSessionId

And here are the read-only metadata properties when receiving a message:

  • metadata.DeliveryCount
  • metadata.LockedUntilUtc
  • metadata.LockToken
  • metadata.EnqueuedTimeUtc
  • metadata.SequenceNumber

Note that all times are populated by the server and are not adjusted for clock skews.

Subscriptions

Credit: youtube.com, Getting Started with Azure Service Bus Topics and Subscriptions in Azure Logic Apps

Subscriptions are a crucial part of Azure Service Bus Topics, allowing you to receive copies of messages sent to the topic.

A subscription is essentially a virtual queue that receives copies of messages sent to the topic. This means that your applications don't access the topic directly, but rather access the subscriptions to get a copy of the message.

To subscribe to a topic, you need to provide properties in the subscription metadata, including requireSessions, sessionIdleTimeoutInSec, and maxConcurrentSessions.

Here are the default values for these properties: PropertyDefault ValuerequireSessionsfalsesessionIdleTimeoutInSec60maxConcurrentSessions8

You can manage subscriptions related to a topic by navigating to the topic page and clicking on Subscriptions in the Entities panel.

A subscription can have multiple message handlers, each performing a different task, such as taking payment for an order or sending a customer an order confirmation email.

In Azure, you can create a topic and subscription using the Azure CLI or the Azure portal. For example, you can create a topic called topic1 and two subscriptions for that topic (subscription1 and subscription2).

Configuring and Upgrading

Credit: youtube.com, Azure Service Bus Topic Follow Along - Azure Developer Associate AZ-204

To configure and upgrade your Azure Service Bus Topic, you'll first need to upgrade your pricing tier. This can be done by heading to the resource details of the Queue and clicking on the Pricing Tier section in the Azure Portal.

Select the Standard tier and save your changes. This will allow you to use Topics, which are similar to Queues in that they enable applications to send and receive messages.

Here are the pricing tiers available for comparison:

Once you've upgraded your pricing tier, you can configure the Service Bus connector with the mandatory configuration properties. This includes tweaking the configuration to your requirements, as outlined in the full config example.

Metadata

Metadata plays a crucial role in Azure Service Bus, and understanding how to work with it is essential for efficient configuration and upgrades.

To set Azure Service Bus metadata when sending a message, you can set query parameters on the HTTP request or the gRPC metadata. This includes properties like metadata.MessageId, metadata.CorrelationId, and metadata.SessionId.

Credit: youtube.com, Develop for Changing Requirements With Custom Metadata Types (1)

When sending a message with metadata, it's essential to note that the metadata.MessageId property does not set the id property of the cloud event returned by Dapr and should be treated in isolation.

Here are some settable metadata properties you can use when sending a message:

When receiving a message with metadata, Dapr attaches Azure Service Bus message metadata to the request using either HTTP headers or gRPC metadata. This includes read-only properties like metadata.DeliveryCount, metadata.LockedUntilUtc, and metadata.LockToken.

Note that all times are populated by the server and are not adjusted for clock skews.

Microsoft Entra ID

Microsoft Entra ID provides a secure way to authenticate with Azure Service Bus Topics pubsub component.

The Azure Service Bus Topics pubsub component supports authentication using all Microsoft Entra ID mechanisms.

You can use Managed Identities with Microsoft Entra ID for authentication.

For more information on how to set up authentication with Microsoft Entra ID, see the docs for authenticating to Azure.

Pricing Tier Upgrade

Credit: youtube.com, 10.AZ-220-Configure IoT Hub pricing,tier and scaling

To upgrade your pricing tier, you'll need to upgrade your subscription to Standard or Premium. This is necessary for using Topics.

The Basic tier is the most affordable option, but it only supports Queues. Upgrading to the Standard tier adds support for both Queues and Topics.

Here are the pricing tiers in more detail:

  • Basic: only supports Queues, with a price that depends on how many messages you send.
  • Standard: supports both Queues and Topics, and is the minimum requirement for using Topics.
  • Premium: is a more expensive option that supports both Queues and Topics, and is zone-redundant.

To upgrade to the Standard tier, simply head to the Pricing Tier section of your Queue's resource details in Azure Portal, select the Standard tier, and save your changes.

Full Config Example

The full config example is a comprehensive setup that includes all mandatory configuration properties for the Service Bus connector.

The example lists all the necessary configuration properties, which you can then tweak to suit your specific requirements.

Please note that there are also optional parameters available, but they are not included in the full config example.

You can find a reference to the optional parameters in the article, where it's linked for your convenience.

Feel free to use the full config example as a starting point and customize it to fit your needs.

Frequently Asked Questions

What is the difference between Azure Service Bus topic and event grid?

Azure Service Bus Topics and Azure Event Grid are both Pub/Sub models, but they differ in their messaging approach: Service Bus Topics uses a pull model, while Event Grid uses a push model. This difference affects how messages are delivered and processed in each system.

Cora Stoltenberg

Junior Writer

Cora Stoltenberg is a skilled writer with a passion for crafting engaging content on a wide range of topics. Her expertise spans various categories, including Search Engine Optimization (SEO) Strategies, where she provides actionable tips and insights to help businesses improve their online presence. With a keen eye for detail and a knack for simplifying complex concepts, Cora's writing is both informative and accessible to readers of all levels.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.