Thursday, December 9, 2010

HttpRunTime.Cache & ReaderWriterLock


Yesterday there we had a big problem with one of our client website. It was using 100% CPU and chewing all the memory it can get. Yes it is a retail website (a lot of visitors visiting the website every second). However it still shouldn’t bring all other website in the server down and unresponsive.

After some investigation I found out that the cache manager (where we use HttpRunTime.Cache) uses a lock during insert and remove item method call. It should use a lock because there will be multiple request happening at the same time. If we don’t use any lock some of the data returned to the browser might be corrupted.

However because we were using a normal lock what happen was all the request queuing to get a slice of time in the cache even when it was only trying to read. As more visitors visiting the website the longer the queue and the more memory they use.

I remember I used ReaderWriterLock in the past and I found it very useful especially in the situation like this. So I decided to change the lock to use ReaderWriterLock. And wallah it worked like magic and instantaneous drop in the memory usage and CPU time.

So this is my first tip: Cache & ReaderWriterLock are best friends J
Have a good day.

No comments:

Post a Comment