How to Use Azure Storage Queues

This guide provides practical steps and code examples for interacting with Azure Storage Queues, a fully managed cloud messaging service that enables you to decouple application components. Use queues to communicate between different parts of your application, ensuring reliable message delivery and enabling asynchronous processing.

What are Azure Storage Queues?

Azure Storage Queues offer a simple, robust, and cost-effective way to send and receive messages between application components. They are designed for scenarios where you need to:

Prerequisites

Creating a Queue

You can create a queue using the Azure portal, Azure CLI, or the Azure SDK. Here's an example using the Azure SDK for Python:


from azure.storage.queue import QueueServiceClient

# Replace with your actual connection string
connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
queue_name = "my-sample-queue"

try:
    # Create the QueueServiceClient object
    queue_service_client = QueueServiceClient.from_connection_string(connect_str)

    # Create a queue
    queue_client = queue_service_client.create_queue(queue_name)
    print(f"Queue '{queue_name}' created successfully.")

except Exception as ex:
    print(f"Error creating queue: {ex}")
            

Adding Messages to a Queue

Messages can be added to a queue as strings. The maximum message size is 64 KB.


from azure.storage.queue import QueueServiceClient

connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
queue_name = "my-sample-queue"
message_text = "This is my first message."

try:
    queue_service_client = QueueServiceClient.from_connection_string(connect_str)
    queue_client = queue_service_client.get_queue_client(queue_name)

    # Add a message to the queue
    queue_client.send_message(message_text)
    print(f"Message '{message_text}' added to queue '{queue_name}'.")

except Exception as ex:
    print(f"Error sending message: {ex}")
            

Retrieving and Deleting Messages

When you retrieve a message, it becomes invisible for a specified period (the visibility timeout). After processing, you must delete the message. If not deleted within the timeout, it becomes visible again.


from azure.storage.queue import QueueServiceClient

connect_str = "YOUR_AZURE_STORAGE_CONNECTION_STRING"
queue_name = "my-sample-queue"

try:
    queue_service_client = QueueServiceClient.from_connection_string(connect_str)
    queue_client = queue_service_client.get_queue_client(queue_name)

    # Get a message from the queue
    message = queue_client.receive_message()

    if message:
        print(f"Received message: {message.content}")

        # Process the message here...

        # Delete the message after successful processing
        queue_client.delete_message(message.id, message.pop_receipt)
        print(f"Message '{message.content}' deleted.")
    else:
        print(f"Queue '{queue_name}' is empty.")

except Exception as ex:
    print(f"Error processing message: {ex}")
            
Important: Always ensure you delete messages after successful processing to prevent them from being re-processed. You can also use the peek_messages() method to view messages without making them invisible.

Queue Operations Summary

Best Practices

Tip: For more complex scenarios or high throughput, consider using Azure Service Bus Queues, which offer advanced features like sessions, transactions, and dead-lettering.