Using ASP.NET Cache in Web Farms

ASP.NET has now become a really popular technology for web apps and more and more people are developing high traffic applications in it. And, to handle higher traffic, these ASP.NET apps are deployed in load balanced web farms where you can add more servers as your traffic load increases. So, it is very scalable except for one problem.

And, that problem is the database and your data storage which cannot scale in the same fashion to handle the higher traffic loads. So, what you get very quickly is a bottleneck where your ASP.NET application slows down and can even grind to a halt.

Download NCache free trial - Extremely fast and scalable in-memory distributed cache

In such situations, data caching is an excellent way of resolving this database and data storage bottleneck. Caching allows you to store application data close-by and reduce those expensive database trips.

What is ASP.NET Cache?

ASP.NET Cache allows you to cache application data and is actually a fairly feature-rich cache including the following features:

  • Expirations: Automatic absolute and sliding expirations
  • CacheDependency: To manage data relationships in the cache
  • SqlCacheDependency: To synchronize cache with database
  • Callbacks: To be notified when items are updated in cache

Here is some sample code showing ASP.NET Cache usage.

using System.Web.Caching;

// Create a key to lookup in the cache
// The key for will be like “Employees:PK:1000”
string key = "Employee:EmployeeId:" + emp.EmployeeId.ToString();

Employee employee = (Employee)Cache[key];
if (employee == null) {
    // item not found in the cache. load from db
    LoadEmployeeFromDb(employee);

    // Now, add it to the cache for future reference
    Cache.Insert(key, employee, null,
                 Cache.NoAbsoluteExpiration,
                 Cache.NoSlidingExpiration,
                 CacheItemPriority.Default, null );
}

ASP.NET Cache Limitations in Web Farms

Despite very useful caching features, ASP.NET Cache has some serious limitations. They are:

  • Does not synchronize across server or worker processes: It does not synchronize across multiple servers or even multiple worker processes. So, you cannot use it in a web farm or even a web garden unless your data is read-only whereas you need to cache all kinds of data, including one that changes somewhat frequently.
  • Cache size limitation: You cannot grow the ASP.NET Cache to be more than what one ASP.NET worker process can contain. For 32-bit systems, this is 1GB and that includes app code as well. Even for 64-bit systems, the size cannot scale.

Use ASP.NET Cache Compatible Distributed Cache

The way to work around these limitations of ASP.NET Cache is to use a distributed cache like NCache for web farms. NCache provides the same features that ASP.NET Cache plus more. But, as a distributed cache, NCache easily synchronizes across multiple servers. Here are some benefits you get from NCache:

  1. Scales transaction load very nicely: You can keep adding more cache servers to the cache cluster as your web farm grows from 2 to 200 servers. NCache never becomes a bottleneck in handling more traffic.
  2. Scales data storage nicely: As you add more cache servers, your cache storage capacity grows due to Partition Cache topology.
  3. Replicates data for reliability: You can ensure that no data loss occurs even if a server goes down because data is replicated to other servers.
  4. Dynamic self healing cache cluster: NCache provides 100% uptime through this. And, you can add or remove cache servers at runtime without stopping the cache or your application.

asp-net-cache-blog-figure1

Well, if you have an ASP.NET application running in a web farm, take a look at NCache and see how it will help improve your application’s performance and scalability. Here are some useful links for NCache:

 Download NCache Trial  |  NCache Details

This entry was posted in ASP.Net, ASP.NET Cache, Distributed caching, SQL cache dependency and tagged , , , , , . Bookmark the permalink.

3 Responses to Using ASP.NET Cache in Web Farms

  1. James says:

    I’ve used NCache during one of my projects and results were very impressive. highly recommended !!!

  2. Appreciate it for the excellent writeup. Anyway, how could we communicate?

Leave a Reply

Your email address will not be published. Required fields are marked *


4 + six =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>