How to Use Azure Storage Queues

This guide provides step-by-step instructions on how to use Azure Storage Queues for reliable message queuing in your applications. Azure Queues is a service that allows you to store large numbers of messages that can be processed by a client application at your own pace.

1. Setting Up Azure Storage

Before you can use Azure Storage Queues, you need an Azure Storage account. If you don't have one, you can create one through the Azure portal.

Once your storage account is created, you'll need to retrieve your storage account name and access key. These are found in the "Access keys" section of your storage account in the Azure portal. You'll use these credentials to authenticate your application requests.

2. Creating a Queue

You can create a queue using the Azure SDKs, Azure CLI, PowerShell, or the REST API.

Using Azure CLI:


az storage queue create --name myqueue --account-name mystorageaccount --account-key 'YOUR_ACCOUNT_KEY'
            

Replace myqueue with your desired queue name and YOUR_ACCOUNT_KEY with your actual storage account key.

3. Adding Messages to a Queue

Messages can be added to a queue using the Put Message operation.

Using Azure SDK for Python:


from azure.storage.queue import QueueClient

connection_string = "YOUR_CONNECTION_STRING"  # Or use account_name and account_key
queue_name = "myqueue"

queue_client = QueueClient.from_connection_string(connection_string, queue_name)

message_text = "This is the first message."
queue_client.send_message(message_text)

message_text_2 = "This is another message."
queue_client.send_message(message_text_2, visibility_timeout=60) # Message will be invisible for 60 seconds
            

You can obtain your connection string from the Azure portal under your storage account's "Access keys" section.

4. Retrieving and Processing Messages

Messages are dequeued using the Get Messages operation. When you retrieve a message, it becomes invisible to other clients for a specified period (the invisibility timeout). After processing, you must explicitly delete the message.

Using Azure SDK for Python:


from azure.storage.queue import QueueClient

connection_string = "YOUR_CONNECTION_STRING"
queue_name = "myqueue"

queue_client = QueueClient.from_connection_string(connection_string, queue_name)

# Retrieve up to 5 messages, with a 30-second invisibility timeout
messages = queue_client.receive_messages(max_messages=5, visibility_timeout=30)

for msg in messages:
    print(f"Processing message: {msg.message_text}")
    # Simulate processing the message
    print("Message processed.")
    # Delete the message after successful processing
    queue_client.delete_message(msg.id, msg.pop_receipt)
    print("Message deleted.")
            

If a message is not deleted within its invisibility timeout, it becomes visible again in the queue to be processed by another client or the same client.

5. Deleting Messages

Messages must be explicitly deleted after they have been successfully processed. This is done using the Delete Message operation, which requires the message ID and its pop receipt.

6. Other Queue Operations

Best Practices

Further Reading