How to resolve ASP.NET Cache Limitations in Web Farms
Microsoft provides ASP.NET Cache object within the Application scope of ASP.NET. And this allows you to cache application data and reduce those expensive database trips and improve your ASP.NET performance. Here is how ASP.NET Cache is typically used.
1 using System.Web.Caching;
3 string key ="Employee:EmployeeId:1000";
4 Employee employee = (Employee)Cache[key];
5 if (employee ==null)
7 // Load Employee from DB & put it in the Cache 8 LoadEmployeeFromDb(employee);
9 Cache.Insert(key, employee, null,
12 CacheItemPriority.Default, null );13 }
ASP.NET Cache Limitations in Web Farms
ASP.NET Cache is a stand-alone in-process (InProc) cache and therefore has many limitations if your application is deployed in a load-balanced web farm. These limitations are:
Multiple cache copies not synchronized: Multiple copies of your cache in web gardens or web farms are not synchronized with each other because ASP.NET Cache is stand-alone. And, this poses a major data integrity issue for your application.
Cache lost when worker process recycles: All ASP.NET Cache data is lost when the worker process is recycled. And, this causes a performance issue because you have to reload the entire cache from the database again.
Cache size issue is limited: ASP.NET Cache must live within the ASP.NET worker process memory limit and this puts a severe constraint on how big your cache can be.
Cache synchronization thru DB has issues: SQL CacheDependency in ASP.NET Cache allows you to synchronize cache with the database. And, some people use it to synchronize multiple copies of cache on different web servers with each other by going through the database. But, this has major performance and scalability issues because the database is again in the center of all activity and SQL CacheDependency itself is not very scalable.
Distributed Cache (NCache): Solution for Web Farms
NCache is an in-memory distributed cache and resolves all the limitations of ASP.NET Cache mentioned above. Here is how NCache addresses these limitations:
Cache synchronized in web farm: NCache is a distributed cache and synchronizes the cache across multiple servers in a web farm. This means there are no data integrity issues with NCache.
Cache OutProc in web garden: NCache is an out-of-process cache so multiple worker processes can share a common cache.
Cache size very scalable: NCache not only puts any ASP.NET worker process type of memory size limitation but also allows you to add more cache servers to grow storage capacity. So, you can have 100GB or more of cache size easily.
Cache scales for high transactions in web farms: NCache is a distributed cache and provides scalable caching topologies. This allows NCache to scale the cache cluster without any problems.
Cache is highly reliable with data replication: NCache provides intelligent data replication without sacrificing any performance. This data replication ensures that even if a cache server goes down, no data is lost. This allows your ASP.NET application to cache data without worrying about any data loss.
NCache has ASP.NET Cache API plus More
NCache provides all of ASP.NET Cache features with an identical API plus. This enables you to migrate from ASP.NET Cache to NCache seamlessly. You only change the namespace from System.Web.Caching to Alachisoft.NCache.Web.Caching and ensure that all your objects being cached are serializable.
Additionally, NCache provides numerous caching features that ASP.NET Cache does not have. Here is a partial list: