Are Azure Static Class Variables shared across role instance?

No. Static class variables behave as they should, with it’s value shared only within the instance it is instantiated in.  Thus, static variables should not be used to synchronise or provide locks between role instances.

To confirm this behaviour, I created a simple web role with a button that sets a static class variable. A timer periodically triggers to attempt to retrieve the variable to displays it.  The web role then deployed with at least 2 or more instances.

From the screen below, we can see that each web role instance read a different GUID value from the GUID static class variable.

image

However, access to the same web role instance read back the same GUID value.

image

Hit the link below for the source files if you want to test it out.

http://cid-78528331d9c95bbb.office.live.com/embedicon.aspx/Blog%20Attachment/Blog20110203_AzureStaticVariableTest.zip

 

Hope this is remotely useful for some other Azure beginner like me Smile.

Advertisements

2 responses to “Are Azure Static Class Variables shared across role instance?

  1. more interesting would be a solution to solve this problem. I’m working on a lock mechanism between role instances, so only one role does the job.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s