If your ASP.NET application has just a few users, you might not notice any performance problems. However, as user load increases, ASP.NET performance can 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.
It has become popular for high-traffic applications, often deployed in large web farms with 10-20 servers or even 50-100 servers in some cases. In such situations, maintaining performance becomes even more crucial. Therefore, it’s essential to address performance issues early to ensure your application can handle increasing loads smoothly.
Using NCache as a Distributed Cache
The main reason for ASP.NET performance drops as you increase the load on it is your database that 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 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 that you can scale linearly like the web farm. As a result, with NCache, your performance remains great even under extreme transaction loads. You can use NCache in the following two ways:
ASP.NET Session State Storage
You can configure your application to store ASP.NET Session State in NCache instead of InProc, State Server, or SQL Server. Please note that no code change is required here. You only modify the web.config as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<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" sessionLockingRetry="-1" emptySessionWhenLocked="false" /> sessionAppId="demoApp" useInProc="false" enableLogs="false" cacheName="myReplicatedCache" writeExceptionsToEventLog="false" AsyncSession="false"> </providers> </sessionstate> |
ASP.NET Application Data Cache
The other way is to cache application data in a distributed cache like NCache so the next time your application needs this data, it will find it in the cache. The more data you cache, the less you have to go to the database and the faster is your application performance. Here is a sample of how to cache application data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ICache cache = CacheManager.GetCache("myCache"); // Create a key to lookup in the cache string customerKey = $"Customer:ALFKI"; Customer customer = FetchCustomerFromDB(customerKey); // Get customer from database if not found in cache if (customer == null) { // Get customer from database customer = FetchCustomerFromDB("ALFKI"); cache.Add(customerKey, customer); } // Item added in cache successfully |
Conclusion
Enhancing ASP.NET performance through distributed caching solutions like NCache proves invaluable as applications scale. By offloading session state management and application data caching to NCache, ASP.NET applications not only maintain optimal performance under heavy transaction loads but also ensure seamless scalability across multiple server environments. Download NCache today and explore how it can meet your application’s high-performance demands effortlessly.