How to Resolve ASP.NET Cache Limitations in Web Farms with NCache

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.

using System.Web.Caching; ... string key = "Employee:EmployeeId:1000"; Employee employee = (Employee)Cache[key]; if (employee == null){ // Load Employee from DB & put it in the Cache LoadEmployeeFromDb(employee); Cache.Insert(key, employee, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null ); }

Doc: Client Side ASP.NET Features

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.

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.

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.

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 through 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 a 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.

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.

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.

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.

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:

Dynamic Clustering

Caching Topologies

Asynchronous & Bulk Operations

Read-through, Write-through, & Write-behind

SQL-Like Query Language and LINQ

Event Notifications

GUI Based Cache Administration & Monitoring

Much more!

Read more about all of NCache edition features.

How to Quickly Use NCache?

Here are some simple steps you could take to quickly benefit from it:

Install NCache: on your web servers and two cache servers Create a cache: through a GUI based NCache Manager tool Cache application data in NCache: You will need to reference NCache assemblies and then make NCache API calls to store and fetch your .NET objects from the cache Monitor ASP.NET performance: You can now monitor ASP.NET performance and also NCache performance. NCache provides a rich set of PerfMon counters for you to monitor with

What to Do Next?