5

With multiple instances of an Azure Website running, how do static variables work? This is a fundamental question of not fully understanding "what" an azure website is. Take the below simplified code.

static int MyCachedInt=10;

public ActionResult SetMyCachedInt(int a)
{
    MyCachedInt = a;
    return this.Content(MyCachedInt.ToString());
}

public ActionResult GetMyCachedInt()
{
    return this.Content(MyCachedInt.ToString());
} 

On a single web-server, or a traditional virtual machine I would expect each website to either return 10, or whatever the last value was set. With Azure would all websites return the same value or would each instance be updated individually? If Azure scales up a new website, would the value be 10 or the last instance of one of the previously scaled?

Aaron Sherman
  • 3,789
  • 1
  • 30
  • 33
  • 1
    An Azure web site is just a web site running in an Application Domain on an application pool. Just as with a web farm in your company, different machines, or even different appdomains on a single web server have their own static fields. You *can* have the same code running on different application pools. It's not that Azure Web sites are different, it's the "traditional" sites that don't work the way you expected – Panagiotis Kanavos Dec 11 '14 at 14:47
  • What I am reading from you would imply if I have 5 scaled websites, I have 5 instances of my website running. Where that seems to fall apart is if I look in kudu at the processes, it shows me a single pid for my website with a single memory dump. – Aaron Sherman Dec 11 '14 at 14:52
  • No, I said that if you have 5 app domains, you have 5 instances. An application pool (which is the actual executable) can host multiple app domains, one for each site it runs. Thats the case eg with the "ASP.NET v4" pool you may have on your local machine running multiple sites. I'm not talking about Azure Web Sites at all, I'm talking about regular, local IIS sites here. – Panagiotis Kanavos Dec 11 '14 at 14:57
  • I think I am getting a bit confused, can we break this down a bit more? If I have 1 visual studio website, deployed to a Standard Azure Website, which is then scaled to 5 instances - via azure scaling, how many app pools and app domains do I have. How many instances of "MyCachedInt" exist? – Aaron Sherman Dec 11 '14 at 15:08
  • This is really about processes and threads. A static var is process-restricted, but shared between threads. If the sites are all on the same server all sharing the same process (all in the same app pool), then it's possible they may all be able to see the same static var, but in an Azure environment, especially, if you're using websites and not running a full VM instance, I wouldn't count on any of the sites even being co-located, let alone sharing anything. – Chris Pratt Dec 11 '14 at 15:25
  • @ChrisPratt static values are per AppDomain see [here](http://stackoverflow.com/questions/6562162/in-net-is-the-staticness-of-a-public-static-variable-limited-to-an-appdomain). Aaron, the point is, if I have a web farm with 10 machines or 10 VMs behind a load balancer, scaling out means adding another VM to run the same site. The same happens with an Azure Web Site. The "difference" is that eg I use a custom Octopus script while Azure uses some other script to deploy the data, start the site and update the load balancer. – Panagiotis Kanavos Dec 11 '14 at 15:32
  • 2
    @PanagiotisKanavos thanks for all of your help, I think I understand what you are trying to say, but can you make an answer (that I will say answers this question) that very specifically and explicitly says "If you have 5 instances of a scaled standard website on Azure, that means you will have XXX app domains and xxx app pools. Since static variables are specific to an app domain it means the static variable [is / is not] shared. Possibly at this point of definition this is now a Server Fault Question. – Aaron Sherman Dec 11 '14 at 17:52

0 Answers0