Azure Service Bus - Introduction for beginners
Tags: azure, serverless, beginners, azureservicebus
Published: 2021-02-13 11:19:30

In this article we will be exploring what is Azure Service bus.

You can watch the entire step by step video on Youtube
{% youtube fERL-FXDEAg %}

So what are we going to cover today.

- What is Azure Service Bus?
- Some Terminology
- Application structure Old vs New
- Azure Service Bus types
- Protocols
- Features
 
In essence Azure Service Bus is a way to send a message from point a to point b. it means that i am going to put the info that i want to send somewhere and the specific service thats needs it will pick it up. 

Azure Service bus leverage the idea of async process in the message queue and its very durable.

Once we publish a message to the queue its very hard to loose that data

![Sample ASB](https://dev-to-uploads.s3.amazonaws.com/i/detf28du2dxv2wn47o12.png)

### Traditional application

In a traditional web application, the entire processing will be in the monolith so processing a request can take up to a minute or two and the user will have to wait in the unknown until the request has been processed

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uphbtjx7gqi49rea34oo.png)

In this case all of the requests are being processed by the API server

Pros:
- Easy to host
- Single point of processing

Cons:
- Single point of failure
- Very hard to scale
- bad user experience
- difficult to maintain without down time


### Service based application

The application divide the work that needs to be done to different services. Services can run asynchronously and the user will get a faster response while the application does it processing and once the process has been complete the user will get a confirmation. 

![New structure](https://dev-to-uploads.s3.amazonaws.com/i/qwig1kd1gzaj709izldg.png)

We can see that the API delegate the processing of the order to different queue and every single one of them is running asynchronously to process the order   

Pros
- Scalable 
- Easy to maintain without down time
- better user experience
- less error prone

Cons:
- Complicated to host
- multiple services to manage

## Azure Service bus:

There is 2 types in Azure Service bus: Queue and Topics

**Queue**:

- Follow the FIFO model (First in first out)
- The consumer is 1 type of the application
- Point to point model
- We cannot have multiple types of the application as when its consumed by 1 service its lost of ever no other service will be able to pick it up

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sgtw9leg67q4r4uczxgn.png)

**Topics**:

- Topics will have subscriptions
- Once there is a message in the queue every application that is subscribed to the queue will receive a copy of that message
- Publish and subscribe model: deals with contract negotiations and changes because there is more consumers
- Services can have filter to allow only certain types of messages to be sent to them

![Queue Topics](https://dev-to-uploads.s3.amazonaws.com/i/1ae7ee28aoj2y89pu7e1.png)

**Azure Service Bus Protocols:**

- AMQP is the default one (Advance Messaging Queue Protocol) it’s the one the is used out of the box in the SDK
- HTTP incase there is some firewall rules and access rules this protocol can be used
- JMS (Java Messaging Service) for premium service bus

## **Features**

**Dead letterings**: 

if we publish something into the queue and the processor fails to process the message, it will go back to a separate queue called dead letter queue so we can go back and check what is the issue and incase we fix the issue with the processor we can reinitiate the letters in the queue so we don't loose any information

![Dead lettering](https://dev-to-uploads.s3.amazonaws.com/i/9sd0a44iir22dcnk08wc.png)

**Message Sessions**:

We want to group a list of messages together and process them all in a single instance instead of separate processing

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xdgll1uymm2q9mwxew7v.png)

**Duplicate Message detection**:

In case a message got released multiple times to the queue, the queue is capable to knowing that there is a duplicated and ignores it

![Duplicate Message](https://dev-to-uploads.s3.amazonaws.com/i/9grrbiwvy4pd0vkquddx.png)

**Scheduling**:

When a message enters the Queue we can give it a date and time on when it needs to be processed by the processors. Only at that time its processed else it will stay in the queue and wait. 

![Scheduling](https://dev-to-uploads.s3.amazonaws.com/i/5eekun86262cyqrx6w2d.png)

**Subscription filters**:

When we are using the Pub/Sub approach if one of the processors dont want to listen to every incoming message, we can add a filter to the process to accept only certain types of messages

![Filtration](https://dev-to-uploads.s3.amazonaws.com/i/92iycu305hqen9jsnald.png)

**Time To Live (TTL)**

Add a deadline to the message so in case the deadline has passed and the message has not been processed it is ignored and not processed

![TTL](https://dev-to-uploads.s3.amazonaws.com/i/ch6b1m3kcv5xzlwiidjx.png)

Links to Azure official documentation :
https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview

Thank you for reading