lock(string) deadlock, and interning

In one of my applications, one of the database (Oracle Server), started responding very slowly which was causing timeout errors on the browser. I decided to cache the data, so i wrote this:

   1: HttpContext.Current.Cache.Add("dtUsers", dtUsers, null, DateTime.Now.AddDays(2), 
   2: TimeSpan.Zero,System.Web.Caching.CacheItemPriority.Normal, null);

The above code is simply putting the database into the cache.I read in so many different places that System.Web.Cache is thread safe; however still i decided to put a lock mechanism to make sure, only 1 instance can access to cache, as there is only 1 item in the cache (which is the datatable), and all other threads will be reading/writing to this. So i read about lock in ASP.NET, and i wrote the below code:

   1: DataTable dtUsers = null;
   2: if (HttpContext.Current.Cache["dtUsers"] != null)
   3:     {
   4:         return (HttpContext.Current.Cache["dtUsers"] as DataTable).Copy().DefaultView;
   5:     }
   6:     lock (CacheSyncObject)
   7:     {          
   8:         if (HttpContext.Current.Cache["dtUsers"] != null)
   9:         {           
  10:             return (HttpContext.Current.Cache["dtUsers"] as DataTable).
  11:                 Copy().DefaultView;
  12:             }
  13:             dtUsers = OnlineDirectoryAccess.GetUsers();
  14:             HttpContext.Current.Cache.Add("dtUsers", dtUsers, null, 
  15:                 DateTime.Now.AddDays(2), TimeSpan.Zero,
  16:                 System.Web.Caching.CacheItemPriority.Normal, null);
  17:             return (HttpContext.Current.Cache["dtUsers"] as DataTable).Copy().DefaultView;
  18:         }

The lock object CacheSyncObject was a string object i created as a static variable, so that simple code was:

   1: public static string CacheSyncObject = "CacheSync";

Everything was working in my local, however when i deployed the project, the web application stopped working, and got timeout errors. I put some logs to see when it hang, and apparently at the lock() statement, the system was frozen. There was a deadlock in the system, after so many reading, and asking questions in the forums, i got my answer :) and i want to share it now :)

.NET Framework uses string interning to store strings. You can see the definition of this at wikipedia. Basically for performance issues, framework is storing only 1 copy of each distinct string value in the memory. By this way, same copy of string is shared between threads and different AppDomains. This also brings the issue of not being able have too much control over the strings. For this reason we should not put locks on string objects as other threads have still access to the same memory address.

The solution was very simple:

   1: public static object CacheSyncObject = new object();


E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses


Azure? Cloud Computing?

    So what is this Azure platform? What is cloud computing? I went to Southern California Code Camp. and attended the Azure presentations, as well as i attended the user group meetings. What is the cloud? Let’s say you are web developer, and you create a website; What do you do next? Publish it to a host so that everyone in the world can access to your site right? You can think Cloud environment as a hosting environment for you but there are some differences :). Microsoft Azure is a cloud environment from Microsoft.

    One of the biggest difference is, your website will be in a datacenter that Microsoft (i am talking about Microsoft Azure when i mention Cloud in this post) owns. These datacenters (yes there is more than one geographically located around the world) are huge, and 7/24 up. These datacenters will synchronize your web application by checking the your visitors’ locations, For example if your application is in a datacenter at Southern California, and you are getting so many hits from Europe, your application will be copied to datacenter in Europe automatically to serve your visitors faster. This geo relocation will be done completely by Microsoft.
These servers will be patched by Microsoft, the security will be handled by Microsoft, uptime will be handled by Microsoft, power issues will be handled by Microsoft, ram, disk, cpu resources will be handled by Microsoft… You only have to worry about your application and your visitors. Some of you may be worried about data confidentiality; but think this, if Microsoft uses your data for their own business, and they are caught doing this; all their customers will leave them, and they will be left alone with their multi million dollar investments :).

    This cloud computing in Microsoft world is called Windows Azure platform. You can think of Microsoft Azure, as the operating system of this cloud that supports some services such as: Microsoft .NET Services (of course :)), Live services, Sql Data Services.. Here is simple picture that shows Azure and its layers:


   It is a simple topology i guess, just a virtual kinda operating system called Windows Azure, supporting some programming platforms, and you host your application here, that could be synced to multiple datacenters depending on the requests. So is this all? just a bunch of datacenters that syncs your app :) no not at all. There are more stuff in this system than just a distributed environment.

   Now lets talk about  some other advantages, If you host your web application in a host environment, after some time, if your web application gets good hits, you may want to buy a dedicated server to have more resources, and maybe later on if your business is really doing good, more servers or a web farm. However what if your application will be getting hits on some specific days such as a super bowl night, Christmas weekend or valentines day as more people will be shopping, will be buying more servers just for 10 days of the year? In the cloud environment, what you can do is ask for more ram, more machines instantly, use it, and then give it back, so you only get the resources you need, when you need it. For the ctp version, you request the resources through a configuration file, however in the release version it is said that, the system will scale the resources automatically, which means will add more cpu or ram when your application requires it (of course you will put a maximum resource limit in the configuration file).

   Now that we have this super efficient, reliable, scalable computer network out there for us, how much will Microsoft charge us? Don't know, as they haven't announced it. All i know is the price will be comparable and affordable to other Cloud network suppliers (such as Google or Amazon).

   I will download the tools to start doing some experiments, and will be posting my experiences too. Stay tuned :)


E-mail | Permalink | Trackback | Post RSSRSS comment feed 1 Responses


Socal Codecamp

azurelogo The star of this year’s codecamp was Windows Azure. So many good talks about Windows Azure was on the first day. I got the basics of it, even though i still have some doubts such as how about the sessions, sync of sessions, file backup/restore, using third party tools in the cloud such as emailer etc. I do have a small poject in my mind that my wife was the starter for this project :). I will be using Azure, Sql Data Services and Silverlight. I dont know how this will work out but we will see what i could do with these :)


E-mail | Permalink | Trackback | Post RSSRSS comment feed 0 Responses