How to Cache ASP.NET View State in a Distributed Cache?

ASP.NET today is widely used for high traffic web applications that need to handle millions of users and are deployed in load balanced web farms. One important part of ASP.NET is View State that many applications use. ASP.NET View State is a very powerful mechanism that stores pages, controls and custom values between multiple HTTP requests across client and the web server.

ASP.NET View State values are stored in a hidden field on the page and encoded as a Base64 string. An ASP.NET View State looks like this:

Although very useful, ASP.NET View State frequently becomes quite large especially in situations where your ASP.NET application has grid controls and many other controls on pages. This is added to your HTTP request and response and that really slows down your ASP.NET response time to unbearable levels.

Another downside of heavy ASP.NET View State is the increased bandwidth usage that increases your bandwidth cost considerably. For example, if for each HTTP requests, you end up appending 60-100k of additional ASP.NET View State data, just multiply it by the total number of transactions and you’ll quickly see how much more it will cost you in bandwidth consumption.

Finally, in some situations, there is a security risk with sending confidential data as part of ASP.NET View State. Encrypting it before sending is also costly.

To resolve all these problems you can cache ASP.NET View State on the web server and assign a GUID as its key in the cache. This GUID is then sent to the browser in a hidden field and it comes back along with the next HTTP request and is used to fetch the corresponding ASP.NET View State from the cache. This reduces your payload sent to the browser, which not only improves ASP.NET response time, but also reduces your bandwidth consumption cost dramatically.

If your ASP.NET application is running in a load balanced web farm, then you must use a distributed cache. A stand-alone cache like ASP.NET Cache won’t work. NCache is an enterprise level distributed cache that provides ASP.NET View State caching module. In order to use it, you don’t need to do any programming. Just modify your ASP.NET web.config for it.

Notice that another hidden tag is added with ASP.NET View State. It contains the unique key assigned to ASP.NET View State of your page, in your distributed cache. So whenever your application server needs ASP.NET View State value it can get it from cache easily.

With this, you will see a remarkable performance boost in your ASP.NET response times and your bandwidth consumption cost is reduced significantly as well.

Please explore more about ASP.NET View State caching by trying NCache ASP.NET View State module yourself.

2 comments

You can use NCache for Azure for storing ASP.NET Session State for Microsoft Azure ASP.NET applications. Here is a blog that explains Microsoft Azure Session Storage options and why NCache for Azure is best among all available options in Microsoft Azure.