8
votes

I understand that MS Azure Queue service document http://msdn.microsoft.com/en-us/library/windowsazure/dd179363.aspx says first out (FIFO) behavior is not guaranteed.

However, our application is such that ALL the messages have to be read and processed in FIFO order. Could anyone please suggest how to achieve a guaranteed FIFO using Azure Queue Service?

Thank you.

7

7 Answers

6
votes

The docs say for Azure Storage queues that:

Messages in Storage queues are typically first-in-first-out, but sometimes they can be out of order; for example, when a message's visibility timeout duration expires (for example, as a result of a client application crashing during processing). When the visibility timeout expires, the message becomes visible again on the queue for another worker to dequeue it. At that point, the newly visible message might be placed in the queue (to be dequeued again) after a message that was originally enqueued after it.

Maybe that is good enough for you? Else use Service bus.

5
votes

The latest Service Bus release offers reliable messaging queuing: Queues, topics and subscriptions

1
votes
0
votes

You just need to follow below steps to ensure Message ordering.:

1) Create a Queue with session enabled=false. 2) While saving message in the queue, provide the session id like below:-

var message = new BrokeredMessage(item);
message.SessionId = "LB";
Console.WriteLine("Response from Central Scoring System : " + item);
client.Send(message);

3) While creating receiver for reviving message:-

queueClient.OnMessage(s =>
{
    var body = s.GetBody<string>();
    var messageId = s.MessageId;
    Console.WriteLine("Message Body:" + body);
    Console.WriteLine("Message Id:" + messageId);
});

4) While having the same session id, it would automatically ensure order and give the ordered message.

Thanks!!

0
votes

I don't know how fast do you want to process the messages, but if you need to have a real FIFO, don't allow Azure's queue to get more than one message at a time.

Use this at your "program.cs" at the top of the function.

    static void Main()
            {
                var config = new JobHostConfiguration();

                if (config.IsDevelopment)
                {
                    config.UseDevelopmentSettings();
                }
                config.Queues.BatchSize = 1; //Number of messages to dequeue at the same time.
                config.Queues.MaxPollingInterval = TimeSpan.FromMilliseconds(100); //Pooling request to the queue.


                JobHost host = new JobHost(config);

....your initial information...

            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();

This will get one message at a time with a wait period of 100 miliseconds.

This is working perfectly with a logger webjob to write to files the traze information.

0
votes

Unfortunately, many answers misleads to Service Bus Queues but I assume the question is about Storage Queues from the tags mentioned. In Azure Storage Queues, FIFO is not guranteed, whereas in Service Bus, FIFO message ordering is guaranteed and that too, only with the use of a concept called Sessions.

A simple scenario could be, if any consumer receives a message from the queue, it is not visible to you when you are the second receiver. So you assume the second message you received is actually the first message (Where FIFO failed :P)

Consider using Service Bus if this is not your requirement.

-1
votes

As mentioned here https://www.jayway.com/2013/12/20/message-ordering-on-windows-azure-service-bus-queues/ ordering is not guaranteed also in service bus, except of using recieve and delete mode which is risky