Azure Service Bus Overview
Tags: azure, serverless
Published: 2019-08-30 06:37:00

<h2>What is Azure Service Bus?</h2>
<p>Azure Service Bus is a messaging service on cloud used to connect any applications, devices, and also services running in the cloud to any other applications or services. As a result, it acts as a messaging backbone for applications available in the cloud or across any devices.</p>
<h2>Service Bus Concepts</h2>
<p>At the time of this webinar, there were three basic concepts in Azure Service Bus:</p>
<ul>
	<li>Relay -&gt; To establish a secure connection between applications which is bridged through the cloud</li>
	<li>Messaging -&gt; Used to push/pull messages from the queue</li>
	<li>Event Hub -&gt; High performing stream-based event system</li>
</ul>
<p>Relays and also Event Hubs were moved out as separate services and Service Bus will only refer to Messaging that consists of Queues / Topics</p>
<h2>Multi-Tenant Service Bus environment</h2>
<p>In this environment, we can create a namespace which would be the container for our Service Bus artifacts in the cloud. The namespace can be available in different regions.</p>
<p>Each namespace comprises messaging entities like Queues, Relays or Topics. Most importantly, a namespace will be dedicated only to one type of Service Bus.</p>
<h3>Message processing statistics (as of 2015)</h3>
<p>The statistics generated by the Microsoft team working on Service Bus are as follows,</p>
<ul>
	<li>Event Hub = 30 Billion Events per day</li>
	<li>Messaging = 7 Billion Messages per day</li>
</ul>
<p>It can handle a colossal number of messages in a very executable manner.</p>
<h2>Service Bus Relay</h2>
<p>In Service Bus Relay, we will have an endpoint that will be hosted in the cloud, but the difference would be the fact that our on-premise service will make an outbound connection to register with the Service Bus relay endpoint.</p>
<p>At a conceptual level, we send a message to an endpoint in the cloud from where it will be forwarded to an on-premise service. As a result, it gives us the advantage to connect from one organization to another organization without having to set VPNs.</p>
<p>Some of the key features are,</p>
<img class="alignnone size-full wp-image-80213250" src="http://www.serverless360.com/wp-content/uploads/2018/08/Relay-advantages.png" alt="Azure Service Bus overview" /> 
<p>Service Bus Relay can be used to solve problems in scenarios like,</p>
<ol>
	<li>Information passed between two data centers.</li>
	<li>Building an integration architecture through SAP to forward messages into microservice architecture using Service Bus Relay.</li>
</ol>
<p>Example Scenarios:</p>
<ul>
	<li>Many of the Microsoft API apps connect to on-premise systems use relay under the hood</li>
	<li>Danish pension and also insurance supplier exposes IBM WebSphere integration platform to partners through relay</li>
	<li>Northumbria University use the relay to bridge cloud-hosted application to integrate with on-premise WCF service</li>
</ul>
<h3>Demo-Relay</h3>
<p>This demo explains the process of hybrid connectivity with Service Bus Relay. We would initially work on a WCF service (on-premise service). The demo constructed on a .NET framework with console application is to call the back-end service.</p>
<p>In <a href="https://www.serverless360.com/compare-service-bus-explorer" target="_blank" rel="noopener noreferrer">Service Bus Explorer</a>, we will have a registered endpoint under the “relay” section. Now, a virtual directory (router) will be linked to the configuration file “web” under user defined location to establish a connection. The folder would also comprise of two more files, which are,</p>
<ul>
	<li>Bin directory</li>
	<li>WcfRoutingService(SVC file)</li>
</ul>
<p>In the SVC file, there would be service pointing to WCF routing service endpoint. In the configuration file, there would a WCF service which uses the HTTP relay binding along with other credentials to access the Service Bus Relay.</p>
<p>Finally, Service Bus Relay accepts a message and forwards it to the back-end service. When executed on the console application, every message will be processed within a time frame of 100 milliseconds.</p>
<h2>Messaging</h2>
<p>In Azure Service Bus, We can transfer the data between services or applications in message format like XML, JSON, or text.</p>
<p>In this messaging protocol, Microsoft would handle most of the scaling. We just have to create a namespace using platforms like Azure Portal, Service Bus Explorer or <a href="https://www.serverless360.com/">Serverless360</a>.</p>
<img class="alignnone size-full wp-image-80213251" src="http://www.serverless360.com/wp-content/uploads/2018/08/Messaging-Advantages.png" alt="Messaging Advantages" />
<p>Let us consider some example scenarios,</p>
<ul>
	<li>Construction companies use sensors to monitor buildings in real time to ensure adherence to safety more standards.</li>
	<li>Using Service Bus, “Qoros” (Chinese car manufacturer) connects a car to the internet to stream messages.</li>
	<li>Metro Bank Use Service Bus Messaging to manage near real-time messaging between SharePoint and also Dynamics CRM.</li>
</ul>
<h3>Queue</h3>
<p>A messaging protocol based on the first-in-first-out pattern. Here a sender would send a message to a queue from where the receiver would collect it in initial or later stage. Besides that, queues enable us to store the message until the receiver is available to receive and also process them. The user will receive the message upon request.</p>
<img class="alignnone size-full wp-image-80213224" src="http://www.serverless360.com/wp-content/uploads/2018/08/Queue.png" alt="service bus Queue" /></strong>
<h3>Topics</h3>
<p>Topics are similar to queues. Most importantly, topics make multiple subscriptions available. A sender will send a message to a topic then a routing rule can determine which subscription may receive the slotted copy of the message.</p>
<p>The receiver can receive the message from the subscription. Multiple receivers could be allocated to a subscription.</p>
<img class="alignnone size-full wp-image-80213225" src="http://www.serverless360.com/wp-content/uploads/2018/08/Topics.png" alt="service bus Topics" />
<p>Other messaging patterns include,</p>
<ul>
	<li>RPC</li>
	<li>Scatter Gather</li>
	<li>Pub/Sub</li>
</ul>
<h3>Demo-Messaging</h3>
<p>In this demo, we will send a message to a queue and also collect it back. Initially, a queue (testqueue) will be specified and also the following properties will be determined in Service Bus Explorer as mentioned below,</p>
<ul>
	<li>Path</li>
	<li>Queue properties</li>
	<li>Lock duration</li>
</ul>
<p>To send a message to a queue and also to collect it back, we define the Service Bus connection string along with reference to Service Bus NuGet package.</p>
<p>We will now create a Service Bus message and sent it. On receiving side, we have a competing consumer pattern which enables multiple concurrent consumers to process messages received on the messaging channel. Dynamic message delivery method is employed in a single-thread pattern i.e., first-in-first-out. But in multi-threading pattern, messages will be processed at a higher throughput for multiple receivers.</p>
<p>On receiving side, using “OnMessagingOption” we can specify the number of concurrent receivers required,</p>
<img class="alignnone size-full wp-image-80213227" src="http://www.serverless360.com/wp-content/uploads/2018/08/code-02.png" alt="code-02" />
<p>We will then access the body of the message through the event (OnMessage) and also write it to the console window.</p>
<img class="alignnone size-full wp-image-80213228" src="http://www.serverless360.com/wp-content/uploads/2018/08/Demo-Messaging-Code-02.png" alt="Demo Messaging Code-02" />
<p>The message can either be removed from the queue or abandoned. Service Bus handles serialization with the help of SDK’s.</p>
<p>Let us consider, when we send a message it would be processed in a loop format. So, we will take the .NET type specified as “TestMessage” and also moves it to “BrokeredMessage” to handle the serialization of the .NET type when passed to the queue.</p>
<img class="alignnone size-full wp-image-80213229" src="http://www.serverless360.com/wp-content/uploads/2018/08/Demo-Messaging-Code-03.png" alt="Demo Messaging Code 03" />
<p>The content of the message will be extracted. Then the format of the message which delivered, received should be verified.</p>
<p>Now if we run our demo on two separate console applications, one to send and also another to receive. The respective messages would be sent and also received from the queue.</p>
<img class="alignnone wp-image-80213230 size-full" src="http://www.serverless360.com/wp-content/uploads/2018/08/messaging-code-01.png" alt="Messaging output 01" />
<img class="alignnone size-full wp-image-80213231" src="http://www.serverless360.com/wp-content/uploads/2018/08/messaging-output-02.png" alt="Messaging output 02" />
<h2>Event Hubs:</h2>
<p>Event hubs will manage massive scale event stream processing and are similar to Service Bus messaging. It is similar in the case that it supports sending the message over AMQP (Advanced Message Queuing Protocol).</p>
<p>The key difference between Event Hubs and also Service Bus messaging is that there is no concept of deleting a peek lock message like in Service Bus. Instead, Microsoft has used a reader across a stream which provides a high level of scaling.</p>
<img class="alignnone size-full wp-image-80213252" src="http://www.serverless360.com/wp-content/uploads/2018/08/Event-Hub-Advantages.png" alt="Azure Event Hub Advantages" />
<p>Let us consider an IOT scenario where Event Hub comes in use,</p>
<img class="alignnone size-full wp-image-80213235" src="http://www.serverless360.com/wp-content/uploads/2018/08/event-hub-scenario.png" alt="azure event hub scenario" />
<p>The above picture comprises many subsystems, each with an individual offering. Event Hub can stream a high volume of data from different applications and also store it.</p>
<p>Most importantly, users can process the streamed data and apply it for their individual purpose.</p>
<p>Let us consider some example scenarios:</p>
<ul>
	<li>Microsoft uses Event Hub under the hood as a part of application insights and also API management.</li>
	<li>A company called “Autolib” uses Event Hub as a part of Microsoft IOT stack to exchange information between devices and also vehicles as a part of a car-sharing scheme in Paris.</li>
</ul>
<p>In Event Hub, multiple senders store their telemetry data in the message stream. A receiver cannot remove the message from the stream. Rather, they only get deleted when their retention period gets over. A consumer group can access message from any stream.</p>
<img class="alignnone size-full wp-image-80213236" src="http://www.serverless360.com/wp-content/uploads/2018/08/Event-Hub-example.png" alt="Event Hub example" />
<h3>Application Telemetry</h3>
<img class="alignnone size-full wp-image-80213237" src="http://www.serverless360.com/wp-content/uploads/2018/08/Application-Telemetry.png" alt="Application Telemetry" />
<p>The above picture represents a typical message flow of a system. The idea is to feed telemetry data to Power BI through Event Hub. Message from the user would pass through various systems and also would reach Event Hub.</p>
<p>Here, Stream Analytics and Machine learning process collect the data from Event Hub and send it to Power BI for any business analytics service.  </p>
<h3>Demo-Event Hubs</h3>
<p>This demo illustrates the purpose of Event Hub and is similar to Azure Service Bus messaging.</p>
<p>On Sender Side,</p>
<p>In the console application, we have a connection string followed by an Event Hub name (key name). In the code portion, we have</p>
<ul>
	<li>EventHubClient -&gt; To create an instance of a client through the connection string</li>
	<li>Client.Send -&gt; To send an object</li>
</ul>
<img class="alignnone size-full wp-image-80213238" src="http://www.serverless360.com/wp-content/uploads/2018/08/Demo-Code.png" alt="Demo Code" />
<p>On Receiver Side,</p>
<p>In the console application, we have an “EventHubName” and also storage account followed by a <a href="https://www.nuget.org/packages/Microsoft.Azure.ServiceBus.EventProcessorHost/" target="_blank" rel="noopener noreferrer">ServiceBus.EventProcessorHost</a> which is used to create a listener for different partitions in the Event Hub instance. In the Azure storage account, this package creates a blob for each partition to manage the index of its location in the stream.</p>
<p>When a message is received, it will be fired to the event listed in the below piece of code,</p>
<img class="alignnone size-full wp-image-80213239" src="http://www.serverless360.com/wp-content/uploads/2018/08/Demo-code-02.png" alt="Demo Code 02" />
<p>Our position in the stream can also be acknowledged so if the whole process blew up, we can simply move to the last index we read and also continue our progress. Now, we can see how the operation gets executed. As a result, Event Hub can receive and store any kind of data and events. </p>
<h2>Fact</h2>
<p>Service Bus team provided a cost estimation analysis for processing a billion events per day which lead to,</p>
<ul>
	<li>12 Throughput units</li>
	<li>12,000 events per second</li>
	<li>720,000 events per minute</li>
	<li>43,200,000 events per hour</li>
	<li>Total Cost -&gt; $37.67</li>
</ul>
<h2>Service Bus Costs</h2>
<h3>Relay:</h3>
<ul>
	<li>10,000 listening hours=6.60 pounds</li>
	<li>100 million messages=6.20 pounds</li>
</ul>
<h3><strong>Messaging:</strong></h3>
<ul>
	<li>5 million operations per month=6.20 pounds</li>
	<li>13-100 million operations = 50pounds per million</li>
	<li>100-2500 million = 31pounds per million</li>
</ul>
<h3>Event Hubs:</h3>
<ul>
	<li>100 million events = 1.72 pounds</li>
	<li>The rate of Processing = Throughput units = 14 pounds per month per unit</li>
</ul>
<h2><strong>Service Bus + BizTalk</strong></h2>
<p>Although Service Bus and BizTalk provide connectivity in many aspects for a seamless experience, they could be more aligned in the following aspects;</p>
<p>SB- Adapter (latest version not available)</p>
<ul>
	<li>Service Bus Relay</li>
	<li>Service Bus Messaging</li>
	<li>Event Hubs-it does not provide out-of-box support</li>
</ul>
<p>Addition of features list includes,</p>
<ul>
	<li>AMQP Adapter- Supports non-Microsoft Service Bus type products</li>
	<li>Event Hub Adapter</li>
</ul>
<h2><strong>User scenarios of BizTalk+Relay</strong></h2>
<h3>Scenario 1:</h3>
<img class="alignnone size-full wp-image-80213242" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-1.png" alt="Scenario-1" />
<p>Let us consider a BizTalk server hosted in Azure as a Virtual Machine needs to connect to an on-premise web service. The simplest method to do that would be through SB adapter in BizTalk.</p>
<p>Here, BizTalk Server will send a message to Relay and the rest of the process will be carried out in WCF service.</p>
<h3>Scenario 2:</h3>
<img class="alignnone size-full wp-image-80213243" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-2.png" alt="Scenario-2" />
<p>If we have BizTalk server on-premise followed by a SaaS application in the cloud, a communication can be established between them through Service Bus Relay.</p>
<p>This can be achieved through Service Bus Relay or by assigning a REST API between SaaS component and the server. So, the Relay offers an easy way to expose a BizTalk endpoint from an organization.</p>
<h3>Scenario 3:</h3>
<img class="alignnone size-full wp-image-80213244" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-3.png" alt="Scenario-3" />
<p>Let us consider the scenario where two BizTalk Servers in separate companies needs to communicate. The best way to do that will be to send the message through Relay by exposing their endpoints. This flow does not require any separate infrastructure support.</p>
<h2>User scenarios of BizTalk+Messaging</h2>
<h3>Scenario 1:</h3>
<img class="alignnone size-full wp-image-80213245" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-4.png" alt="Scenario-4" />
<p>This scenario would focus more on transferring a massive number of messages from a web application in the cloud to on-premise Server. Here, BizTalk Server pulls the messages from the queue at a manageable level.</p>
<h3>Scenario 2:</h3>
<img class="alignnone size-full wp-image-80213246" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-5.png" alt="Scenario-5" />
<p>To publish an event in pub/sub format to various receivers, we will push the message from on-premise server to a queue where the application can access it. You cannot directly link the applications to the BizTalk Server located on-premise.</p>
<p>The key difference between Service Bus and BizTalk is that,</p>
<ul>
	<li>BizTalk does not support poll pattern</li>
	<li>Service Bus does not support push pattern</li>
</ul>
<p>Therefore, combining both the services would lead to dynamic productivity. The receiver can access the message available on Service Bus Topic at any time.</p>
<h3>Scenario 3:</h3>
<img class="alignnone size-full wp-image-80213247" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-6.png" alt="Scenario-6" />
<p>Let us consider a scenario where Service Bus Topic will share the resource between two BizTalk application. But if we want to delete the shared resource in the application and patch a new resource, the dependency management in BizTalk would make it harder.</p>
<p>When the BizTalk application has to be updated, we cannot undeploy it because the subscriptions would be eliminated. Therefore, this causes overcomplexity when trying to solve it. </p>
<h3><strong>Demo</strong></h3>
<p>This demo explains on combining both BizTalk and Service Bus, through which messages can be sent and also received between BizTalk applications. Most importantly, you can execute the process in JSON format.</p>
<p>Let us assume there are three BizTalk applications (app1, app2, app3), each comprising of “Hello World” scheme followed by a unique namespace. We will have a sender in each application to receive a message from the file system, which will be routed to Service Bus through Send Ports. Furthermore, you can convert the messages to JSON format before sending it using JSON encoder.</p>
<p>Most importantly, when we receive the message in Service Bus, it would hit the topic comprising of two subscriptions. Each subscription will forward the content to the desired queue. The Topic can specify who can send and also receive the message in it, but if there are multiple receivers, there is a probability where one receiver may fetch the message of another receiver. Hence, this problem can be solved by using “Forward to” option in Topic and can also specify which receiver should receive the appropriate message.</p>
<p>Most importantly, the agenda is to send a message from any application to topics before it gets forward to the selected queues.</p>
<p>After the initialization process gets over, the virtual machine will receive a message. Using the JSON decode, we will change the format of the message in the receive pipeline of the virtual machine. Finally, the respective Biztalk applications receive the message.</p>
<h2><strong>User scenarios of BizTalk+Event Hub</strong></h2>
<h3>Scenario 1:</h3>
<img class="alignnone size-full wp-image-80213248" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-7.png" alt="Scenario-7" />
<p>Here Event Hubs forwards the messages to BizTalk server after processing from event processor host. If the scenario does not involve a huge number of messages to forward to BizTalk server, then the event processor host will act as an isolated BizTalk host without any adapter for a smoother transition.</p>
<h3>Scenario 2:</h3>
<img class="alignnone size-full wp-image-80213249" src="http://www.serverless360.com/wp-content/uploads/2018/08/Scenario-8.png" alt="Scenario-8" />
<p>In this scenario, you share the telemetry data and logging information with Service Bus Event Hub when a message flows through the pipeline of BizTalk server. Most importantly, powerful analysis machines like Power BI and also Cortana Analytics make use of the telemetry data from Service Bus.</p>
<h2>Wrap up</h2>
<p>This blog gives an overview of various Service Bus components available with demo scenarios for a better understanding of the audience. There are a lot of features available in Azure Service Bus and also more to come soon.</p>