Search code examples
c#algorithmazurequeueazure-storage-queues

Implementing Delay Queue using one or more standard FIFO Queues


A delay queue is a queue in which each message has a delay time associated with it and a message can only be taken when its delay has expired. The head of the queue is that message whose delay expired furthest in the past. If no delay has expired there is no head and dequeue will return null.

Actually, I am writing a cloud application using Azure and in Azure only FIFO queues are available and not priority/delay queues. So I came here looking if someone can give me some pointers from where I can head start in the right direction. I googled a lot but only find out about the delay queues implementation in Java and no standard tutorial/research paper which talks about delay queues in general.

EDIT:

What I have code?
Actually, I have to first design this things and present it to my manager and once we finalize the design then only I can start coding.

More details about the scenario
Its a distributed application based on master/slave model. A master produce messages and put them into Azure Service Bus queues and there are multiple slaves (running on multiple machines) which read from queues and precess them. If in case a master goes down, then one of the slave acts as a master and starts producing messages. I don't want to store any state information in the master because in case master goes down all that state information will also goes with it.


Solution

  • Windows Azure Queue messages have a delay, in seconds, that you specify when inserting a message onto the queue. A message won't be visible until that timeout delay is hit. See this MSDN article to see the API details.

    The invisibility timeout is implemented in the various language SDK implementations as well. Since you're working with C#, here's what the AddMessage() call would look like. Note the 3rd parameter of AddMessage()specifies the invisibility timeout:

            var acct = CloudStorageAccount.DevelopmentStorageAccount;
            var queueClient = acct.CreateCloudQueueClient();
            var queue = queueClient.GetQueueReference("myqueue");
            queue.CreateIfNotExist();
    
            var msg = new CloudQueueMessage("test message");
            queue.AddMessage(msg, TimeSpan.FromHours(2), TimeSpan.FromMinutes(30));