Pub/Sub Architecture: An In-Depth Overview with Example

Arindam Das
4 min readJun 1, 2024

--

From Microsoft Learn

Publish/Subscribe (Pub/Sub) architecture is a messaging pattern where senders (publishers) do not send messages directly to specific receivers (subscribers). Instead, messages are published to a channel (topic), and subscribers receive only the messages they are interested in, based on the topics they subscribe to. This decouples the senders from the receivers, allowing for a more scalable and flexible communication system.

Key Components

  1. Publisher: The component that sends messages to a topic.
  2. Subscriber: The component that receives messages from a topic.
  3. Topic: A logical channel to which publishers send messages. Subscribers can subscribe to one or more topics to receive relevant messages.
  4. Message Broker: The intermediary that manages topics and ensures messages are delivered from publishers to subscribers. In Azure, this role is often fulfilled by Azure Service Bus or Azure Event Grid.

Benefits of Pub/Sub Architecture

Scalability: Easily handles an increasing number of messages and subscribers.

Flexibility: Allows new subscribers to be added without altering the publishers.

Decoupling: Publishers and subscribers operate independently, enhancing system modularity.

Real-Time Communication: Enables real-time message delivery and processing.

Pub/Sub with Azure Services

Azure offers several services to implement a Pub/Sub architecture, including Azure Service Bus, Azure Event Grid, and Azure Event Hubs. For this example, we’ll focus on Azure Service Bus.

Example: Implementing Pub/Sub with Azure Service Bus

Scenario: Let’s consider a scenario where we have a system that tracks orders in an e-commerce platform. When an order is placed, an order service publishes an event to a topic. Different services like inventory management, shipping, and notification systems subscribe to this topic to process the order accordingly.

Step-by-Step Implementation

Step 1: Create an Azure Service Bus Namespace

First, we need to create a Service Bus namespace in the Azure portal.

  • Navigate to the Azure portal.
  • Click on “Create a resource” and select “Service Bus”.
  • Fill in the necessary details (e.g., subscription, resource group, namespace name, location) and create the namespace.

Step 2: Create a Topic

Within the Service Bus namespace, create a topic that will act as the message channel.

  • In the Service Bus namespace, click on “Topics” and then “Add”.
  • Provide a name for the topic (e.g., “order-events”) and create it.

Step 3: Create Subscriptions

Create subscriptions under the topic for each service that needs to process the order events.

  • Go to the created topic (“order-events”).
  • Click on “Subscriptions” and then “Add”.
  • Create subscriptions for each service (e.g., “inventory-subscription”, “shipping-subscription”, “notification-subscription”).

Step 4: Publish Messages to the Topic

The order service will publish messages to the “order-events” topic. This can be done using Azure SDKs in various programming languages. Here’s an example using C#:

using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;

class Program
{
const string connectionString = "<Your-Service-Bus-Connection-String>";
const string topicName = "order-events";

static async Task Main(string[] args)
{
await using var client = new ServiceBusClient(connectionString);
ServiceBusSender sender = client.CreateSender(topicName);

string orderMessage = "Order ID: 12345, Product: Widget, Quantity: 10";
ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes(orderMessage));

await sender.SendMessageAsync(message);
Console.WriteLine("Order event published.");
}
}

Step 5: Receive Messages from the Subscriptions

Each subscribing service will receive messages from its respective subscription. Here’s an example for the inventory service using C#:

using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;

class Program
{
const string connectionString = "<Your-Service-Bus-Connection-String>";
const string subscriptionName = "inventory-subscription";
const string topicName = "order-events";

static async Task Main(string[] args)
{
await using var client = new ServiceBusClient(connectionString);
ServiceBusProcessor processor = client.CreateProcessor(topicName, subscriptionName);

processor.ProcessMessageAsync += MessageHandler;
processor.ProcessErrorAsync += ErrorHandler;

await processor.StartProcessingAsync();
Console.WriteLine("Receiving messages...");

Console.ReadKey();
await processor.StopProcessingAsync();
}

static async Task MessageHandler(ProcessMessageEventArgs args)
{
string body = Encoding.UTF8.GetString(args.Message.Body);
Console.WriteLine($"Received message: {body}");

await args.CompleteMessageAsync(args.Message);
}

static Task ErrorHandler(ProcessErrorEventArgs args)
{
Console.WriteLine($"Error processing message: {args.Exception}");
return Task.CompletedTask;
}
}

Advanced Features and Best Practices

Message Filtering and Forwarding:

  • Use SQL-like filter rules to determine which messages a subscription receives.
  • Enable message forwarding to chain topics and subscriptions for more complex scenarios.

Dead-letter Queues (DLQs):

  • Use DLQs to handle messages that cannot be delivered or processed.
  • Implement retry policies and error handling strategies to manage dead-lettered messages effectively.

Session Management:

  • Utilize sessions for ordered message processing.
  • Implement session state to maintain context across a series of related messages.

Auto-scaling and Load Balancing:

  • Take advantage of Azure Service Bus’s ability to auto-scale and distribute messages across multiple processors.

Security and Access Control:

  • Secure your Service Bus namespace using Shared Access Signatures (SAS) and Azure Active Directory (AAD) for authentication and authorization.
  • Implement role-based access control (RBAC) to manage permissions effectively.

Monitoring and Diagnostics:

  • Use Azure Monitor and Azure Service Bus metrics to track message flow, latency, and throughput.
  • Set up alerts to detect and respond to issues promptly.

Conclusion

Pub/Sub architecture is a powerful pattern for building scalable and decoupled systems. Azure Service Bus provides a robust platform to implement Pub/Sub, facilitating real-time communication and processing across various services. By leveraging topics and subscriptions, we can ensure that our system components are loosely coupled yet efficiently coordinated, enabling us to build resilient and maintainable applications.

Through careful implementation of advanced features and best practices, Azure Service Bus can be fine-tuned to meet the specific needs of your application, providing a flexible and reliable messaging backbone for your distributed systems.

--

--

Arindam Das
Arindam Das

No responses yet