<p>We are trying to lighten our page load as much as possible. Since ViewState can sometimes swell up to 100k of the page, I'd love to completely eliminate it.</p>

<p>I'd love to hear some techniques other people have used to move ViewState to a custom provider.</p>

<p>That said, a few caveats:</p>

<ul>
<li>We serve on average 2 Million unique visitors per hour.</li>
<li>Because of this, Database reads have been a serious issue in performance, so I don't want to store ViewState in the database.</li>
<li>We also are behind a load balancer, so any solution has to work with the user bouncing from machine to machine per postback.</li>
</ul>

<p>Ideas?</p>

[{'answer_id': 48082, 'author': 'Erick Sgarbi', 'author_id': 4171, 'author_profile': 'https://Stackoverflow.com/users/4171', 'pm_score': 2, 'selected': False, 'text': '<p>I have tested many ways to remove the load of view state from the page and between all hacks and some software out there the only thing that it is truly scalable is the <a href="http://www.strangeloopnetworks.com/products/AS1000/default.aspx" rel="nofollow noreferrer">StrangeLoops As10000 appliance</a>. Transparent, no need to change the underlying application.</p>\n'}, {'answer_id': 48100, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 2, 'selected': False, 'text': '<p>As <a href="https://stackoverflow.com/questions/48026/good-book-on-scaling-aspnet-applications#48052">previously stated</a>, I have used the database to store the ViewState in the past.  Although this works for us, we don\'t come close to 2 million unique visitors per hour.</p>\n\n<p>I think a hardware solution is definitely the way to go, whether using the <a href="http://www.strangeloopnetworks.com/" rel="nofollow noreferrer">StrangeLoop</a> products or another product.</p>\n'}, {'answer_id': 48105, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': "<p>Due to the typical organizational bloat, requesting new hardware takes eons, and requesting hardware that would involve a complete rewire of our current setup would probably get some severe resistance from the engineering department.</p>\n\n<p>I really need to come up with a software solution, because that's the only world I have some control over.</p>\n\n<p>Yay for Enterprise :(</p>\n"}, {'answer_id': 48117, 'author': 'Erick Sgarbi', 'author_id': 4171, 'author_profile': 'https://Stackoverflow.com/users/4171', 'pm_score': 0, 'selected': False, 'text': '<p>I\'ve tried to find some of the products I had researched in the past that works just like <a href="http://www.strangeloopnetworks.com/products/AS1000/default.aspx" rel="nofollow noreferrer">StrangeLoops</a> (but software based) It looks like they went all out of business, the only thing from my list that still up there is <a href="http://www.scaleoutsoftware.com/" rel="nofollow noreferrer">ScaleOut</a> but they are specialized in session state caching. </p>\n\n<p>I understand how hard it is to sell hardware solutions to senior management but it is always a good idea to at least get management to accept listening to the hardware\'s sales rep. I am much rather putting some hardware that will present me with an immediate solution because it allows me (or buy me some time) to get some other real job done. </p>\n\n<p>I understand, it really sucks but the alternative is to change your code for optimization and that would maybe cost a lot more than getting an appliance.</p>\n\n<p>Let me know if you find another software based solution.  </p>\n'}, {'answer_id': 48128, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': "<p>I'm going to see if I can come up with a way to leverage our current State server to contain the viewstate in memory, I should be able to use the user session ID to keep things synched up between machines.</p>\n\n<p>If I come up with a good solution, I'll remove any IP protected code and put it out for public use.</p>\n"}, {'answer_id': 48131, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 0, 'selected': False, 'text': '<p>Oh no, <a href="https://stackoverflow.com/questions/48070/moving-viewstate-out-of-the-page#48105">red tape</a>.  Well this is going to be a tall order to fill.  You mentioned <a href="https://stackoverflow.com/questions/48026/good-book-on-scaling-aspnet-applications#48045">here</a> that you use a state server to serve your session state.  How do you have this setup?  Maybe you can do something similar here also? </p>\n\n<p>Edit</p>\n\n<p>Awh @Jonathan, <a href="https://stackoverflow.com/questions/48070/moving-viewstate-out-of-the-page#48128">you posted</a> while I was typing this answer up.  I think going that route could be promising.  One thing is that it will definitely be memory intensive.</p>\n\n<p><a href="https://stackoverflow.com/questions/48070/moving-viewstate-out-of-the-page#48133">@Mike</a> I don\'t think storing it in the session information will be a good idea, due to the memory intensiveness of viewstate and also how many times you will need to access the viewstate.  SessionState is accessed a lot less often as the viewstate.  I would keep the two separate.</p>\n\n<p>I think the ultimate solution would be storing the ViewState on the client some how and maybe worth looking at.  With <a href="http://gears.google.com/" rel="nofollow noreferrer">Google Gears</a>, this could be possible now.</p>\n'}, {'answer_id': 48133, 'author': 'Mike', 'author_id': 1573, 'author_profile': 'https://Stackoverflow.com/users/1573', 'pm_score': 3, 'selected': False, 'text': '<p>How do you handle Session State?  There is a built-in "store the viewstate in the session state" provider.  If you are storing the session state in some fast, out of proc system, that might be the best option for the viewstate.</p>\n\n<p>edit: to do this add the following code to the your Page classes / global page base class</p>\n\n<pre><code>    protected override PageStatePersister PageStatePersister {\n        get { return new SessionPageStatePersister(this); }\n    }\n</code></pre>\n\n<p>Also... this is by no means a perfect (or even good) solution to a large viewstate.  As always, minimize the size of the viewstate as much as possible.  However, the SessionPageStatePersister is relatively intelligent and avoids storing an unbounded number of viewstates per session as well as avoids storing only a single viewstate per session.</p>\n'}, {'answer_id': 82329, 'author': 'JacquesB', 'author_id': 7488, 'author_profile': 'https://Stackoverflow.com/users/7488', 'pm_score': 0, 'selected': False, 'text': '<p>Have you considered if you really <em>need</em> all that viewstate? For example, if you populate a datagrid from a database, all the data will be saved in viewstate by default. However, if the grid is just for presenting data, you dont need a form a all, and hence no viewstate. </p>\n\n<p>You only need viewstate when there is some interaction with the user through postbacks, and even then the actual form data may be sufficient to recreate the view. You can selectively disable viewstate for controls on the page.</p>\n\n<p>You have a very special UI if you actually <em>need</em> 100K of viewstate. If you reduce the viewstate to what is absolutely necessary, it might turn out to be the easiest and most scalable to keep the viewstate in the page.</p>\n'}, {'answer_id': 82346, 'author': 'leppie', 'author_id': 15541, 'author_profile': 'https://Stackoverflow.com/users/15541', 'pm_score': 2, 'selected': False, 'text': '<p>The following works quite well for me:</p>\n\n<pre><code>string vsid;\n\nprotected override object LoadPageStateFromPersistenceMedium()\n{\n  Pair vs = base.LoadPageStateFromPersistenceMedium() as Pair;\n  vsid = vs.First as string;\n  object result = Session[vsid];\n  Session.Remove(vsid);\n  return result;\n}\n\nprotected override void SavePageStateToPersistenceMedium(object state)\n{\n  if (vsid == null)\n  {\n    vsid = Guid.NewGuid().ToString();\n  }\n  Session[vsid] = state;\n  base.SavePageStateToPersistenceMedium(new Pair(vsid, null));\n}\n</code></pre>\n'}, {'answer_id': 271279, 'author': 'Middletone', 'author_id': 35331, 'author_profile': 'https://Stackoverflow.com/users/35331', 'pm_score': 0, 'selected': False, 'text': '<p>I might have a simple solution for you in another post.  It\'s a simple class to include in your app and a few lines of code in the asp.net page itself.  If you combine it with a distributed caching system you could save a lot of dough as viewstate is large and costly. Microsoft’s velocity might be a good product to attach this method too.  If you do use it and save a ton of money though I\'d love a little mention for that.  Also if you are unsure of anything let me know and I can talk with you in person.</p>\n\n<p>Here is the link to my code. <a href="https://stackoverflow.com/questions/271269/ideas-for-how-to-deal-with-viewstate-when-using-ajax-and-update-panels">link text</a></p>\n\n<p>If you are concerned with scaling then using the session token as a unique identifier or storing the state in session is more or less guaranteed to work in a web farm scenario.</p>\n'}, {'answer_id': 432395, 'author': 'Middletone', 'author_id': 35331, 'author_profile': 'https://Stackoverflow.com/users/35331', 'pm_score': 0, 'selected': False, 'text': '<p>Store the viewstate in a session object and use a distributed cache or state service to store session seperate from the we servers such as microsofts velocity.</p>\n'}, {'answer_id': 1197820, 'author': 'ChickenMilkBomb', 'author_id': 118065, 'author_profile': 'https://Stackoverflow.com/users/118065', 'pm_score': 0, 'selected': False, 'text': '<p>I know this is a little stale, but I\'ve been working for a couple of days on an opensource "virtual appliance" using squid and ecap to:</p>\n\n<p>1.) gzip\n2.) handle ssl\n3.) replace viewstate with a token on request / response\n4.) memcache for object caching</p>\n\n<p>Anyways, it looks pretty promising.  basically it would sit in front of the loadbalancers and should really help client performance.  Doesnt seem to be very hard to set up either.</p>\n'}, {'answer_id': 1266899, 'author': 'mcintyre321', 'author_id': 2086, 'author_profile': 'https://Stackoverflow.com/users/2086', 'pm_score': 0, 'selected': False, 'text': '<p>I blogged on this a while ago - the solution is at <a href="http://www.adverseconditionals.com/2008/06/storing-viewstate-in-memcached-ultimate.html" rel="nofollow noreferrer">http://www.adverseconditionals.com/2008/06/storing-viewstate-in-memcached-ultimate.html</a></p>\n\n<p>This lets you change the ViewState provider to one of your choice without having to change each of your Page classes, by using a custom PageAdapter. I stored the ViewState in memcached. In retrospect I think storing it in a database or on disk is better - we filled memcached up very quickly. Its a very low friction solution.</p>\n'}, {'answer_id': 5446743, 'author': 'saille', 'author_id': 30246, 'author_profile': 'https://Stackoverflow.com/users/30246', 'pm_score': 1, 'selected': False, 'text': '<p>You can always compress ViewState so you get the benefits of ViewState without so much bloat:</p>\n\n<pre><code>public partial class _Default : System.Web.UI.Page {\n\n  protected override object LoadPageStateFromPersistenceMedium() {\n    string viewState = Request.Form["__VSTATE"];\n    byte[] bytes = Convert.FromBase64String(viewState);\n    bytes = Compressor.Decompress(bytes);\n    LosFormatter formatter = new LosFormatter();\n    return formatter.Deserialize(Convert.ToBase64String(bytes));\n  }\n\n  protected override void SavePageStateToPersistenceMedium(object viewState) {\n    LosFormatter formatter = new LosFormatter();\n    StringWriter writer = new StringWriter();\n    formatter.Serialize(writer, viewState);\n    string viewStateString = writer.ToString();\n    byte[] bytes = Convert.FromBase64String(viewStateString);\n    bytes = Compressor.Compress(bytes);\n    ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));\n  }\n\n  // ...\n\n}\n\nusing System.IO;\nusing System.IO.Compression;\n\npublic static class Compressor {\n\n  public static byte[] Compress(byte[] data) {\n    MemoryStream output = new MemoryStream();\n    GZipStream gzip = new GZipStream(output, \n                      CompressionMode.Compress, true);\n    gzip.Write(data, 0, data.Length);\n    gzip.Close();\n    return output.ToArray();\n  }\n\n  public static byte[] Decompress(byte[] data) {\n    MemoryStream input = new MemoryStream();\n    input.Write(data, 0, data.Length);\n    input.Position = 0;\n    GZipStream gzip = new GZipStream(input, \n                      CompressionMode.Decompress, true);\n    MemoryStream output = new MemoryStream();\n    byte[] buff = new byte[64];\n    int read = -1;\n    read = gzip.Read(buff, 0, buff.Length);\n    while(read &gt; 0) {\n      output.Write(buff, 0, read);\n      read = gzip.Read(buff, 0, buff.Length);\n    }\n    gzip.Close();\n    return output.ToArray();\n  }\n}\n</code></pre>\n'}, {'answer_id': 8222627, 'author': 'Ashish Nangla', 'author_id': 973495, 'author_profile': 'https://Stackoverflow.com/users/973495', 'pm_score': 0, 'selected': False, 'text': '<p>No need to buy or sell anything to eliminate viewstate bloating. Just need to extend the HiddenFieldPageStatePersister. The 100-200KB of ViewState will stay on the server and will send only a 62byte token on the page instead.</p>\n\n<p>Here is a detailed article on how this can be done:</p>\n\n<p><a href="http://ashishnangla.com/2011/07/21/reducing-size-of-viewstate-in-asp-net-webforms-by-writing-a-custom-viewstate-provider-pagestatepersister-part-12/" rel="nofollow">http://ashishnangla.com/2011/07/21/reducing-size-of-viewstate-in-asp-net-webforms-by-writing-a-custom-viewstate-provider-pagestatepersister-part-12/</a> </p>\n'}]
['https://Stackoverflow.com/questions/48070', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1965/']