Bird
Raised Fist0
Azurecloud~5 mins

Queue storage basics in Azure - Commands & Configuration

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of Azure Queue Storage?
easy
A. To store messages that help different parts of an app communicate
B. To store large files like videos and images
C. To run virtual machines in the cloud
D. To create databases for storing user data

Solution

  1. Step 1: Understand the role of Azure Queue Storage

    Azure Queue Storage is designed to hold messages temporarily so different parts of an application can send and receive information reliably.
  2. Step 2: Compare options with this purpose

    Options B, C, and D describe other Azure services like Blob Storage, Virtual Machines, and Databases, which are not for message passing.
  3. Final Answer:

    To store messages that help different parts of an app communicate -> Option A
  4. Quick Check:

    Queue Storage = Message storage for app communication [OK]
Hint: Queue Storage is for messages, not files or VMs [OK]
Common Mistakes:
  • Confusing queue storage with blob storage
  • Thinking queue storage runs code or VMs
  • Mixing queue storage with database services
2. Which of the following is the correct way to add a message to an Azure queue using Azure SDK syntax?
easy
A. queueClient.addMessage('Hello World')
B. queueClient.sendMessage('Hello World')
C. queueClient.enqueueMessage('Hello World')
D. queueClient.pushMessage('Hello World')

Solution

  1. Step 1: Recall Azure Queue Storage SDK method for adding messages

    The correct method to add a message to a queue is sendMessage.
  2. Step 2: Check other options for correctness

    Methods like addMessage, enqueueMessage, and pushMessage are not valid Azure SDK methods for queues.
  3. Final Answer:

    queueClient.sendMessage('Hello World') -> Option B
  4. Quick Check:

    Send message = sendMessage() method [OK]
Hint: Use sendMessage() to add messages to Azure queues [OK]
Common Mistakes:
  • Using incorrect method names like addMessage or pushMessage
  • Confusing queue methods with blob storage methods
  • Forgetting to call the method on the queue client object
3. Given this code snippet, what will be the output after reading the message?
const { QueueClient } = require('@azure/storage-queue');
const queueClient = new QueueClient('https://myaccount.queue.core.windows.net/myqueue', credential);

async function readMessage() {
  const response = await queueClient.receiveMessages();
  if (response.receivedMessageItems.length > 0) {
    return response.receivedMessageItems[0].messageText;
  } else {
    return 'No messages';
  }
}

readMessage().then(console.log);
medium
A. The text of the first message in the queue
B. An error because receiveMessages() does not exist
C. No messages if the queue is empty
D. The number of messages in the queue

Solution

  1. Step 1: Understand receiveMessages() behavior

    The receiveMessages() method fetches messages from the queue. If messages exist, they are in receivedMessageItems.
  2. Step 2: Analyze the code logic

    If there is at least one message, the code returns the text of the first message. Otherwise, it returns 'No messages'.
  3. Final Answer:

    The text of the first message in the queue -> Option A
  4. Quick Check:

    receiveMessages() returns messages array [OK]
Hint: receiveMessages() returns messages array, pick first text [OK]
Common Mistakes:
  • Thinking receiveMessages() returns count or error
  • Ignoring the check for empty message list
  • Confusing method names or properties
4. You wrote this code to delete a message from an Azure queue but it fails:
const response = await queueClient.receiveMessages();
const message = response.receivedMessageItems[0];
await queueClient.deleteMessage(message.messageId);

What is missing to fix the error?
medium
A. You should use removeMessage() instead of deleteMessage()
B. You must call sendMessage() before deleteMessage()
C. You need to provide the message's popReceipt along with messageId to delete
D. No fix needed; the code is correct

Solution

  1. Step 1: Understand deleteMessage() requirements

    To delete a message, Azure requires both the messageId and the popReceipt which proves you received the message.
  2. Step 2: Identify missing parameter in code

    The code only passes messageId but misses popReceipt, causing failure.
  3. Final Answer:

    You need to provide the message's popReceipt along with messageId to delete -> Option C
  4. Quick Check:

    Delete needs messageId + popReceipt [OK]
Hint: Delete message requires popReceipt with messageId [OK]
Common Mistakes:
  • Passing only messageId to deleteMessage()
  • Using wrong method name like removeMessage()
  • Assuming sendMessage() is needed before delete
5. You want to build a system where multiple workers process messages from an Azure queue without processing the same message twice. Which approach best achieves this?
hard
A. Workers use peekMessages() to read messages and delete them immediately
B. Workers read messages but never delete them, so messages stay in the queue
C. Workers delete messages before processing to avoid duplicates
D. Each worker calls receiveMessages(), processes the message, then deletes it using messageId and popReceipt

Solution

  1. Step 1: Understand message processing with Azure queues

    To avoid duplicate processing, workers must receive messages, process them, then delete them to remove from the queue.
  2. Step 2: Evaluate each option

    Each worker calls receiveMessages(), processes the message, then deletes it using messageId and popReceipt correctly describes the receive-process-delete cycle with required parameters. Workers read messages but never delete them, so messages stay in the queue leaves messages undeleted causing duplicates. Workers delete messages before processing to avoid duplicates deletes before processing risking lost messages. Workers use peekMessages() to read messages and delete them immediately uses peekMessages() which does not lock or remove messages, so duplicates occur.
  3. Final Answer:

    Each worker calls receiveMessages(), processes the message, then deletes it using messageId and popReceipt -> Option D
  4. Quick Check:

    Receive, process, then delete to avoid duplicates [OK]
Hint: Process then delete messages to prevent duplicates [OK]
Common Mistakes:
  • Deleting messages before processing
  • Using peekMessages() for processing
  • Not deleting messages after processing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard