As you can probably agree, very few applications, short of “Hello World”, do not have some form of storage. The use of storage can range from core repositories to scratch disks, but regardless of the use it typically plays a critical part in the overall architecture of the application. With the importance of storage in mind Windows Azure provides you with a lot of options ranging from strongly typed relational through to transient unstructured. For the purposes of this post we will focus on local storage for transient unstructured data.
If you have worked in or read on Windows Azure compute you will hear the common principle of “do not store things locally and remain stateless”. This is a key part of being horizontally scalable, ensuring that the consumer can move between instances in the same role without ever having an expectation that state or data already exists on the instance. This principle holds true but remember there are specific pieces of data that can be reconstructed when required, that data is the target of local storage. Some examples of this transient data include temporary files, folders and caches.
Local Storage is a directory on the instance that is created for you and you can access as you would any other directory or file. To setup a Local Storage directory you leverage the property sheets in the Cloud Project’s Local Storage tab. Defining the storage is as simple as adding a name that is meaningful to your application, the size in MB that you require and whether you want the storage to always be cleared out when the instance recycles. I personally default to selecting the “Clean on role recycle” option so I return to a known state each time. In the event that you have a large cache etc. that you would have to rebuild on recycle you may opt to leverage some form of time stamp or hash to ensure it is all up to date, rather than incurring the reconstruction cost.
When the role is deployed the provisioning process will ensure that a directory is available for each of the various entries in the Local Storage tab and set the ACL’s appropriately.
As you can see in the code sample above to get the local path we request it from the RoleEnvironment object that is found in the Microsoft.WindowsAzure.ServiceRuntime namespace. Once you retrieve the directory you access it the same way you would access your local file system.
In fact the storage is nothing more than a directory created on your local instance as you see above.
When selecting the size of the Local Storage you must remember to select a Virtual Machine that has enough local storage capacity.