Search This Blog

Azure Service Bus

Azure Service Bus
  • It is a messaging service in azure. Usually data is in form of JSON but it could be in text, xml as well. 
  • Two types of service bus used in azure 1) Queue and 2) Topic. 
  • Azure service bus queue is different from Azure Storage Queue.  The difference between queue service in Azure Storage and Queue service in Azure service bus is - In Storage queue we send data as string whereas in service bus queue we send data in bytes. 
  • Publisher will go ahead and add a message in a queue and consumer will consume will go ahead and consume a service from a queue
  • Topic: Consumers in case of Topic are known as subscribers. This is the same subscribe logic used in case you subscribe any youtube channel. It means behind the scene Topic is implemented there. 
  • Difference between Receive and Peek message in Queue
    • Peek- Will not actually consume the message. It will only see what is the first message in a queue. 
    • Receive will consume the message and Queue count will become 0. (Note: it will not go in dead queue)
  • Name space requried to work with Azure service bus when want to consume  it
  • Connection String-
  • If we want to give access to all Tasks and Queues in one go. Use the Shared Access policy at Service Bus Namespace level. 
  • Otherwise we have SAS Policy at Queues and Topic level as well. 
  • <<space>>
  •  IQueueClient _client;
                _client = new QueueClient(_bus_connectionstring, _queue_name);
  • In the background these objects make use of the MessagingFactory object. This provides the internal management of connections.
  • Closing and opening a connection using queueclient on azure service is a costly process so do not open and close the connectdoin with every work instead use the same queueclient object for multiple purpose. 
  • Incase of Topic we use TopicClient. To create a subscription we use SubscriptionClient Class
  • TimeToLive : -  In case of queue Consumer has 1 day(timetolive) to consume that message from the queue. After the timetolive expries message can go to DeadLetterQueue or can be completely removed from the queues. 
  • LockDuration: Message will be invisible to period of lockDuration, it means after lockduration message will be avalible for others consumer in case it was not deleted by old consumer. 
  • There are 3 parts of message u send to azure message queue 1) Body(this is in byte and this is the main data we want to transfer (2) Broker properties (3) User Properties 
  • by default, the triggers such as BLOB, queue triggers will be retried upto 5 times. After the fifth retry, the triggers sends a message to a special poison queue.
Topics
  • Filters for subscribers : In case we are sending messages to Topic we can create filters for subscribers so that subscribers can filter out what kind of messages they want to receive.  
  • By default SQL filter is implemented with value of filter is 1=1. it means a subscriber will get all messages. IF you remove the default filter then a subscriber will stop getting any messages. 
  • Types of filters : Boolean filters, SQL filters, Correlations filters
  • Always prefer correlation filters over SQL filters whereever possible for better performance. 
  • CorrelationID - This can be used to correlated multiple messages together. 
  • ManagementClient class is used to create/ Manage the new subscription for topic in azure bus service.  
  • How you can enable duplicate detection on your messages? -- First enable the duplicate detection and then enable the message id property in your body. duplicate detection will check the message id for duplicity. 
  • This is used when you want to manage messages. like creation of Queue or Topic


  • We have to use Message class to construct a message
  • We have to use "Encoding.UTF8.GetBytes” method before sendthing the message to azure service bus queue
  • use the “SendAsync” method to send the message to the queue.
  • The messages in Azure Event Hubs and Azure Storage Queues can only last for a maximum of 7 days.

No comments:

Post a Comment