ASP.NET Output Cache in Microsoft Azure to Improve Performance

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.

<%@Page ... %>
<%@OutputCacheDuration="duration"VaryByParam="paramList"%>

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 also improves application scalability because the database generally becomes a scalability bottleneck when there are millions of such pages and requests involving database operations.

Problems with ASP.NET Output Cache in Microsoft Azure Environment

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.

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

<compilation debug="true "targetFramework="4.0">
    <assemblies>
        <add assembly= "Alachisoft.NCache.OutputCache,Version=x.x.x.x,Culture=neutral"/>
    </assemblies>
</compilation>

Step 2: Register NCache for Azure Output Cache Provider under <configuration> section and provide cache settings.

File: web.config

<caching>
    <outputCache defaultProvider ="NOutputCacheProvider" />
        <providers>
            <add name="NOutputCacheProvider" type="NCOutputCache.NOutputCacheProvider"
            exceptionsEnabled="true" enableLogs="false" cacheName="mypartitionofReplicaCache"/>
        </providers>
    </outputCache>
</caching>

Step 3: Add ASP.NET Output Cache directive on the page that you want to cache. 

<%@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:

  1. Specify duration for Page Output: NCache for Azure allows you to specify a duration for which you want to cache ASP.NET page output.
  2. 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.
  3. 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.
  4. 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.

Download NCache Trial

This entry was posted in ASP.Net, ASP.NET Output Cache, Microsoft Azure and tagged , . Bookmark the permalink.

One Response to ASP.NET Output Cache in Microsoft Azure to Improve Performance

  1. Pingback: How to use a Distributed Cache for ASP.NET Output Cache? - NCache Blog

Leave a Reply

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