How to Improve ASP.NET Performance with Distributed Caching?

If your ASP.NET application only has a few users, you probably don’t care how fast or slow it is and it is probably giving you pretty good performance anyway. But, as you add more load to your ASP.NET application, the chances are quite high that ASP.NET performance will drop significantly. It might even grind to a halt if enough load is put on it. And, ironically, all of that happens just when your business is seeing more activity so the impact is even greater.

ASP.NET today has become really popular for high traffic apps and it is now common to see 10-20 server load balanced web farms and in some cases even 50-100 server farms. So, in these situations, ASP.NET performance is even more sensitive issue to resolve.

The main reason for ASP.NET performance drop as you increase load on it is your database which cannot handle larger loads the way your ASP.NET application web farm can. This is because you can add more servers to the ASP.NET web farm but you cannot do the same with your database.

So, in these situations, your best bet is to use a distributed cache like NCache. NCache is in-memory so it is much faster than the database. And, NCache builds a cluster of cache servers and you can grow the cluster linearly just like the web farm. As a result, with NCache, your ASP.NET performance remains great even under extreme transaction loads.

You can use NCache in two ways:

1. ASP.NET Session State Storage

You can configure your ASP.NET application to store ASP.NET Session State in NCache instead of InProc, State Server, or SQL Server. Please note that no programming is needed here. You only modify web.config code as following:

  <sessionstate cookieless="false"
         <add name="NCacheSessionProvider"
	      exceptionsEnabled="true" enableSessionLocking="true"
	      emptySessionWhenLocked="false"  sessionLockingRetry="-1"
	      sessionAppId="NCacheTest" useInProc="false" enableLogs="false"
	      cacheName="myReplicatedCache" writeExceptionsToEventLog="false"

2. ASP.NET Application Data Cache

The other way is for you to cache application data in a distributed cache like NCache so the next time your ASP.NET application needs this data, it will find it in the cache. Here is a small code sample on how to cache application data:

using Alachisoft.NCache.Web.Caching;


      Cache cache = NCache.InitializeCache("myCache");

      // 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

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

The more data you cache, the less you have to go to the database and the faster is your ASP.NET application performance.

Download NCache Trial | NCache Details

Avatar for Iqbal Khan

Leave a Reply

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