Ashutosh Singh

Getting Started With .NET API for Queue Storage @Azure

Before digging into details of Queue lets have briefing of the umbrella technology by Microsoft(MS) i.e. Azure Storage.

It is one of the many platform-as-a-service provided by the MS under its cloud solution Azure. Along with various storage services which are exposed through REST APIs they also have implicit characteristics such as scalability, high availability, disaster recovery etc. All these services are managed under an account in Azure storage i.e. Storage Account which can be created using your Azure subscription. This account is the entry point for all types of storage solutions provided. Inside a storage account each service is grouped under its own structure as depicted in the below image.

 

Azure-Storage-Account

A question came into my mind as I looked into this offering of MS, Why do we need these many different type of services?

Applications consume and generate data of various types. These different type of data generated by application can be broadly categorized into two types that are structured and unstructured. Structured data exhibits same attributes every time they are generated. But unstructured data don’t have a finite well defined structure, which results into different data every time it is produced. Hence structured data is easy to manage, query, aggregate, project etc. while unstructured are not.

Application generates  different types of data such as documents, images, records, logs, transactions etc. Each type of data has its own nature and should be handled in its own way. The very famous and popular choice of data storage for applications is SQL server (any relational database).

But the question is “Is it the right solution for all the types of data generated or consumed by application?”.

Answer is NO. For example, storing an image in relational database will result into performance overhead of serializing it to binary while storing it and deserializing it while fetching for display. Similarly storing images and documents on a server disk is way efficient than relational DB storage but its not scalable, not highly available and if you want to make it so you will have to do your own management which require special & dedicated skill set which is not economical.

We need specialized solution for different type of data and also ensuring their high availability, scalability and disaster recovery. Azure provides these solutions which are:

  • BLOB storage
  • Table storage
  • Queue storage
  • File storage

Queue Storage

When designing application for scale, application components are often decoupled so that individual components can be scaled. Queue provides asynchronous communication between components of application via a reliable messaging.

  • It helps creating a backlog of work to process asynchronously.
  • Passing message from Azure web role to an Azure worker role

Queues are managed under a storage account. An account can have any number of queues and a queue can have any number of messages in it. Message size has a capping and that is 64 Kb. Queues are exposed through REST APIs and can be consumed from any platform such as .NET, Node.js, Java, C++, PHP, Ruby and Python. Here we will be discussing the .NET API for Azure Queue Storage.

Steps to access Queues on Storage account
1. Setup connection string in application
2. Install Azure Storage package and dependencies using nuget package manager (Microsoft.WindowsAzure.Storage.dll)Namespace Declaration>

Code Snippet
  1. using Microsoft.WindowsAzure.Storage;
  2. using Microsoft.WindowsAzure.Storage.Auth;
  3. using Microsoft.WindowsAzure.Storage.Queue;

Important Classes

CloudStorageAccount – Represents your storage account information.

CloudQueueClient – Provides a reference to a queue

CloudQueue – Represents a queue on the cloud

CloudQueueMessage – Represents a message stored in a queue

  • Queue Operations – There are two main operations associated with a queue.

Creating a queue – CloudQueue.CreateIfNotExists

Deleting a queue – CloudQueue.Delete

Insert Message – CloudQueue.AddMessage

Peek Next Message – CloudQueue.PeekMessage

Modify a Message – CloudQueue.UpdateMessage

Delete Next Message – CloudQueue.DeleteMessage

Code Sample:

Code Snippet
  1. CloudStorageAccount accountHandler = CloudStorageAccount.Parse(“StorageConnectionString”);
  2. CloudQueueClient queueHandler = accountHandler.CreateCloudQueueClient();
  3. CloudQueue messageQueue = queueHandler.GetQueueReference(“nameofqueue”);
  4. messageQueue.CreateIfNotExists();
  5. //Enqueue
  6. CloudQueueMessage queueMessage = new CloudQueueMessage(“message”);
  7. messageQueue.AddMessage(queueMessage);
  8. //Dequeue
  9. CloudQueueMessage queueMessage = messageQueue.GetMessage(visibilityTimeout: visibilityPeriod);
  10. messageQueue.DeleteMessage(queueMessage);

References
How to use Queue Storage for .NET