Azure Queue Storage Documentation

Introduction

Azure Queue Storage is a service that stores large numbers of messages that can be accessed from anywhere in the world via HTTP or HTTPS. A queue is a collection of messages, and each message can be up to 64 KB in size. Queue Storage is typically used to build applications that operate asynchronously. Scenarios include:

  • Decoupling application components.
  • Scaling web applications.
  • Processing background tasks.
  • Distributing work to multiple workers.

Key Concepts

Messages

A message is a piece of data that can be stored in a queue. Messages are typically in the form of a string, but can be any sequence of UTF-8 bytes up to 64 KB.

Queues

A queue is a collection of messages. The order in which messages are added to a queue is the order in which they are dequeued. A queue name must be DNS-compliant and must be between 3 and 63 characters long, inclusive. It can contain lowercase letters, numbers, and hyphens. It must start and end with a letter or number. It cannot contain consecutive hyphens.

Dequeueing and Visibility Timeout

When a message is dequeued, it is not immediately deleted. Instead, it becomes invisible to other clients for a specified period, known as the visibility timeout. If the client successfully processes the message within this timeout, it can delete the message. Otherwise, the message becomes visible again and can be dequeued by another client.

Getting Started

To get started with Azure Queue Storage, you'll need an Azure account and a Storage Account. You can create these through the Azure portal.

Create a Queue

You can create a queue using the Azure portal, Azure CLI, Azure PowerShell, or using one of the Azure Storage client libraries.

Azure CLI Example:

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

Core Operations

Azure Queue Storage supports several core operations:

  • Add Message: Adds a new message to the back of a queue.
  • Get Messages: Retrieves one or more messages from the front of a queue.
  • Peek Messages: Retrieves one or more messages from the front of a queue without making them invisible.
  • Update Message: Updates the visibility timeout and dequeues count of a specific message.
  • Delete Message: Deletes a specific message from the queue.
  • Clear Queue: Deletes all messages from a queue.
  • Get Queue Metadata: Retrieves metadata about a queue, including the approximate number of messages.
  • Delete Queue: Deletes a queue and all the messages it contains.

Adding a Message (Conceptual):

Imagine a web application that needs to process images. Instead of processing them immediately, it can add a message containing the image URL to a queue. A separate worker process can then pick up these messages and process them asynchronously.

// Pseudocode using an SDK
queueClient.sendMessage("https://example.com/images/image1.jpg");

Processing a Message (Conceptual):

A worker process retrieves messages, processes them, and then deletes them once completed.

// Pseudocode using an SDK
messages = queueClient.receiveMessages(numberOfMessages: 5, visibilityTimeout: 30);
for message in messages:
    processImage(message.content);
    queueClient.deleteMessage(message.id, message.popReceipt);

Best Practices

  • Idempotency: Design your worker processes to be idempotent, meaning that processing the same message multiple times has the same effect as processing it once. This is crucial as messages might be delivered more than once.
  • Visibility Timeout: Set an appropriate visibility timeout. If it's too short, messages might be processed by multiple workers. If it's too long, a failed worker might hold onto messages unnecessarily.
  • Dead-Letter Queues: Consider using Azure Service Bus dead-letter queues for messages that fail processing repeatedly.
  • Message Size: Keep messages as small as possible. For larger data, store it in Blob Storage and pass a reference in the queue message.
  • Error Handling: Implement robust error handling in your worker processes.

API Reference

Azure Queue Storage exposes a REST API, and client libraries are available for various languages.

POST Add Message

/web?comp=append&messageid={messageid}&popreceipt={popreceipt}

Adds a new message to the queue.

Request Body: XML or JSON containing the message content.

GET Get Messages

/web?comp=get&numofmessages={numofmessages}&visibilitytimeout={visibilitytimeout}

Retrieves one or more messages from the queue.

DELETE Delete Message

/web?comp=delete&messageid={messageid}&popreceipt={popreceipt}

Deletes a specific message.