Monday, February 10, 2014

Reduce ASP.NET Payload with View State Caching

Dramatically improve ASP.NET response times by reducing the payload returned to web clients. ASP.NET applications have to deal with View State becoming a performance overhead. And, NCache helps you cache View State on the web servers and send a much smaller payload to the user's browser containing only a unique token for this ViewState.

Why Cache View State?

View State is an encrypted text string containing the state of web form controls and widgets and is sent to the user's browser to preserve this state across post-backs. For forms with lot of controls, DataGrid control, or some other complex controls, the View State can become quite large. Here are some reasons why you should consider caching the View State.
  • View State is large (tens of kb) on large forms & DataGrid
  • Large View State slows down page download time
  • Large View State slows down post-back request time
  • Can cache View State on WFE servers & send only a token to user
  • View State cache is distributed to all WFE servers in the farm

How NCache Caches View State?

View State is included in the response of a web request as a hidden form field called __VIEWSTATE. On all page visits during the save view state stage, the Page class gathers the collective view state for all of the controls in its control hierarchy and serializes the state to a base-64 encoded string. Below is an example of a View State.

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"value="/wEPDwUJNzg0MDMxMDA1D2QWAmYPZBYCZg9kFgQCAQ9kFgICBQ9kFgJmD2QWAgIBDxYCHhNQcm...
ZpZ2F0aW9uTWVudQ8PZAUKQWxsIGluIE9uZWQiMPVaPthHdSA3rH8zgoNOvWxd4w=="
 />

Once NCache caches this View State, it replaces the "id" and "value" fields with its own data. Here is an example of that.

<input type="hidden" name="__NCPVIEWSTATE" id="__NCPVIEWSTATE"value="vs:cf8c8d3927ad4c1a84da7f891bb89185" /> <input type="hidden"name="__VIEWSTATE" id="__VIEWSTATE" value="" />

Notice, how it is preserved the original "__VIEWSTATE" hidden field so everything works as if there was no View State. But, it has inserted its own "__NCPVIEWSTATE" field that it will read when a post-back request comes from the user to the web server. NCache uses the "value" as the key to fetch the corresponding original View State from the in-memory cache and serves it to the ASP.NET page so it can populate the web form with data from the View State.
And, since the in-memory cache is distributed and synchronized across all WFE servers, even if the post-back request goes to a different WFE server in the farm than the original server where the View State was created, NCache is able to find the View State value in the cache.


No comments:

Post a Comment