Azure Queue Storage

Azure Queue Storage is a service that stores large numbers of messages that can be accessed from anywhere in the world via an HTTP or HTTPS connection. A single queue contains a set of messages. You can use Queue Storage to decouple application components that run at different paces.

Overview

Queue Storage is a NoSQL key-value store that is optimized for storing large amounts of unstructured data, such as text or binary data. It offers:

Key Concepts

Messages

A message is a piece of data stored in a queue. Messages can be up to 64 KB in size. Each message is represented in XML or JSON format.

Queues

A queue is a collection of messages. Queues are named and can be accessed via URIs. The maximum size of a queue is the maximum capacity of the storage account.

Queue Operations

Common operations include:

Getting Started

1. Create a Storage Account

Before you can use Queue Storage, you need to create an Azure Storage account. You can do this through the Azure portal, Azure CLI, or Azure PowerShell.

2. Accessing Queues

You can access Azure Queue Storage using:

Example: Using the Azure SDK for Python

Install the SDK:

pip install azure-storage-queue

Code example:


from azure.storage.queue import QueueClient

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

# Create a QueueClient
queue_client = QueueClient.from_connection_string(connection_string, queue_name)

# Create the queue if it doesn't exist
try:
    queue_client.create_queue()
    print(f"Queue '{queue_name}' created.")
except Exception as e:
    print(f"Queue '{queue_name}' likely already exists: {e}")

# Add a message to the queue
message_body = "Hello, Azure Queue Storage!"
queue_client.send_message(message_body)
print(f"Message sent: '{message_body}'")

# Retrieve and process messages
print("\nProcessing messages:")
messages = queue_client.receive_messages()
for msg in messages:
    print(f"  Message ID: {msg.message_id}")
    print(f"  Content: {msg.content}")
    # Delete the message after processing
    queue_client.delete_message(msg.message_id, msg.pop_receipt)
    print(f"  Message deleted.")

# Clean up the queue (optional)
# queue_client.delete_queue()
# print(f"\nQueue '{queue_name}' deleted.")
        

Best Practices

Note: Queue Storage is designed for asynchronous message queuing and is not intended for use as a message bus or for tasks requiring complex routing or publish-subscribe patterns. For those scenarios, consider Azure Service Bus.

API Reference

Queue Service REST API: https://docs.microsoft.com/en-us/rest/api/storageservices/queue-service-rest-api

Learn More