Showing posts with label Azure Service Bus. Show all posts
Showing posts with label Azure Service Bus. Show all posts

Tuesday, May 2, 2023

Azure service bus topics vs Queues and example of which one should be used and when ?

Azure Service Bus provides two types of messaging entities: queues and topics. Both queues and topics can be used to implement messaging patterns like point-to-point and publish-subscribe, but they have different characteristics and are best suited for different scenarios.

Azure Service Bus Queues:

  • Queues provide a one-to-one messaging pattern, where a message is sent to a single recipient (receiver) that retrieves and processes the message from the queue.
  • The messages in the queue are processed in the order they were received.
  • Queues guarantee message delivery to a single recipient in a first-in, first-out (FIFO) order.
  • Queues can be used for load leveling, as multiple receivers can be set up to retrieve and process messages from the same queue.

Azure Service Bus Topics:

  • Topics provide a one-to-many messaging pattern, where a message is sent to a topic and all subscribers receive a copy of the message.
  • Subscribers can filter the messages they receive based on message properties.
  • Topics are used for publish-subscribe scenarios, where multiple subscribers need to receive the same message.
  • Topics provide a way to decouple the sender and the receivers, as the sender doesn't need to know who the subscribers are or how many there are.

Here's an example of when to use a queue versus a topic:

Suppose you have an e-commerce website that needs to process orders. Each order consists of multiple items, and each item needs to be processed by a different component in your system. You have two options to implement the message passing mechanism:

  1. Use a queue: In this case, you can have a single queue for all the orders, and each item in the order is sent as a separate message to the queue. Each component can retrieve and process the messages from the queue in a FIFO order, and you can use multiple instances of the component to process messages in parallel.

  2. Use a topic: In this case, you can have a topic for each component, and each item in the order is sent as a message to the corresponding topic. Each component subscribes to its own topic and receives only the messages that match its subscription filter. This way, each component processes only the messages it needs to process, and you can add or remove components without affecting the other components.

In summary, queues are best suited for point-to-point scenarios where messages need to be processed in a specific order, while topics are best suited for publish-subscribe scenarios where multiple subscribers need to receive the same message.

Creating Custom Triggers for Azure Functions with Azure Event Hubs and Azure Service Bus

Azure Functions is a serverless compute service that allows you to run your code on-demand without having to manage infrastructure. With Azure Functions, you can build scalable, event-driven applications that can respond to changes in real-time. One way to achieve this is by creating custom triggers that respond to events from Azure Event Hubs and Azure Service Bus. In this tutorial, we'll show you how to create custom triggers for Azure Functions using these two services.
Prerequisites
Before we get started, you'll need to have the following:

1. An Azure account
2. Visual Studio Code
3. Azure Functions extension for Visual Studio Code
Creating an Azure Event Hub 
The first step is to create an Azure Event Hub. In the Azure portal, select "Create a resource" and search for "Event Hubs". Choose "Event Hubs" and follow the prompts to create a new Event Hub.
Once your Event Hub is created, you can send events to it using any compatible client library. In this tutorial, we'll use the Azure Functions extension for Visual Studio Code to create a custom trigger that responds to events from our Event Hub.
Creating an Azure Service Bus
The next step is to create an Azure Service Bus. In the Azure portal, select "Create a resource" and search for "Service Bus". Choose "Service Bus" and follow the prompts to create a new Service Bus.
Once your Service Bus is created, you can send messages to it using any compatible client library. We'll use the Azure Functions extension for Visual Studio Code to create a custom trigger that responds to messages from our Service Bus.
Creating Custom Triggers for Azure Functions
Now that our Event Hub and Service Bus are set up, we can create custom triggers for Azure Functions that respond to events and messages from these services.

To create a custom trigger for Azure Functions, you'll need to define a function that takes in the event or message as input. This function can then process the event or message and perform any necessary actions.
Custom Trigger for Azure Event Hubs
Here's an example of a custom trigger for Azure Event Hubs:
module.exports = async function(context, eventHubMessages) {
    context.log(`Event hub trigger function called for message array: ${eventHubMessages}`);

    eventHubMessages.forEach(message => {
        // Process message here
    });
};
This function takes in the eventHubMessages array as input and processes each message in the array. You can add your own processing logic to this function, such as sending notifications or updating a database.

To connect this function to your Event Hub, you'll need to add a new function to your Azure Functions app using the Event Hub trigger template. Follow the prompts to specify the Event Hub connection string and configure the function.
Custom Trigger for Azure Service Bus
Here's an example of a custom trigger for Azure Service Bus:
module.exports = async function(context, mySbMsg) {
    context.log(`Service bus trigger function called for message: ${mySbMsg}`);

    // Process message here
};
This function takes in the mySbMsg object as input and processes the message. You can add your own processing logic to this function, such as sending notifications or updating a database.
To connect this function to your Service Bus, you'll need to add a new function to your Azure Functions app using the Service Bus trigger template. Follow the prompts to specify the Service Bus connection string and configure the function.

ASP.NET Core

 Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...