Azure Storage Queues Programming Guide

This guide provides detailed information on how to programmatically interact with Azure Storage Queues, a service that enables you to store large numbers of messages that can be accessed from anywhere in the world via HTTP or HTTPS.

Introduction to Azure Storage Queues

Azure Storage Queues offers a simple way to decouple application components. Messages are typically processed by one or more clients. Each message remains in the queue until the client processing it explicitly deletes it. This provides a robust messaging infrastructure for distributed applications.

Key Concepts

Getting Started with Azure Storage Queues

To start using Azure Storage Queues, you'll need an Azure Storage account. You can create one through the Azure portal, Azure CLI, or Azure PowerShell.

Prerequisites

Using the Azure SDK

Azure provides SDKs for various programming languages to simplify interactions with Storage Queues. Here are examples for common languages:

.NET SDK

Install the NuGet package:

Install-Package Azure.Storage.Queues

Example C# code:


using Azure.Storage.Queues;
using System;
using System.Threading.Tasks;

public class QueueExample
{
    public static async Task Main(string[] args)
    {
        string queueName = "my-message-queue";
        string connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING"; // Replace with your connection string

        // Create a QueueClient object
        QueueClient queueClient = new QueueClient(connectionString, queueName);

        // Ensure queue exists, create if it doesn't
        await queueClient.CreateIfNotExistsAsync();

        // Send a message
        string messageContent = $"Hello from .NET SDK at {DateTime.Now}";
        await queueClient.SendMessageAsync(messageContent);
        Console.WriteLine($"Sent: {messageContent}");

        // Receive a message
        var response = await queueClient.ReceiveMessageAsync();
        var message = response.Value;

        if (message != null)
        {
            Console.WriteLine($"Received: {message.MessageText}");

            // Process the message and delete it
            await queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
            Console.WriteLine("Message deleted.");
        }
    }
}
            

Python SDK

Install the package:

pip install azure-storage-queue

Example Python code:


from azure.storage.queue import QueueClient
import os
from datetime import datetime

connection_string = "YOUR_AZURE_STORAGE_CONNECTION_STRING" # Replace with your connection string
queue_name = "my-python-queue"

def main():
    # Create a QueueClient object
    queue_client = QueueClient.from_connection_string(connection_string, queue_name)

    # Ensure queue exists, create if it doesn't
    queue_client.create_queue()

    # Send a message
    message_content = f"Hello from Python SDK at {datetime.now()}"
    queue_client.send_message(message_content)
    print(f"Sent: {message_content}")

    # Receive and delete message
    messages = queue_client.receive_messages()
    for message_item in messages:
        print(f"Received: {message_item.content}")
        queue_client.delete_message(message_item.id, message_item.pop_receipt)
        print("Message deleted.")

if __name__ == "__main__":
    main()
            

Common Operations

Best Practices

Tip:

For complex scenarios involving work queues, consider Azure Service Bus Queues or Topics, which offer more advanced features like dead-lettering, sessions, and transactions.

Advanced Features

Message Time-to-Live (TTL)

You can set a TTL for messages, after which they are automatically deleted by the queue service. This is useful for transient messages.

Message Dequeue Count

The Azure SDKs often expose the dequeue count for a message. This count increases each time a message is dequeued and made visible again. You can use this to implement retry logic or move messages to a dead-letter queue after a certain number of failed attempts.

Conclusion

Azure Storage Queues provide a scalable and cost-effective messaging solution for decoupling application components. By following the guidelines and best practices outlined in this document, you can effectively leverage Storage Queues to build resilient and distributed applications on Azure.