Many ASP.NET applications today serve high-traffic environments, catering to tens of thousands of users. These applications often struggle with performance issues caused by slow database access, particularly when network traffic is heavy. This latency is becoming increasingly problematic as more users access these applications.
To resolve this performance constraint, ASP.NET and ASP.NET Core provides a built-in caching mechanism called “ASP.NET Cache“. This feature stores application data in memory, for faster access and reduces database trips, thereby enhancing the overall performance of your application.
Key Takeaways:
Overcoming In-Process Limits: Traditional ASP.NET caching is restricted to a single server and loses all data during worker process recycles.
Distributed Architecture: NCache provides an out-of-process (OutProc) distributed cache that scales across multi-server web farms and ensures data synchronization.
Four Core Caching Strategies: The solution supports Application Data Caching for custom objects, Session State management for persistence, View State reduction for payload optimization, and Output Caching for pre-rendered pages.
Configurable Integration: Most ASP.NET caching features, such as Session State and Output Cache, can be distributed via simple Web.config changes without requiring application code rewrites.
Built-In ASP.NET Caching Has Limitations
However, the built-in ASP.NET Cache is a stand-alone, in-process cache that lives within your ASP.NET application’s worker process. As a result, it is only useful for a single server environment. Some other limitations of ASP.NET include:
- Multiple Cache Instances Not Synchronized: It does not allow synchronization of cache instances running on multiple ASP.NET applications. This causes data integrity issues.
- NET Worker Process Recycles: All cached data is lost when the ASP.NET worker processes are recycled. The cache then needs to be reloaded from a data source, causing a big performance hit.
- Limited Worker Process Memory Size: There’s limited memory for this process to use for caching.
The following matrix summarizes how the built-in framework compares to an enterprise distributed architecture:
| Feature | Built-In In-Process Cache | NCache Distributed Cache |
|---|---|---|
| Deployment Environment | Single Server Only | Multi-Server Web Farm |
| Process Isolation | InProc (Lives inside worker process) | OutProc (Dedicated standalone process) |
| Data Synchronization | None (Instances are independent) | Automatic across multiple servers |
| Data Persistence | Lost on worker process recycle | Retained (Independent of app recycles) |
| Scalability | Limited by single-process memory | Highly scalable (Add servers in real-time) |
| Data Redundancy | No replication (Single point of failure) | Efficient data replication mechanisms |
Solution: ASP.NET Caching with Distributed Cache
To counter such limitations of ASP.NET caching, you need a distributed cache like NCache. It lives in its own process on multiple servers and also provides a mechanism to synchronize caches in a web farm.

Figure 1: Scalable ASP.NET caching architecture using NCache distributed cluster.
Here’s how a distributed cache such as NCache solves your problems with ASP.NET caching:
- A distributed cache synchronizes all cache instances created by multiple ASP.NET application instances, resolving data integrity issues.
- Since it is an OutProc (out of process) cache, it can be shared by multiple servers and worker processes.
- High scalability allows you to use as much memory as you want since there is no process limitation.
- You can scale your ASP.NET app caching servers in real-time.
- The efficient data replication feature keeps you at ease without any data loss issues.
How to Use ASP.NET Caching with Distributed Cache?
NCache provides different types of caching that you can use to get your ASP.NET applications up and running with caching.
- Application Data Caching: Programmatically cache custom object data (e.g., Product class objects) to reduce database round-trips.
- ASP.NET Session State Caching: Store volatile user-interaction data out-of-process across a web farm to prevent data loss during server recycles.
- ASP.NET View State Caching: Cache page and control values on the server side, returning only a unique ID to the browser to minimize payload sizes.
- ASP.NET Output Caching: Cache rendered page output dynamically based on query strings or browser types to accelerate response times.
Application Data Caching
Frequent retrieval of application data from your data source can slow down your ASP.NET application. By using data caching, you can store this data locally, reducing response times and improving overall performance.
This includes your custom object data such as Product class objects, as shown in the example below. The Product object is fetched from the database for the first time and then added to the cache and retrieved from the cache, the next time the data is accessed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using Alachisoft.NCache.Client; ... ICache _cache = CacheManager.GetCache("demoCache"); string key = "Product:1001"; // Search for key in cache var result = _cache.Get<string>(key); // If it doesn't exist, fetch from DB and add to cache if (result == null) { var product = LoadProductFromDB(1001); CacheItem item = new CacheItem(product); _cache.Add(key, item); } |
ASP.NET Session State Caching
You can use session caching to store user relevant data for your ASP.NET applications. Session data belongs to user interactions on your ASP.NET app. For example, an e-commerce business cannot afford to lose sessions in case the ASP.NET cache goes down. Hence, you can plug in NCache to your ASP.NET application to prevent data loss.
To use NCache for ASP.NET caching, you need no programming effort. Simply add the following configuration to Web.config of your application:
|
1 2 3 4 5 6 7 8 9 |
... <assemblies> <add assembly ="Alachisoft.NCache.SessionStoreProvider, Version=x.x.x.x, Culture=neutral, PublicKeyToken=CFF5926ED6A53769"/> </assemblies> ... |
Modify session state configuration to enable session state caching in ASP.NET. In web.config add the following section:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<configuration> ... <sessionState cookieless="false" regenerateExpiredSessionId="true" mode="Custom" customProvider="NCacheSessionProvider" timeout="20"> <providers> <add name="NCacheSessionProvider" type="Alachisoft.NCache.Web.SessionState.NSessionStoreProvider" cacheName="demoCache" sessionAppId="NCacheApp" exceptionsEnabled="true" writeExceptionsToEventLog="false" enableLogs="false" enableSessionLocking="true" sessionLockingRetry="-1" emptySessionWhenLocked="false" /> </providers> </sessionState> ... </configuration> ... |
ASP.NET View State Caching
ASP.NET View State provides client-side state management mechanism. It helps preserve page and control values between complete round trips for client requests. You can store ASP.NET view state on the web server and send a unique ID back to the browser. This ID will find the right ASP.NET view state in the cache.
Achieving ASP.NET view state caching in NCache is very easy. Here’s a part of the configuration that needs to be added to Web.config of your ASP.NET application:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
... <ncContentOptimization> <settings viewstateThreshold="12" enableViewstateCaching="true" enableTrace="false" groupedViewStateWithSessions="false" maxViewStatesPerSession="5" > <cacheSettings cacheName="demoCache"> <expiration type="None" duration="100" /> </cacheSettings> </settings> </ncContentOptimization> ... |
ASP.NET Output Caching
For web pages that are being frequently accessed, you can use output caching to improve response times for these specific pages. ASP.NET’s output caching system caches the different versions of pages’ content depending on the various parameters like query string parameters and browser type.
You can enable Output caching with NCache in your ASP.NET application without any code change and simply plugging in the following in Web.config:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
... <caching> <outputCache defaultProvider ="NOutputCacheProvider"> <providers> <add name="NOutputCacheProvider" type= "Alachisoft.NCache.OutputCacheProvider.NOutputCacheProvider, Alachisoft.NCache.OutputCacheProvider, Version=x.x.x.x, Culture=neutral, PublicKeyToken=cff5926ed6a53769" cacheName="demoCache" exceptionsEnabled="true"enableDetailLogs="false" enableLogs="true" writeExceptionsToEventLog="true"/>" </providers> </outputCache> </caching> ... |
Conclusion
To sum it up, a distributed cache like NCache is highly scalable, reliable and performance optimized to handle caching in ASP.NET. It has all the necessary features to overcome all the limitations of ASP.NET caching and is the only distributed cache that can handle ASP.NET caching on its own without you worrying about it. With all flavors of caching available with NCache, you can easily cache any type of data your application requires, by ensuring a boost in performance.
Frequently Asked Questions (FAQ)
Q: How does NCache prevent data loss during ASP.NET worker process recycles?
A: Unlike the built-in cache which lives inside the w3wp.exe process, NCache runs as a separate service (Out-of-Process). Because the cache exists independently of the application process, your data remains intact and available even if the ASP.NET application restarts or recycles.
Q: Can NCache improve mobile user experience via View State Caching?
A: Yes. By caching heavy View State data on the server and only sending a small “handshake” ID to the client’s browser, NCache significantly reduces the HTTP request/response payload. This leads to faster page load times and reduced bandwidth consumption for mobile and remote users.
Q: What is the benefit of using the NCache Output Cache provider over the default one?
A: The default Output Cache provider is limited to a single server. The NCache provider allows the rendered HTML of frequently accessed pages to be shared across all servers in a web farm. This ensures that if Server A renders a page, Server B can serve it from the cache immediately without re-executing the page life cycle.
Q: Do I need to change my application code to use NCache for Session State?
A: No. NCache integrates as a custom SessionStateStoreProvider. You only need to modify your Web.config file to point the sessionState section to NCache. This enables distributed sessions across your entire web farm without a single line of code change.
Q: How does Application Data Caching differ from Output Caching in NCache?
A: Application Data Caching is programmatic and used to store specific objects (like a Product list) retrieved from a database. Output Caching is configuration-based and used to store the entire HTML output of a page or control to avoid the overhead of re-rendering.







Very good article, Ron. The ASP.NET caching is explained in detail. The distributed cache NCache is reliable and gives a better performance for ASP.NET. It helps to overcome the limitations of ASP.NET caching. The article gives a clear picture of how the distributed caching can be done.