ovolko ovolko - 2 months ago 6x
ASP.NET (C#) Question

How to share state between Azure instances?

I want to use a global data for MVC web application running on Windows Azure (e.g. something like a list of users having new messages).

For a normal webapp, I could use some per-appdomain storage like AppDomain.SetData or just static variable. What should I use for Azure instead (cache? blob storage? queues?) and what solution would be the fastest one?


AppFabric Cache is an excellent fit for sharing data between roles (or instances of the same role). The interesting thing about AppFabric Cache is that it doesn't apply just to ASP.NET Session State - there just happens to be an out-of-the-box ASP.NET Session State Provider that sits atop the cache.

Using the cache is nearly trivial. Here's a snippet from a command-line console app demo:

var dataCacheFactory = new DataCacheFactory();
DataCache dataCache = dataCacheFactory.GetDefaultCache();

Console.Write("Enter a string to cache: ");
string value = Console.ReadLine();

dataCache.Put("key", value);

string response = (string)dataCache.Get("key");
Console.WriteLine("Cached string: " + response);

Using it as a Session State provider requires zero code change - it's all driven by the app.config / web.config.

vtortola makes a good point about the AppFabric Cache being in CTP, but we should see that in production in the near-term.

Table Storage will work as well, depending on the complexity of your queries.It sounds like your queries would be relatively straightforward.

Since pricing hasn't been announced yet for AppFabric Cache, this could factor into your decision vs., say, Table Storage which runs $0.15 / GB plus related transactions (although transactions won't likely have any noticeable impact on your cost, at $0.01 per 10,000 transactions).

EDIT June 7, 2012 Pricing info has changed since original answer:

  • The Cache service is in production, and starts at $45 for 128MB (full pricing details here).
  • Transactions are now $0.01 per 100,000 transactions, with storage starting at $0.125 per GB and dropping based on quantity (see here for details).
  • There's now a new Cache capability that may be enabled within your Web or Worker role instances, using a percentage of available RAM, and costing ZERO. You may also create an independent Cache Role. Both of these are supported with the brand new SDK v1.7. Scott Guthrie blogged about the new features (including Cache).