Azure Functions with Azure Storage Queues
This article explains how to integrate Azure Functions with Azure Storage Queues to build event-driven applications. Storage Queues provide a simple, scalable messaging solution for decoupling application components.
Introduction to Azure Storage Queues
Azure Storage Queues are a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated HTTP calls. A queue belongs to a storage account. A queue can contain an unlimited number of messages. However, there is a limit to the total size of messages that can be contained in the queue.
Using the Queue Trigger
The Queue trigger allows your function to run in response to a message appearing in a Storage Queue. When a message is received, the trigger invokes your function.
Example: Queue Trigger in C#
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
public static class QueueTriggerFunction
{
[FunctionName("QueueTriggerCSharp")]
public static void Run(
[QueueTrigger("myqueue-items")] string myQueueItem,
ILogger log)
{
log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
}
}
Example: Queue Trigger in JavaScript
module.exports = async function (context, myQueueItem) {
context.log('JavaScript queue trigger function processed work item', myQueueItem);
};
Using the Queue Output Binding
The Queue output binding allows your function to send messages to an Azure Storage Queue. This is useful for sending work items to be processed by other functions or services.
Example: Queue Output Binding in C#
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
public static class QueueOutputFunction
{
[FunctionName("QueueOutputCSharp")]
public static void Run(
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
[Queue("outputqueue", Connection = "AzureWebJobsStorage")] out string message,
ILogger log)
{
message = $"New message generated at: {myTimer.ScheduleStatus.Next}";
log.LogInformation($"C# function sent a message to the output queue: {message}");
}
}
Example: Queue Output Binding in JavaScript
module.exports = async function (context, timer) {
const timeStamp = new Date().toISOString();
context.log('JavaScript is running function at timestamp:', timeStamp);
context.bindings.message = `New message generated at: ${timeStamp}`;
context.log(`JavaScript function sent a message to the output queue: ${context.bindings.message}`);
};
local.settings.json
or application settings in Azure have the correct connection string for your storage account, typically named AzureWebJobsStorage
.
Best Practices
- Idempotency: Design your functions to be idempotent, as messages might be processed more than once in certain failure scenarios.
- Error Handling: Implement robust error handling and consider using poison queues for messages that repeatedly fail processing.
- Message Size: Be mindful of the 64KB message size limit for Azure Storage Queues. For larger data, consider storing data in Blob Storage and passing a reference in the queue message.
- Batching: For high-throughput scenarios, consider using the `Microsoft.Azure.WebJobs.Extensions.Storage.Queues` package for more advanced queue interactions and potential batch processing.

Conceptual diagram showing Azure Functions triggering on Storage Queue messages.