0
0
Azurecloud~5 mins

Queue storage basics in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Queues help apps talk to each other by storing messages safely until the other app is ready to use them. Azure Queue Storage is a simple way to keep these messages in the cloud so apps can work smoothly even if one is busy or offline.
When you want to send a task from a web app to a background worker without losing the task if the worker is busy.
When you need to keep messages safe until another app can process them later.
When you want to balance work between several workers by letting them pull tasks from the same queue.
When you want to decouple parts of your app so they don’t have to run at the same time.
When you want to retry processing a message if it fails the first time.
Config File - queue_policy.json
queue_policy.json
{
  "Version": "2023-06-01",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "Microsoft.Storage/storageAccounts/queueServices/queues/messages/read",
        "Microsoft.Storage/storageAccounts/queueServices/queues/messages/add"
      ],
      "Resource": "https://examplestorageaccount.queue.core.windows.net/myqueue/messages"
    }
  ]
}

This JSON file defines a simple access policy for the Azure Queue Storage. It allows reading and adding messages to the queue named myqueue in the storage account examplestorageaccount. This controls who can send or receive messages.

Commands
This command creates a new queue called 'myqueue' in the Azure storage account named 'examplestorageaccount'. We use the account key to authenticate. The output is shown in a table format for easy reading.
Terminal
az storage queue create --name myqueue --account-name examplestorageaccount --account-key Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUz6z5r5+3k= --output table
Expected OutputExpected
Name ------ myqueue
--name - Specifies the name of the queue to create.
--account-name - Specifies the Azure storage account name.
--account-key - Provides the key to access the storage account.
This command adds a message with the text 'Hello, Azure Queue!' to the 'myqueue' queue. This simulates sending a task or information to be processed later.
Terminal
az storage message put --queue-name myqueue --content "Hello, Azure Queue!" --account-name examplestorageaccount --account-key Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUz6z5r5+3k=
Expected OutputExpected
{"messageId":"12345678-1234-1234-1234-123456789abc","popReceipt":"AgAAAAMAAAAAAAAAA==","timeNextVisible":"2024-06-01T12:00:00Z"}
--queue-name - Specifies which queue to add the message to.
--content - The actual message text to store in the queue.
This command retrieves one message from the 'myqueue' queue so it can be processed. The message remains in the queue but is hidden temporarily to avoid duplicate processing.
Terminal
az storage message get --queue-name myqueue --account-name examplestorageaccount --account-key Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUz6z5r5+3k=
Expected OutputExpected
[{"messageId":"12345678-1234-1234-1234-123456789abc","popReceipt":"AgAAAAMAAAAAAAAAA==","messageText":"Hello, Azure Queue!"}]
--queue-name - Specifies which queue to read the message from.
This command deletes the message from the queue after it has been processed successfully. It uses the message ID and pop receipt to identify the exact message to remove.
Terminal
az storage message delete --queue-name myqueue --id 12345678-1234-1234-1234-123456789abc --pop-receipt AgAAAAMAAAAAAAAAA== --account-name examplestorageaccount --account-key Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUz6z5r5+3k=
Expected OutputExpected
No output (command runs silently)
--id - The unique ID of the message to delete.
--pop-receipt - A token proving the message was retrieved and can be deleted.
Key Concept

If you remember nothing else from this pattern, remember: Azure Queue Storage safely holds messages until your app is ready to process them, helping apps work smoothly together.

Common Mistakes
Trying to read a message without deleting it afterward.
The message stays hidden temporarily but reappears later, causing duplicate processing.
Always delete the message after processing it successfully using its ID and pop receipt.
Using wrong or expired account keys in commands.
Authentication fails and commands do not work.
Use the correct and current storage account key for all commands.
Creating queues with invalid names or special characters.
Azure rejects queue names that do not follow naming rules.
Use only lowercase letters, numbers, and hyphens for queue names.
Summary
Create a queue to hold messages using 'az storage queue create'.
Add messages to the queue with 'az storage message put' to send tasks or info.
Retrieve messages with 'az storage message get' to process them safely.
Delete messages after processing with 'az storage message delete' to avoid repeats.