Publish Subscribe messaging in Azure is a messaging pattern where senders and receivers are loosely coupled, allowing for greater scalability and flexibility.
This pattern is based on the Observer design pattern, where a publisher sends messages to subscribers who are interested in receiving them.
In Azure, Publish Subscribe messaging is typically implemented using Service Bus or Event Grid.
The publisher doesn't need to know the details of the subscribers, and the subscribers don't need to know the details of the publisher.
Publish/Subscribe Pattern
The publish/subscribe pattern is a messaging platform in the cloud that allows for one-to-many communication. It's useful for scaling to large numbers of recipients and provides a virtual queue that receives copies of messages sent to a topic.
A sender sends a message to a topic and anyone interested can subscribe to the topic to receive the message. Subscribers can apply filters on incoming messages to only receive certain messages that comply with the defined filter.
The Service Bus provides secure messaging and relay capabilities that enable building distributed and loosely-coupled applications in the cloud. It supports multiple messaging protocols and patterns and handles delivery assurance, reliable messaging, and scale for your applications.
A topic can have one or multiple subscribers, and each subscriber can have a filter to restrict the messages they receive. This allows for efficient and reliable delivery of messages to the right recipients.
You can use the Windows Azure Service Bus Explorer to create new subscriptions with filters or add rules to existing subscriptions. This graphical interface can save you time and effort compared to creating subscriptions by code.
The publish/subscribe pattern is not the same as the pub-sub model used in Azure Event Grid. While both provide a way to handle events, they differ in how they work and the benefits they offer. For example, Azure Event Grid uses a push-push model, whereas the Service Bus uses a push-pull model.
Here's a comparison of the two:
The publish/subscribe pattern offers many benefits, including secure messaging, relay capabilities, and support for multiple messaging protocols and patterns. It's a powerful tool for building distributed and loosely-coupled applications in the cloud.
Azure Messaging
Azure Messaging is a powerful tool for building distributed and loosely-coupled applications in the cloud.
The Service Bus provides a reliable and highly scalable way to store messages as they travel between systems without losing messages in the event of connectivity failure. It also supports multiple messaging protocols and patterns, and handles delivery assurance, reliable messaging, and scale for your applications.
You can use Service Bus Queues to store messages, or Service Bus Topics and Subscriptions to implement a publish/subscribe pattern. Topics and subscriptions deliver a highly scalable, flexible, and cost-effective way to publish messages from an application and deliver them to multiple subscribers.
Here are some key features of Azure Messaging:
- Service Bus Queues offer a reliable, highly scalable way to store messages.
- Service Bus Topics and Subscriptions implement a publish/subscribe pattern.
- The Service Bus Relay enables applications to securely call back to private cloud applications.
- Companies can use the Service Bus relay to expose just the information they want from their private cloud environment.
Messaging
Azure's messaging capabilities are built around the idea of decoupling producers and consumers. This means that messages are stored durably in a queue, allowing producers to send messages without waiting for a reply from consumers. A key benefit of using queues is temporal decoupling, which enables producers and consumers to send and receive messages at different rates.
Queues offer First In, First Out (FIFO) message delivery to one or more competing consumers. This means that receivers typically receive and process messages in the order in which they were added to the queue. Only one message consumer receives and processes each message.
Using queues to intermediate between message producers and consumers provides an inherent loose coupling between the components. Because producers and consumers aren't aware of each other, a consumer can be upgraded without having any effect on the producer.
Queues are particularly useful for achieving load-leveling, which enables producers and consumers to send and receive messages at different rates. Intermediating message producers and consumers with a queue means that the consuming application only has to be able to handle average load instead of peak load.
Here are some key benefits of using queues:
- Temporal decoupling of application components
- Load-leveling, enabling producers and consumers to send and receive messages at different rates
- Loose coupling between producers and consumers
- Competing consumer pattern, where multiple consumers can compete to process messages
Connectivity
Connectivity is a crucial aspect of Azure Messaging, and Service Bus Relay is a key component that enables secure and reliable connectivity between applications.
The Service Bus Relay allows applications hosted in Windows Azure to securely call back to private cloud applications behind a firewall, making connectivity faster and more reliable.
It also supports the ability to integrate applications across existing NATs and firewalls, which is a huge advantage for many enterprises.
The relay service supports a variety of transport protocols and Web services standards, including REST, SOAP, and WS-*.
By using the Service Bus Relay, companies can expose only the information they want from their private cloud environment, creating a more secure architecture than opening up a VPN.
This approach also enables enterprises to use a SOA-based architecture and expose just the services they want to deliver from their on-premise data centers.
Here are some key benefits of using the Service Bus Relay:
- Secure and reliable connectivity between applications
- Support for multiple transport protocols and Web services standards
- Ability to expose only the information needed, creating a more secure architecture
- Integration with existing NATs and firewalls
Windows Messaging
Windows Messaging is a powerful tool in Azure's publish/subscribe pattern. The Windows Azure Service Bus allows messaging using this pattern, which enables building disconnected and reliable systems.
The sender sends a message to a topic and anyone interested can subscribe to receive it. They can apply filters on the incoming messages to only receive certain messages that comply to the defined filter.
The Service Bus provides secure messaging and relay capabilities that enable building distributed and loosely-coupled applications in the cloud. It supports multiple messaging protocols and patterns.
There are many practical uses for the Service Bus, including creating a service bus namespace. After creation, the screen should look like this.
Security and Delivery
Security is a top priority in Azure's publish-subscribe model, and Event Grid has several layers of protection in place. The first line of defense is Role-based Access Control (RBAC) on the Event Grid resource, which requires the creator of a new subscription to have Microsoft.EventGrid/EventSubscriptions/Write permissions.
Event Grid also validates WebHooks, the primary mechanism for listening to events, by sending a validation token to the client. The client must respond with the validation code in a 200-OK response to complete the validation process.
Guaranteed delivery of events is another key aspect of Event Grid, with a default behavior of trying at least once to deliver the event and retrying using exponential backoff. If the Event Handler acknowledges the request with an HTTP 200 OK or HTTP 202 Accepted, the backoff mechanism will stop.
Security
The security of Event Grid is built on several key components. One of them is Role-based Access Control (RBAC) on the Event Grid resource, which requires the creator of a new subscription to have Microsoft.EventGrid/EventSubscriptions/Write permissions.
To validate WebHooks, Event Grid sends a token to the client, which must respond with the validation code in a 200-OK response. This ensures that only authorized clients can receive events.
Event Grid also requires validation for custom code subscriptions, where the client needs to respond to the validation token sent by Event Grid. This adds an extra layer of security to the process.
To publish custom events to a topic, you can use SAS tokens or key authentication. This provides an additional security mechanism for controlling who can send events to a topic.
Event Delivery
Event delivery is a crucial aspect of Event Grid, and it's designed to ensure that events are delivered reliably.
Event Grid will try at least once to deliver an event and retry using exponential backoff, keeping on sending events to the Event Handler until it acknowledges the request with either an HTTP 200 OK or HTTP 202 Accepted.
This means that Event Grid will not give up on delivering an event, even if the Event Handler is not available initially.
The back-off mechanism will use progressively longer waits between retries for following error responses, and it will last until 24 hours have passed – after 24 hours, the Event Grid will delete the event.
This approach ensures that events are delivered in a timely manner, even in the presence of temporary errors or unavailability of the Event Handler.
Architecture and Design
Loosely coupled architectures are a key benefit of Azure Publish Subscribe, enabling you to build systems that are more resilient to network failure.
This is achieved by using the Service Bus as a connecting broker between different components, eliminating direct dependencies between them.
The Service Bus can act as the central hub, facilitating communication between various parts of a system, making it easier to scale out based on demand.
Easily leverage the Service Bus to architect applications that support application load balancing, ensuring your system can handle increased traffic without compromising performance.
Rules and Actions
In many scenarios, messages with specific characteristics need to be processed differently. You can configure subscriptions to find messages with desired properties and perform modifications to those properties.
Service Bus subscriptions see all messages sent to the topic, but you can only copy a subset of those messages to the virtual subscription queue by using subscription filters. These filters are accomplished using SQL filter expressions that operate on the properties of the message.
Subscription filters can be applied to both system properties, such as Label, and custom application properties, like StoreName. Without a SQL filter expression, any filter action defined on a subscription is done on all the messages for that subscription.
For a full working example, check out the TopicFilters sample on GitHub.
Express Entities
Express entities are designed for high throughput and reduced latency scenarios, but they come with a trade-off: potential data loss and/or redelivery of messages.
In express entities, messages are initially cached in memory instead of being immediately stored in the messaging store. This results in lower latency and higher throughput, but at the risk of losing some acknowledged runtime operations in case of a machine reboot or hardware issue.
The key difference between regular entities and express entities is how they handle runtime operations. In regular entities, these operations are persisted to the store first, while in express entities, they're acknowledged to the client as successful first and then lazily persisted to the store later.
Express entities are not recommended for use due to their potential drawbacks, and the Premium tier of Service Bus does not support them.
Loosely Coupled Architectures
Loosely coupled architectures are a thing of beauty. They allow systems to be more resilient to network failure and can easily scale out based on demand.
The Service Bus can act as the connecting broker between different components of a system, eliminating direct dependencies between them. This makes it easier to leverage the Service Bus to architect applications that support application load balancing.
Loosely coupled systems are more flexible and adaptable, making them a great choice for complex applications. With the Service Bus, you can build systems that are more robust and easier to maintain.
By using the Service Bus, you can break down complex systems into smaller, more manageable components. This makes it easier to develop, test, and deploy applications.
Loosely coupled architectures are all about reducing dependencies and increasing flexibility. With the Service Bus, you can create systems that are more resilient and easier to scale.
Mobile and Explorer
You can easily build applications that can distribute event notifications and data to occasionally-connected clients, such as smart phones or tablets.
The Service Bus enables you to expose notifications or events from an application running either in Windows Azure or in your private cloud environment.
This means you can ensure that notifications or events are ultimately delivered to mobile devices, even if they're not always connected to the internet.
Mobile Applications
Mobile applications are a great way to stay connected with your users, and the Service Bus makes it easy to build apps that can reach mobile devices.
You can expose notifications or events from an application running in Windows Azure or your private cloud environment, and ensure they are delivered to mobile devices.
The Service Bus allows you to distribute event notifications and data to occasionally-connected clients, such as smart phones or tablets.
This means you can keep your users informed, even when they're not connected to the internet.
Exploring Topics and Subscriptions with Explorer
You can explore Service Bus queues, topics, and subscriptions easily with the Service Bus Explorer. This free tool is a graphical interface to create new subscriptions with filters, instead of having to create them by code always.
The Service Bus Explorer can be downloaded from http://code.msdn.microsoft.com/windowsazure/Service-Bus-Explorer-f2abca5a. Once downloaded, you can run the executable to work with it.
To use the Service Bus Explorer, you can add a service bus namespace and it will load and visualize the queues and topics/subscriptions at your Windows Azure service bus namespace. You can also see the rules that are defined for each subscription.
A default rule gets created with a Filter “1=1”, which basically means every message validates against the filter. This is useful for creating new subscriptions easily with a rule or adding rules to existing subscriptions.
To create a new subscription, you can use the Service Bus Explorer to directly set a filter for the subscription. This is a useful feature, especially when compared to the Windows Azure portal at the time of this writing.
Here are some options to create topics and subscriptions:
- Azure portal
- PowerShell
- CLI
- ARM templates.
Sources
- https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions
- https://learn.microsoft.com/en-us/azure/azure-web-pubsub/key-concepts
- https://robbincremers.wordpress.com/2012/02/20/windows-azure-service-bus-messaging-with-publishsubscribe-pattern-using-topics-and-subscriptions/
- https://turbo360.com/blog/building-reactive-solution-with-azure-event-grid
- https://www.imaginarium.dev/azure-webpubsub/
Featured Images: pexels.com