Azure Event Hub or Azure Service Bus?
Some years ago, I was tasked to analyze Azure Event Hub & Azure Service Bus capabilities so that one of these platforms can be used in one of my project to meet some requirements. I sat with my team (which comprised of 6 super smart developers) & we started brainstorming...
Simple. Azure Event Hub is to be used in case of events & Azure Service Bus is to be used for messages. Problem is solved! Or is it?
Some years ago, I was tasked to analyze Azure Event Hub & Azure Service Bus capabilities so that one of these platforms can be used in one of my project to meet some requirements. I sat with my team (which comprised of 6 super smart developers) & we started brainstorming which one cater to our needs. During the exercise, we realized that both of these platforms were able to cater to our requirements(mostly). But then we thought that if Azure is offering two separate platforms then there must be some factors which differentiate between the two. Right? We dig deeper & were able to find some basic use cases to decide which one is the best fit for our requirements.
Fast forward some years & I still see a lot of folks getting confused when it comes to choosing any one out of these two platforms/services. Though there are many technical differences between these two but to finalize which one to go for, it is imperative to understand some basic factors which actually make it far more easy to decide. In this article, I am going to share those with all of you.
So, here it goes.
Now you would think what kind of statement is that. Both are more or less the same thing. Right? Wrong! Both are very different in their own ways. We first have to understand the difference between an event & a message to make our decisions.
Following are the main use cases which differentiate between the two -
- Event denotes something has happened. Usually, the events are generated by the components which are not part of or loosely coupled within the overall system. The component generating the event is not aware of how that event is going to be processed or what action would be taken on the basis of that event. Events may or may not have sufficient data required for other component to process it.
- Message denotes something has to happen. Usually, the messages are generated by the components which are part of or tightly coupled within the overall system. The component generating the messages is aware of how that message is going to be processed. Messages must have sufficient data required for other component to process it.
Let's try to grasp these with two real world examples.
Ronit is a Software Developer in an MNC in Noida. He plans to go to his home town in the coming weekend to meet his old buddies. He connects with them over the call & tell them about his plan. They become very happy to hear this & they tell him to drop a message on WhatsApp group once he reaches the home town.
Here, Ronit is just supposed to broadcast a message on the group to indicate that he has arrived. He is not aware if any or how many of his friends will come to meet him. Also, he is not a regular part of their lives. Every friend is living his own life like his own family, own job or business etc. i.e. he is a loosely coupled component in the overall (lives of friends) system. You can see that this example fulfills following conditions of an Event from the use case #1 -
- Event denotes something has happened.
- The events are generated by the components which are not part of or loosely coupled within the overall system.
- The component generating the event is not aware of how that event is going to be processed or what action would be taken on the basis of that event.
So the message which Ronit is supposed to drop in the group is more suited to be called an event rather than a message.
Dino & Shweta are a happy married couple, and both are working. Dino works as a Technical Trainer who provides online classes & Shweta works as a Teacher in a reputed school. They have a cute 4 years old kid who goes to a school which is not far from their home. The responsibility of dropping the kid to school is taken care of by Shweta & Dino takes care of picking him up from the school at around 10.30 AM. Everyday the bells toll at around 10.00 AM in a near by temple & when Dino hears those he leaves his home for the school to pick up his kid. Shweta is aware of Dino's forgetful nature so everyday she sends a WhatsApp message to Dino at around 10.00 AM to remind him.
Now, if we dissect above example we would come to know following things -
- The bell tolls of temple act as a notification/reminder for Dino that it's time(10.00 AM) to leave for the school to pick up his kid. So, in a way, the temple is broadcasting the information that it's 10.00 AM(something has happened) but the temple is not bothered(not aware) how this information would be processed/used by the persons who hear the bell tolls. Some may just hear it & do nothing. And some may use it for their own different purposes, like Dino(temple is not a part of Dino's regular life/system). So it's safe to say that the bell tolls are more suited to be called as events.
- The WhatsApp message which Shweta drops to Dino also acts as a notification for the same thing. But here the intent of the message is that the kid must be picked up(something has to happen). The message has been generated by Shweta which is a part of Dino's life(a tightly coupled component of the overall system). Shweta knows how this message is supposed to be used by Dino(aware of how the message is going to be processed). It's safe to say that Shweta's message is more suited to be called as a message.
Now let's translate our understanding to a practical scenario of a system.
Consider there is a web application product which comprises of following components -
- An MVC application used for storing in-house documents. A user logs into the application & can upload some of his official document so that he can refer or share it with someone, if required in future. User can email that document also from within the application. The application is hosted on AWS.
- A Web API which is used to send the emails. The email functionality mentioned in #1 calls this Web API to send the emails. This Web API is hosted on Azure.
Below is a high level diagram showing overall functionality flow -
As you can see that the whole flow is totally synchronous i.e. user clicks email button in MVC app, the MVC app calls Email Web API & then the Web API sends out the email. Due to this synchronous/real time nature, the Email API gets very slow when the load increases. So a requirement comes up to modify the system to improve the performance of the API. One way to improve the performance & achieve better scalability is to break down the Email API in following components -
- An Ingestion API that will receive the data from MVC app for email & ingest(store) it in database.
- A windows service(on Azure VM) or an Azure Function which will keep polling the data in the database. It will then pick up the data from DB & push the data to Azure Event Hub or Azure Event Service. Event Hub/Service Bus will act as a queue here.
- Another Azure Function will keep listening to Event Hub/Service Bus & once it finds that the data has been pushed there, it will pick the data & call the Email API.
- Email API whose sole purpose is only to send the emails.
Below is a high level diagram of the flow & components for better clarity -
The approach is set but we have to finalize which service we have to go for, Event Hub or Service Bus? And for that, we have to analyze first that the data we are pushing is more suited to be an event or a message. Right? Let's try to fit the points we discussed till now in this scenario.
- Do the events/messages which are supposed to be pushed in Event Hub/Service Bus denote something has happened or has to happen? As per the scenario, the data needs to be used to send out the emails. That means something has to happen.
- Is the Azure function that is supposed to send the events/messages to Event Hub/Service Bus a part of the whole system(Web APIs + DB + Azure functions) or it's a separate isolated system? As we can see that it's a part of the whole system which is responsible for sending the emails.
- Is the Azure function which sends the events/messages to Event Hub/Service Bus aware of how those are going to be used further? Yes it has to be aware of because it has to send the data with events/messages in pre-defined format so that the other Azure function can use this data & call the Email API without any or less modifications.
- Do the events/messages sent out by Azure function to Event Hub/Service Bus contain enough data to be used further? Yes. It needs to have sufficient data so that the other Azure function can pass this data to Email API without or less modifications.
As you can see that the use case is more suited for messages rather than events. So an Azure Service Bus would be more suited in this scenario rather than an Azure Event Hub.
Let's take one more scenario.
Consider that the company decides to create two separate applications as below -
- An application which will scan the document, once user uploads it from MVC app, for risk analysis. So this application will require some notification/message as soon as the user uploads any document from the MVC app so that it can kickstart its own workflow/business logic.
- An indexing application which will index the uploaded document. Now this application will also require some notification/message as soon as the user uploads any document so that an indexing process can be decided to kickstart.
A very high level diagram for better clarity is shown below -
Now once again we are at crossroads! What to choose? Event Hub or Service Bus? And once again let's try to fit the same points here as well -
- Do the events/messages which are supposed to be pushed from MVC app (once user uploads any document) in Event Hub/Service Bus denote something has happened or has to happen? As per the scenario, the event/message may denote both the things. So here it may be difficult to put it in one category.
- Is the MVC app that is supposed to send the events/messages to Event Hub/Service Bus a part of the whole system(MVC App + Scanning Application + Indexing Application) or it's a separate isolated system? As we can see that it's not a part of the whole system. MVC app is supposed to be a different system from Scanning & Indexing applications.
- Is the MVC app which sends the events/messages to Event Hub/Service Bus aware of how those are going to be used further? No, it need not be aware of it because it is just notifying that the user has uploaded the document, that's it. How Indexing/Scanning application use it, MVC app is not bothered about it. So, in a way, it give a clear answer to our first point now i.e. the event/message generated by MVC app just denotes something has happened i.e. user has uploaded a document.
- Do the events/messages sent out by MVC app to Event Hub/Service Bus contain enough data to be used further? No. They need not have sufficient data because MVC app is just notifying about the action with some basic data attached with the event/message. If Indexing/Scanning application need more data, they will need to fetch it from other source(like DB etc.) & apply business logic on it(if required).
As you can see that the use case is more suited for events rather than messages. So an Azure Event Hub would be more suited in this scenario rather than an Azure Service Bus.
Though there are many other factors as well which should be considered but the points which I have shared in this article should serve as a starting point of your analysis.
I hope the article was helpful in clearing some of the your doubts in choosing between these two giant services.