Azure PubSub Service is a powerful tool for scalable publish-subscribe messaging. It allows for efficient communication between applications and services.
With Azure PubSub Service, you can handle a large volume of messages and scale your messaging system as needed. This makes it an ideal choice for real-time data processing and event-driven architectures.
One key benefit of Azure PubSub Service is its ability to handle high-throughput and high-latency workloads. This is because it uses a distributed architecture that can scale horizontally to meet changing demands.
By using Azure PubSub Service, you can simplify your messaging system and reduce the complexity of your architecture. This can lead to significant cost savings and improved maintainability.
Architecture and Components
At the heart of Azure PubSub lies a robust architecture composed of key components that enable efficient message broadcasting and subscription management.
Topics are the core of this architecture, allowing you to broadcast messages to multiple subscribers.
Each service subscribes to relevant topics to receive messages, establishing a connection between senders and receivers.
Filters are applied to subscriptions to receive only relevant messages, ensuring that services only process messages that are of interest to them.
Here's a breakdown of the main components:
- Topics: Used to broadcast messages to multiple subscribers.
- Subscriptions: Each service subscribes to relevant topics to receive messages.
- Filters: Applied to subscriptions to receive only relevant messages.
Components
Components are the building blocks of a pub/sub system, and they're actually pretty straightforward.
There are three main components: Topics, Subscriptions, and Filters. Topics are used to broadcast messages to multiple subscribers, which is a pretty cool way to get information out to a lot of people at once.
Subscriptions are what allow services to receive messages from topics. Each service subscribes to relevant topics to get the information it needs.
Filters are used to narrow down the messages received from subscriptions. They're applied to subscriptions to receive only relevant messages, which helps reduce noise and focus on what's important.
Here's a breakdown of the components:
- Topics: Used to broadcast messages to multiple subscribers.
- Subscriptions: Each service subscribes to relevant topics to receive messages.
- Filters: Applied to subscriptions to receive only relevant messages.
Implementation
To implement the Azure Service Bus architecture, you'll need to set up a namespace and a topic. In the Azure portal, create a Service Bus namespace and then create a topic, such as orderTopic.
You'll also need to create separate services for each component of the architecture: Order Service, Payment Service, Inventory Service, and Shipping Service.
To set up subscriptions and filters, you'll need to create subscriptions for each service and apply filters to receive only the relevant messages. For example, the Payment Service should subscribe to the orderTopic with a filter for payment-related messages, such as orderCreated messages with a type of 'orderCreated'.
Here's a breakdown of the services and their corresponding subscriptions and filters:
These subscriptions and filters will ensure that each service only receives the messages it needs to process, reducing unnecessary complexity and improving overall system efficiency.
Publish-Subscribe Messaging
Publish-subscribe messaging is a powerful way to send messages in Azure, where one sender can send messages to many receivers at the same time. This is achieved through a message bus, which acts as a middleman between the publisher and the subscriber.
To get started, you'll need to create a Service Bus namespace and a topic in the Azure portal. This will allow you to send messages to a specific category, known as a topic.
The publisher sends a message to a topic on the message bus, and the message bus distributes it to all the subscribers who are listening to that topic. Each subscriber receives the message independently of the others.
Here's a breakdown of the flow:
- Publisher: The sender that creates and sends messages.
- Message Bus: The system that delivers the messages.
- Subscriber: The receiver that gets the messages.
- Topics: The specific category that messages are sent to.
The publisher doesn't need to know who will receive the messages, making it a scalable and flexible way to communicate between different parts of your system.
Configuring and Managing
Configuring and managing your Azure PubSub service is a breeze, thanks to its intuitive features. You can set metadata for bulk publish operations by setting query parameters on the HTTP request or the gRPC metadata.
To configure bulk subscribe options, you can set the maxMessagesCount to 100 and maxAwaitDurationMs to 10000. This will allow you to subscribe to a topic and receive messages in bulk.
You can also configure the checkpointing frequency in a partition by setting the metadata in the HTTP or gRPC subscribe request. This will enable checkpointing after the configured number of events within a partition event sequence. The default checkpoint frequency per partition is 1, but you can disable it by setting the frequency to 0.
Quick Metrics Evaluation
To quickly evaluate the performance of your Azure Web PubSub service, you can check the Server Load metric on the Portal. This metric shows the computing pressure of your service.
Server Load is a key indicator of performance, and if it's below 70%, latency inside the Azure Web PubSub service will remain low. The metric is a valuable tool for monitoring and optimizing your service.
If you're using a unit 50 or larger and sending messages to small groups (group size <20), you should also check the sending to small group metric for reference. This is because there's a large routing cost in these scenarios that's not included in the Server Load metric.
Configuring and Managing
Configuring and Managing Dead-Lettering in Azure Service Bus involves setting the MaxDeliveryCount property to control how many times a message can be retried before it's dead-lettered.
This property is set when the messaging entity is created, and its value determines how many attempts a message has to be processed successfully before being moved to the dead-letter queue.
Each message is processed individually, and if it fails to process and reaches the max delivery count, it's moved to the dead-letter queue one by one.
Messages are dead-lettered as soon as they reach the max delivery count, rather than waiting to move multiple messages to the dead-letter queue at once.
To manage dead-lettering, you should understand the different scenarios where it's triggered, including processing failure, poison messages, and expired messages.
Here are some common scenarios where dead-lettering is used:
In Azure Service Bus, dead-lettering is implemented using two main patterns: the dead-letter channel and the invalid message channel.
Each queue and subscription has its own dead-letter sub-queue, which is used to store messages that cannot be processed.
Configuring Bulk Subscribe
Bulk subscribe is an important feature in Azure Eventhubs that allows you to receive multiple messages in a single operation. This can be a game-changer for applications that require high throughput and low latency.
To configure bulk subscribe, you can set the `maxMessagesCount` and `maxAwaitDurationMs` options. The default value for `maxMessagesCount` is 100, which means you can receive up to 100 messages in a single operation. The default value for `maxAwaitDurationMs` is 10,000, which means you can wait up to 10 seconds for new messages to arrive.
Here's a summary of the default values for these options:
By adjusting these values, you can optimize your bulk subscribe configuration for your specific use case. For example, if you need to receive a large number of messages, you can increase the `maxMessagesCount` value. If you need to receive messages in real-time, you can decrease the `maxAwaitDurationMs` value.
Choosing Unit Size
Choosing the right unit size for your Azure Web PubSub service is crucial for a smooth user experience. You can evaluate the inbound/outbound capacity or find which unit size is suitable for a specific use case by checking the maximum inbound and outbound bandwidth for each unit size.
The maximum inbound bandwidth is always 60 KBps, regardless of the unit size. Inbound messages per second also remain constant at 30, regardless of the unit size.
To determine the outbound bandwidth, consider the number of connections sending the message, the size of a single message, and the interval for sending messages. For example, if you're sending 3,000 messages per second, the outbound bandwidth will be 6 MBps.
Here's a table to help you choose the right unit size based on your outbound bandwidth needs:
For large groups, the outbound bandwidth becomes the bottleneck before hitting the routing cost limit. If you need more inbound bandwidth, you'll need to scale up to use Premium_P2 (unit > 100).
Microsoft Entra ID
Microsoft Entra ID is a robust authentication mechanism supported by Azure Event Hubs pub/sub component.
You can use all Microsoft Entra ID mechanisms for authentication, including Azure Active Directory, Azure Active Directory B2C, and more.
For further information on the relevant component metadata fields, check the docs for authenticating to Azure.
The Azure Event Hubs pub/sub component requires specific metadata fields depending on the chosen Microsoft Entra ID authentication mechanism.
To ensure seamless integration, consult the documentation for the chosen authentication mechanism.
Scenarios and Use Cases
Azure PubSub is a powerful tool for real-time communication between applications and services. It's designed to handle a large number of messages and consumers.
In terms of scenarios, there are three typical use cases for Azure PubSub: sending messages to groups through Web PubSub subprotocol, triggering CloudEvent, and calling REST APIs. Each scenario has its own inbound and outbound capacity, which can be affected by factors such as group member and group count.
Here are some key use cases for Azure PubSub:
- Debugging: Monitor and debug the communication between services.
- Auditing: Keep a record of all messages for audit purposes.
- Analytics: Analyze the messages for patterns or insights.
The default message size for Azure PubSub is 2,048 bytes, and the message send interval is 1 second. This can be useful for applications that need to broadcast information to a significant number of consumers.
E-commerce Application with Microservices
In an e-commerce system, different microservices need to communicate asynchronously. This can be achieved using a publish-subscribe (pub-sub) pattern with topics and subscriptions.
The Order Service publishes an OrderCreated message to the orderTopic, which is then received by the Payment Service, Inventory Service, and Shipping Service. Each of these services performs a specific action based on the message they receive.
The Payment Service processes the payment and publishes a PaymentProcessed message back to the orderTopic. The Shipping Service starts the shipping process when it receives a PaymentProcessed message and publishes an OrderShipped message back to the orderTopic.
Here are the steps involved in the workflow:
- Order Placement: The customer places an order through the Order Service, which publishes an OrderCreated message to the orderTopic.
- Payment Processing: The Payment Service receives the OrderCreated message, processes the payment, and publishes a PaymentProcessed message back to the orderTopic.
- Inventory Update: The Inventory Service receives the OrderCreated message and updates the inventory.
- Shipping Processing: The Shipping Service receives the PaymentProcessed message, starts the shipping process, and publishes an OrderShipped message back to the orderTopic.
This decoupling of services allows for scalability and resilience in the e-commerce system.
When to Use It
This pattern is perfect for applications that need to broadcast information to a significant number of consumers. In fact, it's designed for scenarios where an application can send information to consumers without requiring real-time responses from the consumers.
An application that needs to communicate with one or more independently developed applications or services, which may use different platforms, programming languages, and communication protocols, is also a great fit. This pattern allows for flexibility and compatibility across different systems.
When to use this pattern, consider the following scenarios:
- An application needs to broadcast information to a significant number of consumers.
- An application needs to communicate with one or more independently developed applications or services, which may use different platforms, programming languages, and communication protocols.
- An application can send information to consumers without requiring real-time responses from the consumers.
- The systems being integrated are designed to support an eventual consistency model for their data.
- An application needs to communicate information to multiple consumers, which may have different availability requirements or uptime schedules than the sender.
However, if an application has only a few consumers who need significantly different information from the producing application, this pattern might not be the best choice. Additionally, if an application requires near real-time interaction with consumers, it's better to consider other options.
Scenarios for Dead-Lettering
Dead-lettering is a crucial aspect of messaging systems, and understanding its scenarios is essential for effective implementation. Dead-lettering occurs when a message fails processing and reaches its maximum delivery count.
Repeated failures due to issues like database connection problems can lead to dead-lettering. This is because the message is retried multiple times before being moved to the dead-letter queue.
Invalid or corrupt message content can also result in dead-lettering. This is known as a "poison message" and can cause problems for the messaging system.
Time-critical messages that become irrelevant after a certain period can also be dead-lettered. This is known as an "expired message" and can help prevent unnecessary processing.
Here are some scenarios for dead-lettering:
Workload Design
When designing your workload, it's essential to consider how the Publisher/Subscriber pattern can help you address the goals and principles covered in the Azure Well-Architected Framework pillars.
The decoupling introduced in this pattern enables independent reliability targets on components and removes direct dependencies, making it easier to address reliability design decisions. This is particularly important for failure mode analysis and background jobs.
This pattern introduces an important security segmentation boundary that enables queue subscribers to be network-isolated from the publisher, which is a key aspect of security design decisions.
By decoupling publishers from consumers, you can optimize the compute and code specifically for the task that the consumer needs to perform for the specific message, making it easier to address performance efficiency goals.
Here's a breakdown of how the Publisher/Subscriber pattern supports the Azure Well-Architected Framework pillars:
As with any design decision, consider any tradeoffs against the goals of the other pillars that might be introduced with this pattern.
Frequently Asked Questions
Is Azure Event Hub a pub sub?
Yes, Azure Event Hub supports pub/sub messaging, allowing for sending and receiving multiple messages in a single operation. It's a scalable and efficient way to handle event-driven architectures.
Sources
- https://learn.microsoft.com/en-us/azure/azure-web-pubsub/concept-performance
- https://www.codecademy.com/catalog/subject/cloud-computing
- https://medium.com/@darshana-edirisinghe/azure-service-bus-part-3-publish-subscribe-messaging-for-microservices-e543c79d4ffd
- https://docs.dapr.io/reference/components-reference/supported-pubsub/setup-azure-eventhubs/
- https://learn.microsoft.com/en-us/azure/architecture/patterns/publisher-subscriber
Featured Images: pexels.com