Microsoft ASP.NET Output Cache provides functionality to cache rendered content of ASP.NET pages or user controls for a specified duration. This allows your ASP.NET application to serve all subsequent requests from the cache instead of re-rendering and re-execution of a page.
You add the <% @OutputCache %> directive on the page to use ASP.NET Output Cache.
|
1 2 |
<%@Page … %> <%@OutputCacheDuration="duration"VaryByParam="paramList"%> |
Key Takeaways
Overcomes InProc Limitations: Replaces default In-Process caching to bypass the memory constraints of individual Azure Web Roles.
Ensures High Availability: Prevents data loss during Azure maintenance recycles by using a distributed store with built-in replication.
Enables Stateless Scaling: Eliminates redundant cache copies across load-balanced instances, allowing Web Roles to remain purely stateless.
Removes Database Bottlenecks: Significantly reduces backend load by serving frequent, heavy page requests directly from a high-performance caching tier.
ASP.NET Output Caching is a very useful feature, especially for situations when a page is accessed more frequently than it changes and you serve it from cache. This improves application performance by avoiding page re-executions and also by reducing your expensive database trips especially when page involves a lot of heavy database operations. This facilitates a stateless architecture, which is a prerequisite for horizontal scaling in Azure, as it offloads the memory burden from the application server to a dedicated caching tier. By caching these responses, you eliminate the database scalability bottleneck that typically occurs when processing millions of page requests involving heavy I/O operations.
Challenges of In-Process (InProc) Output Caching in Azure Web Roles
When you use Output Cache in Microsoft Azure then page output is stored as InProc within your Microsoft Azure Web Role by default. First problem with this is that it limits you to the memory that is available on your Web Role instance and this may create an out of memory issue when you cache a large amount of page output data. Another issue is that your application runs on multiple load balanced Microsoft Azure Web Role instances. The next request might go to another Web Role instance, which creates a new copy of ASP.NET Output Cache data in this instance, as well. These redundant copies of page outputs in each Web Role instance consume a lot of extra memory.
Microsoft Azure Web Role instances also recycle quite frequently for maintenance and patching. When this happens, all page outputs are lost and you’ll have to re-execute all pages to re-populate your page Output Cache, which is a negative performance impact on your Azure application.
How to Resolve Output Caching Problems in Microsoft Azure?
One way you can resolve all these issues in Microsoft Azure is to use a distributed cache, which runs out-of-process and is a common store for all Microsoft Azure Web Role instances. ASP.NET 4.0 has introduced an extensibility point that helps developers to use any distributed cache of their choice as their ASP.NET Output Cache store.
| Constraint | In-Process (Default) | NCache (Distributed) |
|---|---|---|
| Storage Capacity | Restricted to Web Role RAM | Scalable Cluster Memory |
| Instance Dependency | Redundant copies per Web Role | Unified Shared Store |
| Recycle Impact | Data lost on Role maintenance | Persistent via Replication |
| Architecture | Resulting in “Sticky” instances | Enables Stateless Web Roles |
Distributed cache is shared by all Microsoft Azure Web Roles for page outputs so there are no redundant copies made within individual web role instances. Microsoft Azure Web Roles become purely stateless so data is never lost when Web Roles are recycled. You can cache a huge amount of data in distributed cache by pooling memory resources of all cache servers together. Moreover, distributed cache reduces load on your database because you don’t have to go through page executions involving database calls in each Microsoft Azure Web Role instance separately.
NCache for Azure is an in-memory distributed cache for .NET applications deployed in Microsoft Azure cloud. NCache for Azure has implemented ASP.NET Output Cache provider which you can use to store ASP.NET page output and resolve all above mentioned issues. Additionally, deploying NCache as a Azure caching service provides data reliability with replication as well as improves application scalability.
How to use NCache for Azure ASP.NET Output Cache Provider
You can use NCache for Azure for output caching in ASP.NET as it follows without any code change to your Microsoft Azure application.
Step 1: Add Reference of NCache for Azure Output Cache provider assembly.
File: web.config
|
1 2 3 4 5 6 |
<compilation debug="true " targetframework="4.0"> <assemblies> <add assembly="Alachisoft.NCache.OutputCache,Version=x.x.x.x,Culture=neutral"> </add></assemblies> </compilation> |
Step 2: Register NCache for Azure Output Cache Provider under <configuration> section and provide cache settings.
File: web.config
|
1 2 3 4 5 6 7 8 |
<caching> <outputcache defaultprovider="NOutputCacheProvider"> <providers> <add name="NOutputCacheProvider" type="NCOutputCache.NOutputCacheProvider" exceptionsenabled="true" enablelogs="false" cachename="mypartitionofReplicaCache"> </add></providers> </outputcache> </caching> |
Step 3: Add ASP.NET Output Cache directive on the page that you want to cache.
|
1 |
<%@OutputCacheVaryByParam="ProductCategory"Duration="300"%> |
Documentation: Using ASP.NET Output Caching with NCache
NCache for Azure ASP.NET Output Cache Features
NCache for Azure provides a rich set of features for caching and managing Output Caching of Microsoft Azure. Below is a list of them:
- Specify duration for Page Output: NCache for Azure allows you to specify a duration for which you want to cache ASP.NET page output.
- Cache different versions of a page: NCache for Azure allows you to cache different versions of page depending on the various ASP.NET Output Cache directives such as VaryByParam,VaryByCustom, VaryByControl. Another version of page output is stored in distributed cache if a different param is received for a page request.
- Cache different portions of a page: You can also specify only portions of the page instead of caching entire page. This is for situations where you cache only static portion of the page and leave the dynamic part which is rendered at runtime.
- Implement Custom Hooks for ASP.NET Output Cache: NCache for Azure allows you to implement and register your custom hooks (interface) for page output. This is to attach some extended attributes to your page outputs such as NCache for Azure database dependencies, Tags, Groups, etc.
Conclusion
As you have seen, distributed cache allows you to cache ASP.NET page outputs, which resolves your application issues with multiple load balanced Azure Web Roles. NCache is an Azure ASP.NET Output Cache provider which helps improve ASP.NET application performance scalability and reliability. By utilizing NCache, Azure-deployed applications can handle millions of requests per second while maintaining sub-millisecond latency, effectively removing the database as a single point of failure for high-volume content delivery.
Frequently Asked Questions (FAQ)
Q: Why is default ASP.NET Output Caching inefficient in a load-balanced Azure environment?
A: By default, Output Cache is stored In-Process (InProc). In a load-balanced setup, each Web Role instance maintains its own isolated cache, leading to redundant memory consumption and inconsistent data across different server instances.
Q: How does NCache prevent cache loss during Azure Web Role maintenance?
A: Since NCache runs as a distributed service external to the Web Role process, it remains unaffected when Azure instances are recycled for patching. Its built-in data replication ensures that page outputs remain highly available across the cluster.
Q: Can I use NCache for Output Caching without rewriting my ASP.NET application?
A: Yes. NCache utilizes the ASP.NET 4.0 extensibility provider model. Integration is handled entirely through web.config settings by registering the NCache Output Cache provider, requiring zero changes to your actual page logic or C# code.
Q: Does NCache support caching specific parts of a page instead of the entire output?
A: Yes, NCache supports Fragment Caching. This allows you to selectively cache static portions of a page while allowing dynamic sections to be rendered at runtime, optimizing performance for pages with mixed content types.






