0
0
AzureComparisonBeginner · 4 min read

Azure Service Bus Queue vs Topic: Key Differences and Usage

An Azure Service Bus Queue delivers messages to a single consumer in a first-in, first-out order, ideal for point-to-point communication. A Service Bus Topic supports publish-subscribe messaging, allowing multiple subscribers to receive copies of each message independently.
⚖️

Quick Comparison

This table summarizes the main differences between Azure Service Bus Queue and Topic.

FeatureService Bus QueueService Bus Topic
Message PatternPoint-to-pointPublish-subscribe
Number of ConsumersSingle consumer per messageMultiple subscribers receive copies
Message DeliveryEach message processed onceEach subscriber gets a copy
Use CaseLoad balancing tasksBroadcasting events
Subscription ManagementNot applicableSupports multiple subscriptions with filters
OrderingFIFO supportedOrdering per subscription
⚖️

Key Differences

Service Bus Queues are designed for simple point-to-point communication where one sender sends messages and one receiver processes them. This ensures that each message is received and processed by only one consumer, making it ideal for load balancing and task distribution scenarios.

In contrast, Service Bus Topics enable a publish-subscribe pattern. A single message sent to a topic can be received by multiple independent subscribers. Each subscriber has its own subscription, which can filter messages, allowing different parts of an application to react to the same event differently.

Queues guarantee message ordering and exactly-once delivery to one consumer, while topics provide flexibility with multiple subscriptions and message filtering but still maintain reliable delivery per subscription. Topics are best when you want to broadcast messages to multiple receivers simultaneously.

⚖️

Code Comparison

Here is a simple example of sending and receiving a message using an Azure Service Bus Queue in C#.

csharp
using Azure.Messaging.ServiceBus;

string connectionString = "<your_connection_string>";
string queueName = "myqueue";

// Create a client
await using var client = new ServiceBusClient(connectionString);

// Create a sender
ServiceBusSender sender = client.CreateSender(queueName);

// Send a message
await sender.SendMessageAsync(new ServiceBusMessage("Hello Queue!"));

// Create a receiver
ServiceBusReceiver receiver = client.CreateReceiver(queueName);

// Receive a message
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

Console.WriteLine($"Received message: {receivedMessage.Body.ToString()}");

// Complete the message
await receiver.CompleteMessageAsync(receivedMessage);
Output
Received message: Hello Queue!
↔️

Topic Equivalent

This example shows sending a message to a Service Bus Topic and receiving it from a subscription in C#.

csharp
using Azure.Messaging.ServiceBus;

string connectionString = "<your_connection_string>";
string topicName = "mytopic";
string subscriptionName = "mysubscription";

// Create a client
await using var client = new ServiceBusClient(connectionString);

// Create a sender for the topic
ServiceBusSender sender = client.CreateSender(topicName);

// Send a message
await sender.SendMessageAsync(new ServiceBusMessage("Hello Topic!"));

// Create a receiver for the subscription
ServiceBusReceiver receiver = client.CreateReceiver(topicName, subscriptionName);

// Receive a message
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

Console.WriteLine($"Received message: {receivedMessage.Body.ToString()}");

// Complete the message
await receiver.CompleteMessageAsync(receivedMessage);
Output
Received message: Hello Topic!
🎯

When to Use Which

Choose Service Bus Queue when you need to distribute work evenly among multiple workers, ensuring each message is processed once. This is perfect for task scheduling, order processing, or any scenario where a message should be handled by only one consumer.

Choose Service Bus Topic when you want to broadcast messages to multiple independent receivers, such as event distribution, notifications, or when different parts of your system need to react to the same message differently. Topics allow filtering and multiple subscriptions, making them flexible for complex messaging patterns.

Key Takeaways

Use Service Bus Queue for point-to-point messaging with single consumer processing each message.
Use Service Bus Topic for publish-subscribe scenarios with multiple subscribers receiving copies.
Queues ensure message ordering and exactly-once delivery to one consumer.
Topics support multiple subscriptions with filtering for flexible message distribution.
Choose based on whether you need load balancing (Queue) or broadcasting (Topic).