Scott Hanselman

The ASP.NET Cache object in a Web Gardening scenario with IIS5 or IIS6

August 18, '04 Comments [3] Posted in ASP.NET
Sponsored By

Remember that "Web Gardening" is like having a tiny Web Farm on a multi-proc box.  Each processor gets an ASP.NET Worker Process with affinity (that means the process "sticks") to that processor.

But what happens if you use the Cache object?  Well, each AppDomain gets a Cache object, and each ASP.NET Worker Process has one AppDomain per Web Site, and each processor gets one WP, so...if you have four processors, the Cache object will exist four times.

That means, ASP.NET Cache state is NOT shared amongst processors in a single-box multi-proc system.

Make sure you grok this if you ever find yourself working on a single box web site with multiple procs and web gardening on.  If you've:

  • coded your site to assume that the cache will fill once and only once per machine
  • used physical files as CacheDependancies to key off the cache
  • assumed that any one user will magically return to the same process on the same machine on subsequent requests

then things may well behave differently than you thinkin a web gardening scenario.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Wednesday, August 18, 2004 9:22:59 AM UTC
Well put, I blogged something similar, but yours is much more eloquently put.

I did remark that the setting for this is far to simple, that there was no warning or description to its effects, but then one could argue that if your messing in those settings you best know what your doing.

http://blog.binaryjam.com/simon/archive/2004/05/14/159.aspx
Wednesday, August 18, 2004 3:55:30 PM UTC
I think another big "gotcha" to point out:

When working with cache, you should never assume the data is available in cache (it can expire before your expirate time), so you must always check before using it. If not available, then retrieve it from the source. So a good way to make sure the cache is updated when your app makes a change to the underlying data source is to remove the item from cache when the change is made.

However, if you programmatically remove an item from cache (so that updated data is retrieved from the source on it's next request), don't assume it will be removed from EVERY instance.
Friday, August 20, 2004 3:38:32 PM UTC
I'm curious, Scott: why are you thinking about using web gardens at all? What scenario is it that web gardens help you out with?
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.