Bird
Raised Fist0
Azurecloud~10 mins

Service Bus topics and subscriptions in Azure - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Service Bus topics and subscriptions
Send message to Topic
↓
Topic receives message
↓
Distribute message to Subscriptions
↓
Each Subscription stores message
↓
Subscribers receive messages from their Subscription
↓
Message processed and removed from Subscription
Messages are sent to a topic, which then distributes copies to each subscription. Subscribers read messages from their subscriptions independently.
Execution Sample
Azure
Create Topic 'orders'
Create Subscription 'priority'
Send message 'Order1' to Topic
Subscription 'priority' receives 'Order1'
Subscriber reads 'Order1' from 'priority'
This sequence shows sending a message to a topic and a subscription receiving and processing it.
Process Table
StepActionTopic StateSubscription 'priority' StateSubscriber Output
1Create Topic 'orders'Topic 'orders' created, emptySubscription 'priority' does not existNo output
2Create Subscription 'priority'Topic 'orders' existsSubscription 'priority' created, emptyNo output
3Send message 'Order1' to TopicTopic 'orders' has message 'Order1'Subscription 'priority' emptyNo output
4Topic distributes 'Order1' to subscriptionsTopic 'orders' has message 'Order1'Subscription 'priority' has message 'Order1'No output
5Subscriber reads message from 'priority'Topic 'orders' has message 'Order1'Subscription 'priority' empty after readSubscriber received 'Order1'
6Message processed and removedTopic 'orders' has message 'Order1'Subscription 'priority' emptySubscriber processed 'Order1'
💡 Message processed and removed from subscription; subscriber output complete.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Topic 'orders' messagesnonenone'Order1''Order1''Order1''Order1'
Subscription 'priority' messagesnonenonenone'Order1'nonenone
Subscriber outputnonenonenonenone'Order1''Order1 processed'
Key Moments - 2 Insights
Why does the topic still have the message after the subscriber reads it from the subscription?
The topic keeps the original message until it expires or is removed; subscriptions hold copies. See execution_table step 5 where subscription is empty but topic still has the message.
Can multiple subscriptions receive the same message from a topic?
Yes, each subscription gets its own copy of the message from the topic. This is shown in the concept flow where the topic distributes messages to all subscriptions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. What is the state of the subscription 'priority'?
AHas message 'Order1'
BSubscription does not exist
CEmpty, no messages
DSubscriber has read the message
💡 Hint
Check the 'Subscription 'priority' State' column at step 4 in the execution_table.
At which step does the subscriber receive the message 'Order1'?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Subscriber Output' column in the execution_table to find when the message appears.
If a second subscription is added, how would the topic distribute messages?
AOnly to the first subscription
BTo both subscriptions independently
CMessages are split between subscriptions
DMessages are sent only once to the topic
💡 Hint
Refer to the concept_flow where the topic distributes messages to all subscriptions.
Concept Snapshot
Service Bus topics receive messages from senders.
Each subscription gets a copy of every message.
Subscribers read messages independently from their subscriptions.
Messages remain in topic until expired or removed.
Subscriptions act like message inboxes for subscribers.
Full Transcript
Service Bus topics act like message hubs. When a message is sent to a topic, it keeps the message and copies it to each subscription. Each subscription stores its own copy. Subscribers read messages from their subscriptions, not directly from the topic. This allows multiple subscribers to get the same message independently. Messages stay in the topic until they expire or are deleted. Subscriptions act like inboxes holding messages for subscribers to process.

Practice

(1/5)
1. What is the main purpose of an Azure Service Bus topic?
easy
A. To send messages to multiple receivers using subscriptions
B. To store data permanently like a database
C. To host web applications
D. To manage virtual machines

Solution

  1. Step 1: Understand the role of a Service Bus topic

    A Service Bus topic acts as a message distributor that sends messages to multiple receivers.
  2. Step 2: Compare other options

    Options B, C, and D describe unrelated Azure services or functions.
  3. Final Answer:

    To send messages to multiple receivers using subscriptions -> Option A
  4. Quick Check:

    Service Bus topic = message distribution [OK]
Hint: Topics distribute messages to many receivers via subscriptions [OK]
Common Mistakes:
  • Confusing topics with queues
  • Thinking topics store data permanently
  • Mixing topics with compute services
2. Which of the following is the correct way to create a subscription under a topic using Azure CLI?
easy
A. az servicebus topic create --resource-group MyGroup --namespace-name MyNamespace --subscription-name MySubscription --name MyTopic
B. az servicebus subscription create --resource-group MyGroup --namespace-name MyNamespace --topic-name MyTopic --name MySubscription
C. az servicebus topic subscription create --resource-group MyGroup --namespace-name MyNamespace --topic-name MyTopic --name MySubscription
D. az servicebus create subscription --resource-group MyGroup --namespace-name MyNamespace --topic MyTopic --subscription MySubscription

Solution

  1. Step 1: Identify the correct Azure CLI command structure

    The correct command to create a subscription under a topic is 'az servicebus subscription create' with required parameters.
  2. Step 2: Check parameters and order

    az servicebus subscription create --resource-group MyGroup --namespace-name MyNamespace --topic-name MyTopic --name MySubscription uses the correct command and parameters: resource group, namespace, topic name, and subscription name.
  3. Final Answer:

    az servicebus subscription create --resource-group MyGroup --namespace-name MyNamespace --topic-name MyTopic --name MySubscription -> Option B
  4. Quick Check:

    Correct CLI syntax = az servicebus subscription create --resource-group MyGroup --namespace-name MyNamespace --topic-name MyTopic --name MySubscription [OK]
Hint: Use 'az servicebus subscription create' to add subscriptions [OK]
Common Mistakes:
  • Using 'az servicebus topic create' for subscriptions
  • Using invalid 'az servicebus topic subscription create'
  • Incorrect command verbs or missing flags
3. Given the following code snippet, what will be the output?
from azure.servicebus import ServiceBusClient
connection_str = "Endpoint=sb://example.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=key"
topic_name = "mytopic"
subscription_name = "mysubscription"

with ServiceBusClient.from_connection_string(connection_str) as client:
    receiver = client.get_subscription_receiver(topic_name, subscription_name)
    with receiver:
        messages = receiver.receive_messages(max_message_count=2, max_wait_time=5)
        print(len(list(messages)))
medium
A. 2
B. The code will not compile
C. An exception is raised due to missing message processing
D. 0

Solution

  1. Step 1: Understand message receiving behavior

    The code tries to receive up to 2 messages with a 5-second wait. If no messages are available, it returns an empty list.
  2. Step 2: Analyze message processing

    Since no messages are explicitly sent before receiving, the list will be empty, so len(messages) is 0.
  3. Final Answer:

    0 -> Option D
  4. Quick Check:

    Receive without sent messages = 0 [OK]
Hint: Receiving messages without sending returns empty list [OK]
Common Mistakes:
  • Assuming messages exist without sending
  • Expecting exceptions for no messages
  • Confusing receive_messages with peek_messages
4. You have created a subscription but messages are not being received. Which of the following is the most likely cause?
medium
A. The subscription filter is set to exclude all messages
B. The topic does not exist
C. The subscription name is too long
D. The Service Bus namespace is in a different region

Solution

  1. Step 1: Check subscription filters

    If a subscription filter excludes all messages, no messages will be received even if the topic and subscription exist.
  2. Step 2: Evaluate other options

    The topic does not exist would prevent subscription creation; The subscription name is too long would cause an error; The Service Bus namespace is in a different region does not block message reception.
  3. Final Answer:

    The subscription filter is set to exclude all messages -> Option A
  4. Quick Check:

    Filter excluding messages = no messages received [OK]
Hint: Check subscription filters if no messages arrive [OK]
Common Mistakes:
  • Ignoring filters when troubleshooting
  • Assuming region mismatch blocks messages
  • Overlooking subscription existence
5. You want to ensure high availability and ordered message processing in your Service Bus topic subscriptions. Which configuration should you apply?
hard
A. Use multiple topics instead of subscriptions
B. Disable partitioning and use duplicate detection only
C. Enable partitioning on the topic and use sessions on subscriptions
D. Enable auto-delete on subscriptions after 1 hour

Solution

  1. Step 1: Understand partitioning and sessions

    Partitioning improves availability by spreading messages across nodes. Sessions enable ordered processing by grouping related messages.
  2. Step 2: Evaluate other options

    Disabling partitioning reduces availability; multiple topics don't guarantee order; auto-delete removes subscriptions prematurely.
  3. Final Answer:

    Enable partitioning on the topic and use sessions on subscriptions -> Option C
  4. Quick Check:

    Partitioning + sessions = high availability + order [OK]
Hint: Use partitioning for availability and sessions for order [OK]
Common Mistakes:
  • Confusing duplicate detection with ordering
  • Thinking multiple topics replace subscriptions
  • Setting auto-delete too aggressively