Azure — Difference between Azure Service Bus Queues and Topics

Ashish Patel
Awesome Azure
Published in
4 min readJul 4, 2021

Comparison — Azure Service Bus Queue vs Topic.

TL;DR:

Queues and Topics are similar when a sender sends messages, but messages are processed differently by a receiver. A queue can have only one consumer, whereas a topic can have multiple subscribers.

Difference between Azure Storage Queue and Service Bus Queue

Azure Service Bus Queue

A queue provides a one-to-one relationship between each message and its consumer. A queue allows processing of a message by a single consumer only.

Azure Service Bus Queue

Queues offer First In, First Out (FIFO) message delivery to one or more competing consumers. That is, receivers typically receive and process messages in the order in which they were added to the queue, which means the first message delivered into the Queue will be the first one to be retrieved by the receiver. And, only one message consumer receives and processes each message.

Azure Service Bus Topic

In contrast to queue, a topic (and subscriptions) provide a one-to-many relationship between each message and consumers. It follows publish and subscribe (pub/sub) pattern which is useful for scaling to large numbers of recipients. Each published message is made available to each subscription registered with the topic.

Azure Service Bus Topic

Publishers send messages to a topic in the same way that they send messages to a queue, but it varies on a slight factor where Topics can have multiple, independent subscriptions. A subscriber to a Topic can receive a copy of each message sent to that Topic.

Consumers don’t receive messages directly from the topic. Instead, consumers receive messages from subscriptions of the topic.

If you do not want all subscribers to read all the messages, then you can apply rules to specify additional actions to filter or modify message properties.

Subscriptions

A topic subscription resembles a virtual queue that receives copies of the messages that are sent to the topic. Consumers receive messages from a subscription identically to the way they receive messages from a queue.

The message-sending functionality of a queue map directly to a topic and its message-receiving functionality maps to a subscription.

Important: Topic itself is not enough, you also need Subscriptions.

Key Differences

Consumer options
Queue: It is possible to have multiple receivers, but each message will be received only by one of those receivers.
Topic: There are multiple receivers (subscribers) to receive the messages, each message copy can be sent to any number of subscribers associated with the Topic.

Message Filtering
Queue: Queue does not essentially require any filters as the messages are being received by only one user.
Topic: Each message that is sent over a Topic can have a collection of properties. Properties are used when a custom filter per subscription is applied.

Topic subscriptions can enable powerful filtering capabilities, such that you can define certain parameters that messages must meet in order to be copied into the subscriptions virtual queue. This can be handy if you need to handle different types of messages or messages with various data structures on the same topic.

Consumer Scalability
Queue: If you need to scale a queue, you’re still limited to having one consumer listening.
Topic: Topics can be more scalable than queues, as more than one consumer can listen to messages. We don’t need to recreate the Topic. Once a new subscription is created, all the new messages that are sent to that Topic will be received by the new subscription also.

Message Auto-forwarding
Queue: Messages can be auto-forwarded to a specified Queue or Topic. But the destination cannot be a Topic Subscription.
Topic: Messages can be auto-forwarded from a Topic to a Queue or Topic. Also, it is possible to forward the messages from a Topic Subscription to a Queue or Topic.

Message Removal
Queue: The first receiver that completes the reading of the message also removes it from the Queue so that the message cannot be processed by other readers.
Topics: The message is removed only after every receiver has processed the message. When the message is read from all the Subscribers, the message is removed from the Topic too.

Message Locking and Deleting — Both queues and topic subscriptions support PeekLock and ReceiveAndDelete modes, so it ensures a message is processed before being dismissed if required.

Use Cases

Choose Service Bus Queues:

  • When there is a need to pass the message in the one-to-one system.

Choose Service Bus Topics:

  • When there is a need to send the message to multiple systems.

Summary

Azure Service Bus Queues and Topics can be used to send and receive messages to implement asynchronous communication and improve durability. A queue is often used for point-to-point communication, whereas a topic is useful in publish/subscribe scenarios.

Download sample source: azure-servicebus-labs

--

--

Ashish Patel
Awesome Azure

Cloud Architect • 4x AWS Certified • 6x Azure Certified • 1x Kubernetes Certified • MCP • .NET • Terraform • DevOps • Blogger [https://bit.ly/iamashishpatel]