TIP

🔥 Make sure you star the repo (opens new window) to keep up to date with new tips and tricks.

💡 Learn more about Azure Service Bus queues, topics, and subscriptions (opens new window).

📺 Watch the video : How to use Azure Service Bus Queues (opens new window).

# How to use Azure Service Bus Queues

# Use Azure Service Bus Queues to decouple applications

Modern applications are made up of many components and services. For instance, you might have a web application that users use to order items from a shop. The web application uses an API that runs in an Azure Function (opens new window) to process the order. This works well, until the Azure Function is offline. To make sure that your application stays available, and that the web application and backend APIs can scale independently, you can decouple them with a queue. Azure Service Bus Queues (opens new window) provides a queue that helps with this.

The sending application (i.e. the web application), puts a message on the Azure Service Bus Queue. The backend API (i.e. the Azure Function), reads the message from the queue and processes the order for the web shop. You can use Azure Service Bus Queues as a simple decoupling mechanism, and use advanced features, like first-in, first-out (FIFO) guarantee (opens new window), chaining Service Bus entities with autoforwarding (opens new window), or Scheduled messages (opens new window).

In this post, we'll explore Azure Service Bus Queues by creating and using one in the Azure portal.

# Prerequisites

If you want to follow along, you'll need the following:

# Creating and using an Azure Service Bus Queue in the Azure portal

Let's create an Azure Service Bus. We'll use the Service Bus Explorer in the Azure portal to send messages to the queue.

  1. Go to the Azure portal (opens new window)
  2. Click the Create a resource button (the plus-sign in the top left corner)
  3. Search for service bus, select the "Service Bus" result and click Create
    1. This brings you to the Create namespace blade
    2. Select a Resource Group
    3. Fill in a Name for the service bus namespace
    4. Pick a Location
    5. Select a Pricing Tier. The Basic tier is fine for this demo
    6. Click Review + create and Create after that

(Create an Azure Service Bus Namespace in the Azure portal)

When the Service Bus Namespace is created, navigate to it in the Azure portal.

  1. In the Service Bus Namespace in the Azure portal, select the + Queue button in the top menu of the overview blade

(Create a queue button in the Azure portal)

  1. Fill in the Create queue blade
    1. Fill in a Name for the queue
    2. Leave everything else as it is. Notice the settings for the advanced features like session, partitioning and forwarding
    3. Click Create to create the queue

(Create a Queue blade in the Azure portal)

  1. Click on the queue to open it
  2. Select the Service Bus Explorer menu item
  3. First, we'll send a message to the queue
    1. Select Text/Plain for the Content Type. Messages for Azure Service Bus can also contain XML or Json content
    2. Type a text message in the text field
    3. Select Expand Advanced Properties. This allows you to edit advanced properties, like Label and Message ID. You can use these in your application to track or filter messages
    4. Type a value in the Label property
    5. Click Send to put the message on the queue

(Send message to the Service Bus Queue)

  1. Scroll up to the menu of the Service Bus Explorer
  2. Messages in the Service Bus Queue will stay there until they are deleted or expire. You can use the Peek function to take a look at a queue message, without deleting it. Click on the Peek menu
  3. You'll see that there is 1 message on the queue. Click Peek
  4. Select the message to see its details
  5. Select the Receive menu
  6. The message is still on the queue. Click Receive to perform a ReceiveAndDelete action, which reads the message and deletes it from the queue
  7. Select the message to read its content. You'll also see the value of the Label property that you have set when you sent the message

(Receive a message from the Service Bus Queue)

# Conclusion

You can use Azure Service Bus Queues (opens new window) to decouple parts of your solution and make them more available and scalable. Azure Service Bus Queues also provides advanced features like first-in, first-out (FIFO) guarantee (opens new window), chaining Service Bus entities with autoforwarding (opens new window), and Scheduled messages (opens new window). Go and check it out!