- Simplicity by design

Cache Items being Removed in ASP.NET after every request


Today, I ran across an issue and thought of documenting it over here. I was implementing a FileDependency based caching scheme and wanted to verify the caching works good. The basic testing by changing the file impacted the application and from functionality perspective everything looked good. But, I wanted to verify the file is not being read for every request. I started by loading the the FileMon to see how frequent the file was being read. To my surprise the file is being accessed on every request.

I thought there was something wrong with my implementation and tried to monitor of an item for which we were already using the File dependency caching and FileMon showed it is also accessed for every new request.

I added the “ASP. NET Applications \ Cache API Entries” and “ASP.NET Applications \ Cache API Turn over rate” counters to the performance monitors and it showed the entries are being reset to 0 and spikes for the turn over rate showing the cache is getting invalidated for every request.

To understand this further I enabled further logging and added a CacheItemRemovedCallback and logged the reason for which the cache item was removed.

I was expecting CacheItemRemovedReason.Expired but, the log showed the items are being deleted from the cache due the CacheItemRemovedReason.Removed. Trusty MSDN Documentation tells

Removed -
The item is removed from the cache by a
Remove method call or by an Insert method call that specified the same key.

But in my case, I am not doing an explicit Remove / Insert. I am just using Add. So, added further tracing information to log the stack trace. The stack trace had the below entries

at System.Web.HttpRuntime.Dispose()
at System.Web.HttpRuntime.ReleaseResourcesAndUnloadAppDomain(Object)

That showed the application for some reason is recycling. So, CaheItemRemovedCallback is getting called with Removed as a reason when the application restarts. To confirm this, I loaded the performance monitor (perform.exe) and loaded the “ASP.NET \ Application Restarts” counter The counter kept increasing for every request, showing the Application restart is happening. So, I enabled the application start and application stop event logging by adding the below entry to the web.config. For more information check out the MSDN article

<add name="Application Events" eventName="Application Lifetime Events"
provider="EventLogProvider" profile="Default" minInterval="00:01:00" />

This logged only the application start event but for every request but no application end event. This thrashed my only hope for finding the the reason for application restart. But, when looking at the event logs, for every application start event there is another information logged by  a in house event source informing about an Event source registration. So, I re-installed the component causing the event and that stopped the Application from re-cycling.

Application started to respond quickly and also, the performance monitor’s Cache counters showed the cache is maintained.

Share
  • Turn this article into a PDF!
  • ramesh
    hellow ramesh, howare you, this is a very nise,
  • Ramesh
    hello world
  • rvramesh
    This comment is for testing Doloto profiling.
blog comments powered by Disqus