Azure Storage Queue Quickstart

This guide will walk you through the essential steps to get started with Azure Storage Queue, a service that enables you to reliably store and retrieve large numbers of messages.

What is Azure Storage Queue?

Azure Queue Storage is a service that lets you store large numbers of unreliable messages to be processed asynchronously. Each message in the queue is typically JSON or XML formatted and can be up to 64 KB in size. Queue Storage is useful for decoupling application components that are developed and timed independently.

Prerequisites

Step 1: Create a Storage Queue

You can create a queue using the Azure Portal, Azure CLI, or programmatically. Here's how to do it with the Azure CLI:

az storage queue create \
    --name "myqueue" \
    --account-name "mystorageaccount" \
    --account-key "your_storage_account_key"

Replace "myqueue" with your desired queue name and "mystorageaccount" and "your_storage_account_key" with your storage account name and primary access key. You can find your storage account key in the Azure Portal under your storage account's "Access keys" section.

Step 2: Add Messages to the Queue

Messages are added to a queue using the message put command. Each message can be a simple string or a JSON object.

az storage message put \
    --queue-name "myqueue" \
    --account-name "mystorageaccount" \
    --account-key "your_storage_account_key" \
    --content "This is my first message."

You can add multiple messages by running the command multiple times or by specifying multiple messages with the --content parameter (separated by newlines).

Step 3: Peek at Messages

To view the front message without removing it from the queue, use the message peek command.

az storage message peek \
    --queue-name "myqueue" \
    --account-name "mystorageaccount" \
    --account-key "your_storage_account_key"

Step 4: Get and Delete Messages

To retrieve a message from the queue and make it invisible for a specified period (the visibility timeout), use the message show command. After processing, you can delete it.

# Get the message (sets visibility timeout to 30 seconds by default)
az storage message show \
    --queue-name "myqueue" \
    --account-name "mystorageaccount" \
    --account-key "your_storage_account_key"

# Assuming you got the message ID and pop receipt from the above command
az storage message delete \
    --queue-name "myqueue" \
    --account-name "mystorageaccount" \
    --account-key "your_storage_account_key" \
    --ids "your_message_id" \
    --pop-receipt "your_pop_receipt"

The message show command returns the message ID and pop receipt, which are required for the message delete command.

Important Note:

If your application crashes or fails to process a message within the visibility timeout, the message will become visible again in the queue and can be retrieved by another worker. This ensures message durability.

Step 5: Clear the Queue

To remove all messages from the queue, use the queue clear command.

az storage queue clear \
    --name "myqueue" \
    --account-name "mystorageaccount" \
    --account-key "your_storage_account_key"

Step 6: Delete the Queue

When you no longer need the queue, you can delete it.

az storage queue delete \
    --name "myqueue" \
    --account-name "mystorageaccount" \
    --account-key "your_storage_account_key"

Pro Tip:

For production scenarios, consider using SDKs (e.g., Python, .NET, Java) for more robust and feature-rich interactions with Azure Storage Queues. This provides better error handling, retry logic, and integration with your application's services.

Next Steps

Congratulations! You have successfully completed the Azure Storage Queue quickstart.