.Net, Azure

Learning Azure Local Storage with me.

Azure has many different storages.  If you start Azure fabric and look at Development Storage UI you can see Blob, Queue, and Table Storage.  In addition to that Windows Azure also has SQL Azure.

image

There’s also Local Storage and that is what I want to talk about. Local Storage is a temporary file system storage area.  If you didn’t get from previous  sentence, it’s a temporary storage.  Local Storage also available to a single role instance and cannot be shared across multiple role instances.  What does it mean for you?  It means that you should not store any data that you want to be persistent for a long time.  If you upgrade a role instance or your instance dies, you will never see the data in the Local Storage again.  All important data you should store in Blob, Table Storage or SQL Azure.  Advantage of Local Storage is that it’s faster than other storage mechanisms mentioned earlier.  Blobs, Table Storage and SQL Azure are located on different servers than your Azure VM.  Local Storage is on the same VM where the role instance is running, therefore you’re going to have less latency.  I read somewhere that the maximum size of Local Storage is 20GB for a single role instance.  However, when I tried to create a Local Storage of 100GB, I was successful.

I think that now we have pretty good idea what Local Storage is.

Let’s set up Local Storage.

You can set up Local Storage by either modifying the definition file (.csdef) or by using GUI when modifying the role properties.

Set up by modifying .csdef file.

Let’s create a Local Storage named “lessonStorage” that is 20 MB.

image

We can place multiple LocaStorage elements inside LocalResources.  As you can see it’s not hard to create Local Storage by hand but it’s even easier to create it using GUI.

Set up using GUI.

Just right click on the role in your Azure project (it’s inside the Roles folder) and select Properties… or press Alt+Enter.  In the properties window select Local Storage tab.

image

As you can see you don’t need a Computer Science degree to change these values.  There’s one extra property “Clean on Role Recycle”.  By default it’s true.  When you check “Clean on Role Recycle”, the temporary data will be deleted when the role is recycled.  There are few conditions when a role can recycle:

  • The application executes RoleEnvironment.RequestRecycle() method.
  • A catastrophic failure on the server.
  • Upgrade to the application.

When you modify it by hand in definition file, you need to add cleanOnRoleRecycle attribute.

<LocalStorage name="lessonStorage" sizeInMB="20" cleanOnRoleRecycle="true" />

How to use Local Storage?

In code to get access to Local Storage we just need to use static GetLocalResource method that takes only one parameter a name of our Local Storage. 

This method returns a LocalResource object.  LocalResource class has three properties:

Property Name Description
Name The name of the local storage.  We defined it in our definition file.  In our case it’s “lessonStorage”.
MaximumSizeInMegabytes The maximum size in megabytes allocated for the local storage.  In our definition file we allocated 20 MB.
RootPath The full directory path to the local storage.

 

All of the properties above are read-only properties, we cannot set them at runtime.

We are going to create Lesson.txt file that will contain only one line “First Lesson”.  Than using Local Storage we will print content of that file in the browser.

First let’s add a label that will be the content of Lesson.txt file to our Default.aspx file:

 

<body>
    <form id="form1" runat="server">
    <div>
        Local Storage file content: <b><asp:Label ID="fileContent" runat="server" /></b>
    </div>
    </form>
</body>

 

Next we are going to put code in Page_Load method to create Lesson.txt file and than display it’s content using Local Storage.

using System;
using System.IO;
using System.Web.UI;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace LocalStorageWeb
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LocalResource myStorage = RoleEnvironment.GetLocalResource("lessonStorage");
            string filePath = Path.Combine(myStorage.RootPath, "Lesson.txt");
            File.WriteAllText(filePath, "First Lesson");

            fileContent.Text = File.ReadAllText(filePath);
        }
    }
}

In order to have access to RoleEnvironment we added using statement on line #4.

The result after execution of this code:

image

I told you almost everything I know about Local Storage.  If I missed anything or said something incorrect, please let me know so I can learn.

kick it on DotNetKicks.com

15 thoughts on “Learning Azure Local Storage with me.

  1. Can I effectively upload thousands of files to a local storage .
    my web role depend on thousands of data files, which need freqeuently read these files. I think the local storage is good in performance, but how can I upload all these file to the local storage. I mean could I access the VM using FTP ?

    1. Accessing blobs is an expensive operation (in terms of time) because they can be in a machine far away from the one actualy executing your code. So, if your application needs to do a lot of reading operations, it is recommended that you download the blobs to local storage (which is the hard drive in the machine where your code is running) only once. From there you can access the files using standard System.IO classes and methods. In my web roles, I usualy download the blobs I need when the application starts.

      1. Can you please provide an example of how to download the blob to the machine running your code?

        Thanks..

  2. I want to create a Folder structure in Local storage, something like this:
    MCA (Main Folder)
    |
    |
    First Folder (Sub Folder 1)
    |
    |
    Second Folder (Sub Folder 2)

    Is it possible?

  3. How much time did it require you to write “Learning Azure Local Storage with me.
    Vadim’s Weblog”? It boasts plenty of beneficial advice. Thank you -Zelma

  4. Oh my goodness! Awesome article dude! Thanks, However I am encountering difficulties with your RSS. I don’t understand why I am unable to subscribe to it. Is there anybody else having identical RSS issues? Anybody who knows the answer will you kindly respond? Thanks!!

Leave a comment