Queue Trigger Fundamentals

This tutorial explores the fundamentals of using queue triggers in Microsoft Azure Functions. Queue triggers are an essential component for building event-driven applications that react to messages placed on a queue.

What are Queue Triggers?

A queue trigger allows your Azure Function to execute automatically whenever a new message appears in a specified Azure Queue Storage queue. This is a powerful pattern for decoupling services and processing tasks asynchronously.

When to Use Queue Triggers

Creating a Queue Triggered Function

Let's create a simple C# function that logs the content of a queue message.

Prerequisites

Function Code (C#)

Create a new Azure Function project and add a queue-triggered function. The essential parts are the attribute that defines the trigger and the function signature.

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace QueueTriggerExample
{
    public static class QueueTriggerFunction
    {
        [FunctionName("ProcessQueueMessage")]
        public static void Run(
            [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem,
            ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }
    }
}
Explanation:
  • [FunctionName("ProcessQueueMessage")]: Defines the name of the function.
  • [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string myQueueItem: This is the core of the queue trigger.
    • "myqueue-items": Specifies the name of the Azure Queue Storage queue to monitor.
    • Connection = "AzureWebJobsStorage": Indicates that the connection string for the queue is stored in the application setting named AzureWebJobsStorage. This setting typically points to your Azure Storage account.
    • string myQueueItem: The message content from the queue will be passed into this parameter as a string. You can also use other types like POCOs (Plain Old CLR Objects) if you have a JSON payload.
  • ILogger log: Provides access to logging capabilities.

Configuration

Ensure you have a local.settings.json file (for local development) or an application setting in Azure (for deployed functions) named AzureWebJobsStorage that contains the connection string to your Azure Storage account.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "AccountName=your_storage_account_name;AccountKey=your_storage_account_key;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

Message Handling and Processing

When a message is successfully processed by the function, it is automatically removed from the queue. If an unhandled exception occurs during processing, the message will remain in the queue and may be retried (depending on queue and function configuration).

Poison Messages

If a message consistently causes a function to fail, it's considered a "poison message." Azure Functions have built-in retry mechanisms, but to prevent infinite loops, messages will eventually be moved to a special "poison queue" or handled according to configured policies.

Advanced Scenarios

Processing Complex Data Types

You can bind queue messages to complex types (like C# classes) if your messages are in JSON format. The Functions runtime will automatically deserialize the JSON into your object.

Output Bindings

Queue triggers are often used in conjunction with other bindings, such as output bindings to send results to another queue, database, or service bus.

Example: A common pattern is to have a queue trigger process an image upload request, and then use an output binding to write a processed image metadata record to a database.

Conclusion

Queue triggers are a fundamental building block for scalable, event-driven applications on Azure Functions. By understanding how to configure and use them effectively, you can build robust and efficient serverless solutions.

For more advanced configurations and details, please refer to the Azure Functions Bindings Reference.