Sunday, December 18, 2022

Azure Service Bus - Session

 I recently attend an interview, even though  I  have very good working experience  in  Azure Service Bus. I could not able to answer well to the interviewer question. So i decided to go back and revisit concepts again.  So let us see it together.

 

Interviewer question:

Consider you are developing Integration solution,  Where some source system sends message to service bus. And you are response of consuming the message from service bus and process them. Source system sends message in following order.


CUSTOMER A- CREATE

CUSTOMER B-CREATE

CUSTOMER B-UPDATE

CUSTOMER C-CREATE

CUSTOMER A-UPDATE

CUSTOMER B-UPDATE

CUSTOMER B-UPDATE

CUSTOMER B-UPDATE

CUSTOMER C-UPDATE

CUSTOMER B-UPDATE

CUSTOMER A-UPDATE

CUSTOMER D-CREATE


How to  process unbounded sequences of related messages together

Answer:

Through session, we can able to process the related message. we will see the implementation in details

Prerequisite:

  • Azure subscription
  • VS code or VS ( Any IDE that support .Net development)

This is journey we will create a queue in azure and also create a console application to send and receive message from service bus (queue). If you are familiar with Service bus, then you can skip the introduction to service bus and Setting up a queue , can directly go the Implementation

Introduction to Service bus

Service bus is the enterprise message broker PAAS service from azure.  We can use this service in following scenario

  • Messaging:Transfer business data, such as sales or purchase orders, journals, or inventory movements.

  • Decouple applications:Improve reliability and scalability of applications and services. Producer and consumer don't have to be online or readily available at the same time. The load is leveled such that traffic spikes don't overtax a service.

  • Load balancing:Allow for multiple competing consumers to read from a queue at the same time, each safely obtaining exclusive ownership to specific messages.

  • Topics and subscriptions: Enable 1:n relationships between publishers and subscribers, allowing subscribers to select particular messages from a published message stream.

  • Transactions: Allows you to do several operations, all in the scope of an atomic transaction. For example, the following operations can be done in the scope of a transaction.

    1. Obtain a message from one queue.
    2. Post results of processing to one or more different queues.
    3. Move the input message from the original queue.

    The results become visible to downstream consumers only upon success, including the successful settlement of input message, allowing for once-only processing semantics. This transaction model is a robust foundation for the compensating transactions pattern in the greater solution context.

  • Message sessions. Implement high-scale coordination of workflows and multiplexed transfers that require strict message ordering or message deferral

     

    Queue:

    Messages are sent to and received from queues (one to one). Queues store messages until the receiving application is available to receive and process them.

     

    Topic: 

    Topic is similar to queue. But it is one to many or publish/subscribe scenarios. 

 

Setting up a queue

  •  Log in to Azure portal home (https://portal.azure.com/#home)
  •  Navigate to all services > Integration > Service Bus
  •  Create Service bus namespace by choosing subscription, resource group, name, location, tier 

  
  • Once Service bus namespace created, go to resource. the create a queue and ensure enable session is checked

Implementation:      

We are going to develop simple console application in .net core. And both  sender and receiver will be same main method. But in real scenario, both sender and receiver may run on difference system.

  • To create the console application , use the following command.  If you don't already installed,  install the net core first  

dotnet new console --framework net6.0 --use-program-main
 
  • In program.cs , we write the method to send message to our session enable queue.  We useSendMessageAsync to send message. And we represent session id as mentioned in screen shot. For sake of simplicity, we developed simple method "GetSessionFromMessage", which return session id based upon message
     


 
 
  •  we used ServiceBusSessionProcessor to receive the message and process them.  But in this example we only process the message that belong  session-1 and session-2 through this processor. And messages belong to session-3 and session-4 will be in service because there is no active client receiver.
  
 
 
 Only CUSTOMER-A and CUSTOMER-B messages belong to session-1 and session-2 are proceed in sequence manner(FIFO). And CUSTOMER-C and CUSTOMER-D message will be in queue, because there is no active client to consume the messages belong to session-3 and session-4
 
Code :git@github.com:shedev/Session-Service-Bus-Example.git
Other Useful Resources:

Duende IdentityServer ClientCredential flow

  Duende (Identity server)  is an OpenID Connect and OAuth 2.0 framework for ASP.NET Core. Duende Identity Server enables the following secu...