Your messaging application will need to interact with other applications and services to deliver and exchange information. Microsoft Azure allows us to send and receive messages through a simple queue. If an ordinary queue isn’t enough, a queue with a publish and subscribe mechanism could solve the problem. Otherwise, some applications on Azure need a connection between applications and queues. To perform such tasks, the platform offers the Azure Service Bus. Service Bus is a reliable Cloud Messaging as a Service on Azure (MaaS) application that supports hybrid integration in a simple way.
In this post, we’ll walk you through the features of Service Bus and how you can use it.
What is Azure Service Bus?
Azure Service Bus is a collection of services that allow us to manage communication between our different application components.
Service Bus is a multi-tenant cloud service. As mentioned in Marco Parenzan’s post Creating multi-tenant applications in Microsoft Azure, multi-tenancy is one of the founding principles of cloud computing. When we said that Service Bus is multi-tenant, this means that the service is shared by multiple users for connecting applications through the cloud.
Service Bus is made up of four communication services. You can create one or more instances of each service to connect to our applications. These services include:
- Queues: Queues act as an intermediary layer between application components and they store messages that other application components can receive.
- Topics: A one-directional communication mechanism that allows client applications or devices to subscribe to a topic. A separate application or device can publish messages to topics for consumption by the client applications.
- Relays: Relays are a bidirectional proxy for communication with a Windows Communication Foundation (WCF) service. Client applications can bind directly to the relay endpoint and the relay infrastructure routes messages to the appropriate WCF service endpoint.
- Notification Hubs: A managed, brokered system for distributing messages from server applications to client devices across various platforms using local notifications.
This is how Service Bus works in the cloud:
What are namespaces?
Namespaces serve as a basic logical grouping of Service Bus service instances. An application developer would first use Service Bus to create a namespace, and then define the communication mechanisms he or she needs within that namespace. On Azure, what you call the namespace is globally unique. When you create a queue, topic, or relay in a namespace, the name is then combined with the namespace to create a unique identifier for the object. Applications can provide this name to Service Bus, and then use that queue, topic, or relay to communicate with one another. Service Bus namespaces can also contain management credentials or shared keys, that client applications can use to connect to Service Bus.
What are Storage Queues?
Storage queues provide a consistent and reliable way to store messages that can be consumed by any number of workers. It provides multi-messaging between the role instances as well as build for massive scale and multiple messages. Common scenarios for using storage queues include:
- Creating a backlog of work to process asynchronously
- Passing messages from an Azure web role to an Azure worker role
Differences between storage queues and Service Bus queues
In my previous post, Getting started with Azure, I wrote about cloud design patterns implemented by Microsoft Azure to solve queue-based load leveling problems for a service whose peaks in demand cause it to overload and therefore unable to respond to requests in a timely manner. Flooding a service with a large number of concurrent requests can also result in the service failing if it’s unable to handle the contention these requests cause. The solution here is to use a queue. The queue acts as a buffer, storing the message until it’s retrieved by the service. The service retrieves the messages from the queue and processes them.
Microsoft Azure supports two types of queue mechanisms: Storage queues and Service Bus queues.
Storage queues: As part of the Azure storage infrastructure, storage queues feature a simple REST-based Get/Put/Peek interface, providing reliable, persistent messaging within and between services.
Service Bus queues: Are part of a broader Azure messaging infrastructure that supports queuing as well as publish/subscribe, and more advanced integration patterns.
Service Bus queues guarantee that messages are processed in a FIFO order. Storage queues do not guarantee this. Both of them can guarantee that messages are delivered at least once and Service Bus queues can also guarantee that a message is delivered only once.
Here is an at-a-glance comparison of the capabilities available with storage queues and Service Bus queues.
|Comparison criteria||Storage queues||Service Bus queues|
|Ordering guarantee||No||Yes: first in first out (FIFO)|
|Receive mode||Peek and Lease||Peek and Lock
Receive and Delete
|Exclusive access mode||Lease-based||Lock-based|
|Lease/Lock granularity||Message level||Queue level|
For more details, please refer to the Azure documentation Storage queues and Service Bus queues – compared and contrasted.
What is Service Bus Relay?
Service Bus Relay provides a flexible way to connect WCF services and clients without having to redesign the network architecture. The Azure Relay service facilitates hybrid applications by including a relay component that can connect existing services to new client applications without exposing the true location or address of the service which allows you to securely expose services.
Relay supports a variety of different transport protocols and web services standards such as Simple Object Access Protocol (SOAP), Web Services standards (WS*), and Representational State Transfer (REST).
Azure Relay has two features:
- WCF Relays: Uses Windows Communication Foundation (WCF) to enable remote procedure calls. WCF Relay is the legacy Relay offering that many customers already use with their WCF programming models.
- Hybrid Connections: Uses the open standard web sockets, enabling multi-platform scenarios.
Hybrid Connections and WCF Relays both enable a secure connection to assets that exist within a corporate enterprise network. For more information, please consult the Relay FAQ in the Azure documentation.
Service Bus Relay Architecture
The Service Bus relay allows you to host WCF services within an existing enterprise environment. We can delegate the task of listening to incoming sessions and requests to the Service Bus service running within Azure. These messages are then routed to the appropriate WCF service. This exposes these services to the application code that is running in Azure, or to mobile workers or extranet partner environments.
Service Bus allows you to securely control who can access these services at a detailed level. It provides a powerful and secure way to expose application functionality and data from existing enterprise solutions and take advantage of it from the cloud.
What is the Service Bus Notification Hub?
The Service Bus Notification Hub is a combination of a service infrastructure and a set of client libraries that publishes push notifications to mobile applications from any application service component.
With Notification Hubs, we can send notifications that are personalized to a specific user, notifications that are distributed to many users across various platforms, and notifications that are filtered to a specific set of users.
The Notification Hubs infrastructure abstracts the implementation of the various Platform Notification Systems (PNS) for each mobile platform. By using a single method call, we can send notifications to various device platforms without having to implement a different message structure or communication mechanism for each platform.
Here are some general scenarios for using the Service Bus Notification Hub:
- Send breaking news notifications to millions with low latency.
- Send location-based coupons to interested user segments.
- Send event-related notifications to users or groups for media/sports/finance/gaming applications.
- Push promotional content to apps to engage with and market to customers.
- Notify users of enterprise events such as new messages and work items.
- Send codes for multi-factor authentication.
Learn more about push notifications for a few popular platforms:
Why use Notification Hubs?
Notification Hubs eliminate the challenges that are involved in managing push notifications. Notification Hubs use a full multi-platform, scaled-out push notification infrastructure and considerably reduce the push-specific code that runs in the app.
Notification Hubs implement all the functionality of a push infrastructure. Devices are only responsible for registering PNS handles, and the backend is responsible for sending platform-independent messages to users or interest groups.
Notification hubs provide a push infrastructure with the following advantages:
– Multiple platforms:
- Support for all major mobile platforms: Windows, Windows Phone, iOS, and Android.
- No platform-specific protocols: The application only communicates with Notification Hubs.
- Device handle management: Notification Hubs maintain the handle registry and feedback from PNSs.
– Works with any backend: It works with cloud or on-premises applications written in .NET, PHP, Java, or Node.
– Scale: Notification Hubs scale to millions of devices without rearchitecting or sharding.
– Rich set of delivery patterns: Associate devices with tags to represent logical users or interest groups:
- Broadcast: Allows for near-simultaneous broadcast to millions of devices with a single Application Programming Interface (API) call.
- Unicast/Multicast: Push to tags representing individual users, including all their devices or a wider group. For example, a user could use the app on separate devices (tablet, phone, etc.) and would require push notifications to either be pushed to all devices or to a specific device.
- Segmentation: Push to a complex segment that is defined by tag expressions (For example, devices in New York following the Yankees).
– Personalization: Each device can have one or more templates to achieve per-device localization and personalization without affecting the backend code.
Notification Hubs architecture
At a high level, all platform notification systems follow the same pattern:
- The client application contacts the PNS to retrieve its handle. The handle type depends on the system. For Windows Notification Service (WNS), it is a URI or notification channel. For Apple Push Notification Service (APNS), it is a token.
- The client application stores this handle in the app backend for later usage. For WNS, the backend is typically a cloud service. For APNS, the system is called a provider.
- To send a push notification, the app backend contacts the PNS by using the handle to target an instance of a specific client application.
- The PNS forwards the notification to the device specified by the handle.
Typical PNS infrastructure
With Notification Hubs, we can rely on the service infrastructure to handle the most complex features and have the application focus only on sending messages. Notification Hubs implement all of the functionality of a push infrastructure. It can be used in flexible ways to register devices and eventually to send a message to the device. Devices can register themselves and receive notifications using the following method:
- The client device reaches out to the PNS by using the Notification Hubs SDK. It registers a unique PNS handle that is used by the service to send notifications to the device whether the application is running or not.
- Alternately, the client device can send its PNS handle to the application backend to have the application register the device.
- When the application backend sends a message to the Notification Hubs service, the service sends the message to the appropriate target clients by using their registered PNS handles. The application backend simply requests that the message be sent and the Notification Hubs service and the PNS handle the actual distribution of messages to client devices.
Messaging as a service on Azure Summary
I absolutely recommend spending some time with Azure documentation to learn more about Azure Service Bus. This topic is also covered in the 70-534 Architecting Azure Solutions Exam, so a better understanding of this service not only help you for certification prep, but also for real-world development for three-tier applications.
Published on 13 June 2017.
Categories: Cloud Native Vision
Founder of CloudMelonVision and a product manager in a top tech company, the author of Microsoft Azure Infrastructure, the Kubernetes Workshop and Certified Kubernetes Administrator (CKA) Exam Guide by Packt Publishing, and technical reviewer for Azure for Architects, 3rd Edition. I am making videos to make technologies simple and entertaining on Youtube, CloudMelonVision, for more content check out my blog website: cloud-melon.com, and follow my blue bird @MelonyQ.