Azure Functions Intermediate

Queue and Service Bus triggers

Queues let one part of a system hand work to another without waiting. Azure Functions can be triggered by Azure Storage queues for simple jobs and by Azure Service Bus when you need topics, sessions, dead-lettering and larger messages.

A queue message from send to done
  1. StartProducer sends messageAn API adds OrderPlaced to the orders queue.
  2. Step 2Trigger picks it upThe host receives the message and runs the function.
  3. Step 3SuccessThe message is deleted or completed.
  4. Step 4FailureThe message returns to the queue and is retried.
  5. EndToo many failuresIt moves to the poison or dead-letter queue.

Delivery is at least once, so design handlers that are safe to repeat.

A Storage queue trigger

The function runs once per message. If it throws, the message becomes visible again and is retried. After the dequeue count passes maxDequeueCount (5 by default), the message moves to a queue named <queue>-poison.

C#
public record OrderPlaced(int OrderId, string Email);

public class SendReceipt(IEmailSender email, ILogger<SendReceipt> logger)
{
    [Function("SendReceipt")]
    public async Task Run(
        [QueueTrigger("orders", Connection = "Storage")] OrderPlaced message,
        CancellationToken ct)
    {
        logger.LogInformation("Sending receipt for order {OrderId}", message.OrderId);
        await email.SendReceiptAsync(message.OrderId, message.Email, ct);
    }
}

A Service Bus trigger

Service Bus adds topics and subscriptions, sessions for ordered processing, scheduled messages and a built-in dead-letter queue. A message is dead-lettered after the entity's max delivery count (10 by default) or when your code dead-letters it on purpose.

C#
[Function("HandlePayment")]
public async Task Run(
    [ServiceBusTrigger("payments", Connection = "ServiceBus")] ServiceBusReceivedMessage message,
    ServiceBusMessageActions actions)
{
    var payment = message.Body.ToObjectFromJson<Payment>();
    if (payment.Amount <= 0)
    {
        await actions.DeadLetterMessageAsync(message, deadLetterReason: "InvalidAmount");
        return;
    }
    await _payments.ApplyAsync(payment);
    // Completed automatically when the function returns without an exception.
}

Which one to use

  • Storage queues: cheap and simple, messages up to 64 KB, huge queues, at-least-once delivery. Good for background jobs.
  • Service Bus: enterprise messaging with topics, sessions (FIFO per session), duplicate detection, transactions and larger messages.
  • Both deliver at least once, so a message can be processed twice. Make handlers idempotent, for example by recording processed message ids.
  • Tune concurrency in host.json: batchSize for queues, maxConcurrentCalls for Service Bus.

Interview question

What is a poison message and how does Azure Functions handle it?

A poison message is one that keeps failing, for example because its data is invalid. For Storage queues, Functions retries it until the dequeue count reaches maxDequeueCount, 5 by default, and then moves it to the <queue>-poison queue. Service Bus moves it to the dead-letter queue after the max delivery count. I monitor those queues and alert on them, fix the cause, and replay the messages.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.