Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Info

Details in Priority Queue pattern - Azure Architecture Center | Microsoft Learn

Problem

  • To handle tasks efficiently based on their priority, workloads need a mechanism to prioritize and execute tasks accordingly.

    • Workloads process tasks in the order they arrive, using a first-in, first-out (FIFO) queue structure.

Solution

  • Application sending a message to the queue assigns a priority to the message, and consumers process the messages by priority.

  • There are 2 approaches :

    • Single queue : All messages are sent to one queue and each message assigned a priority → application (producer) assigns a priority to each message and sends the message to the queue.

    • Multiple queue : Separate queues are used for each message priority.

...

Priority Queue In Practice

...

Example of code in C# : sending message as Priority Low and High

Code Block
languagec#
using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.WebJobs;

namespace PriorityQueueSender
{
    public static class PriorityQueueSenderFn
    {
        [FunctionName("PriorityQueueSenderFunction")]
        public static async Task Run(
            [TimerTrigger("0,30 * * * * *")] TimerInfo myTimer,
            [ServiceBus("messages", Connection = "ServiceBusConnection")] IAsyncCollector<ServiceBusMessage> collector )
        {
            for (int i = 0; i < 10; i++)
            {
                var messageId = Guid.NewGuid().ToString();
                var lpMessage = new ServiceBusMessage() { MessageId = messageId };
                lpMessage.ApplicationProperties["Priority"] = Priority.Low;
                lpMessage.Body = BinaryData.FromString($"Low priority message with Id: {messageId}");
                await collector.AddAsync(lpMessage);

                messageId = Guid.NewGuid().ToString();
                var hpMessage = new ServiceBusMessage() { MessageId = messageId };
                hpMessage.ApplicationProperties["Priority"] = Priority.High;
                hpMessage.Body = BinaryData.FromString($"High priority message with Id: {messageId}");
                await collector.AddAsync(hpMessage);
            }
        }
    }
}