0
0
Azurecloud~5 mins

Service Bus topics and subscriptions in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, many parts of an app need to get messages separately. Service Bus topics and subscriptions let you send one message and have many parts receive it independently. This helps keep messages organized and shared safely.
When you want to send one message to many different parts of your app without mixing them up.
When different teams or services need to get only certain messages from a shared source.
When you want to keep messages safe and make sure no one misses important info.
When you want to separate message handling by topics so each subscriber gets only what they want.
When you want to scale message processing by adding more subscribers without changing the sender.
Config File - topic-subscription.json
topic-subscription.json
{
  "type": "Microsoft.ServiceBus/namespaces/topics",
  "apiVersion": "2021-06-01-preview",
  "name": "example-namespace/example-topic",
  "location": "eastus",
  "properties": {
    "defaultMessageTimeToLive": "P14D",
    "maxSizeInMegabytes": 1024
  }
}

{
  "type": "Microsoft.ServiceBus/namespaces/topics/subscriptions",
  "apiVersion": "2021-06-01-preview",
  "name": "example-namespace/example-topic/example-subscription",
  "properties": {
    "lockDuration": "PT1M",
    "maxDeliveryCount": 10,
    "defaultMessageTimeToLive": "P7D"
  }
}

The first JSON block creates a Service Bus topic named example-topic inside the namespace example-namespace. It sets the message time to live to 14 days and max size to 1 GB.

The second JSON block creates a subscription named example-subscription under the topic. It sets how long a message is locked during processing (1 minute), max delivery attempts (10), and message time to live (7 days).

Commands
Create a Service Bus namespace where topics and subscriptions will live. The Standard SKU supports topics and subscriptions.
Terminal
az servicebus namespace create --resource-group example-rg --name example-namespace --location eastus --sku Standard
Expected OutputExpected
{ "createdAt": "2024-06-01T12:00:00Z", "location": "eastus", "name": "example-namespace", "provisioningState": "Succeeded", "resourceGroup": "example-rg", "sku": { "name": "Standard" }, "type": "Microsoft.ServiceBus/namespaces" }
--sku - Sets the pricing tier; Standard is needed for topics and subscriptions
Create a topic named example-topic in the namespace. This topic will hold messages for subscribers.
Terminal
az servicebus topic create --resource-group example-rg --namespace-name example-namespace --name example-topic --max-size 1024 --default-message-time-to-live 14
Expected OutputExpected
{ "name": "example-topic", "maxSizeInMegabytes": 1024, "defaultMessageTimeToLive": "P14D", "status": "Active" }
--max-size - Sets max size of the topic storage in MB
--default-message-time-to-live - Sets how long messages live before expiring
Create a subscription named example-subscription under the topic. This subscription will receive messages sent to the topic.
Terminal
az servicebus topic subscription create --resource-group example-rg --namespace-name example-namespace --topic-name example-topic --name example-subscription --lock-duration PT1M --max-delivery-count 10 --default-message-time-to-live 7
Expected OutputExpected
{ "name": "example-subscription", "lockDuration": "PT1M", "maxDeliveryCount": 10, "defaultMessageTimeToLive": "P7D", "status": "Active" }
--lock-duration - Sets how long a message is locked during processing
--max-delivery-count - Sets how many times a message can be delivered before dead-lettering
Check the details of the subscription to confirm it was created correctly.
Terminal
az servicebus topic subscription show --resource-group example-rg --namespace-name example-namespace --topic-name example-topic --name example-subscription
Expected OutputExpected
{ "name": "example-subscription", "lockDuration": "PT1M", "maxDeliveryCount": 10, "defaultMessageTimeToLive": "P7D", "status": "Active" }
Key Concept

If you remember nothing else from this pattern, remember: topics let you send one message to many independent subscribers who each get their own copy.

Common Mistakes
Trying to create a subscription before the topic exists
Subscriptions must belong to an existing topic, so creation fails if the topic is missing
Always create the topic first, then create subscriptions under it
Using Basic SKU namespace which does not support topics and subscriptions
Basic SKU only supports queues, so topic commands will fail
Use Standard or Premium SKU for namespaces that need topics and subscriptions
Setting lock duration too short causing message processing to fail
If lock duration is too short, messages may be delivered multiple times before processing finishes
Set lock duration long enough for your processing to complete, like 1 minute or more
Summary
Create a Service Bus namespace with Standard SKU to support topics and subscriptions.
Create a topic to hold messages that multiple subscribers can receive.
Create subscriptions under the topic to receive copies of messages independently.
Use commands to verify the topic and subscription are active and configured correctly.