How to Improve ASP.NET Performance with Distributed Caching?

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<sessionstate cookieless="false"
                   regenerateExpiredSessionId="true"
                   mode="Custom"
                   customProvider="NCacheSessionProvider"
                   timeout="20">
   <providers>
       <add name="NCacheSessionProvider"
        type="Alachisoft.NCache.Web.SessionState.NSessionStoreProvider"
        exceptionsEnabled="true" enableSessionLocking="true"
        emptySessionWhenLocked="false"  sessionLockingRetry="-1"
        sessionAppId="NCacheTest" useInProc="false" enableLogs="false"
        cacheName="myReplicatedCache" writeExceptionsToEventLog="false"
        AsyncSession="false"/>
   </providers>
 </sessionstate>

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
            LoadEmployeeFromDb(employee);
 
            // Now, add it to the cache for future reference
            Cache.Insert(key, employee, null,
                         Cache.NoAbsoluteExpiration,
                         Cache.NoSlidingExpiration,
                         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.