Written by Altınok Darıcı on 16 - Dec - 2014

Azure Storage - Queue

Azure Storage service contains three different storage types. In this post I will mention the Queue Storage.

A storage account contains several queues, and a queue contains number of messages. Queue storage provides communication between components of your solution . As you know, components of scalable application are decoupled, so we can scale these components independently. Queue storage provides reliable messaging between components in asynchronous manner. These components can run on Azure or Desktop or any other computer/server or even on mobile devices. Also queue structure help us to create a process workflows.

Lets start with how to use storage queue client API for .net

Connect to the Queue

// Retrieve storage account from connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

// Create the queue client
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

// Retrieve a reference to a queue
CloudQueue queue = queueClient.GetQueueReference("myqueue");

// Create the queue if it doesn't already exist
queue.CreateIfNotExists();

As you see at above code sample, it is very straightforward.
First, retrieve your storage account from connection string, Then create a queue client. Get the reference of specified queue. You can use CreateIfNotExists() method to ensure that whether queue is exist or not. If queue is not exist, it will create specified queue.

Insert Message into Queue

CloudQueueMessage message = new CloudQueueMessage("First Message");
queue.AddMessage(message);

 Peek Message: only see the message.

// Peek at the next message
CloudQueueMessage peekedMessage = queue.PeekMessage();

// Display message.
Console.WriteLine(peekedMessage.AsString);

Get and Delete the Message (Dequeue)

// Get the next message
CloudQueueMessage retrievedMessage = queue.GetMessage();

//Process the message in less than 30 seconds, and then delete the message
queue.DeleteMessage(retrievedMessage);

There is an important point here, if you get the message using GetMessage() method, message will disappear from the queue immediately ( the key point is Disappear, not deleted ). The default time out is 30 seconds. this means that after 30 seconds from you call GetMessage() function, message will appear in the queue again. So you should call DeleteMessage() method to delete message from the queue permanently.

Limitations & Details

  • Message size: up to 64 KB
  • Lease/Lock duration : 7 days maximum (30 second default)
  • Maximum queue size: 200 TB
  • Maximum message TTL: 7 days.
  • Maximum throughput: Up to 2,000 messages per second


 

comments powered by Disqus