title: Is there a best practice and recommended alternative to Session variables in MVC
body: <p>Okay, so first off before anyone attempts to make a determination that this is a "duplicate" question; I have reviewed most of the posts on SO regarding similar questions but even in combination of all that has been said I still am somewhat at a dilemma as to the definitive or maybe I should say unanimous agreement on this.</p>
  
  <p>I can however say that I have (based on the posts) conclusively determined that the answer is based on the scope of the requirement. But even with consideration of this, the opinions seem too diverse for me to make a decision as to how I should handle this.</p>
  
  <p>My immediate requirement is that I need to persist variable data from 1 controller across many views. More specifically, I have a controller and corresponding view that handles shopping cart item counts and I would like to persist that data across multiple views. I am thinking that the _layout view is the most logical choice for this.</p>
  
  <p>Now I have successfully accomplished this task by assigning the value to a Session variable which is retrieved from my _layout view; so even when the user were to navigate any where within the site the number of items in the Shopping Cart will persist until either they leave the site or complete the checkout; in which case the variable will be cleared in code.</p>
  
  <p>The posts I've read seemed biased to either staying away from Session variables in favor of Cookies and storing data in a database; or stating that for the intent purpose for which I propose to use them, Session variables are perfectly fine to use.</p>
  
  <p>The other thing I've read suggests that Session variables can potentially impede overall performance if there is high traffic on the site since the information is stored on the server.</p>
  
  <p>I personally cannot justify storing this type of information in a database and subsequently hitting the database as I'd imagine that this could also affect site performance and seems a bit overkill for storage of temporary data. TempData, ViewData and ViewBag do not work in persisting the data so they are not logical choices for the requirement IMO.</p>
  
  <p>If there is another well suited alternative to the Session variable (which is working for me) I would like to know what it is.</p>
  
  <p>2 posts that seem contradictory in effort of providing best recommendations leave me a bit confused.</p>
  
  <p>Cons: <a href="https://stackoverflow.com/questions/5220980/is-it-a-good-practice-to-avoid-using-session-state-in-asp-net-mvc-if-yes-why-a">Is it a good practice to avoid using Session State in ASP.NET MVC? If yes, why and how?</a></p>
  
  <p>Pros: <a href="https://stackoverflow.com/questions/6744493/still-ok-to-use-session-variables-in-asp-net-mvc-or-is-there-a-better-alternati">Still ok to use Session variables in ASP.NET mvc, or is there a better alternative for some things (like a cart)</a></p>
  
  <p>Seems that this question (although presented in many different variations) has no definitive answer that I can conclude.</p>
  
  <p>If there is a more preferrable way to accomplish this without overkill then that is the answer I'm in search of.</p>
  
  <p>I read somewhere the use of MVC filters in tandem with the Global.ascx application start section as well, but this does not seem appropriate for variables set at the controller level as much as perhaps, static variables.</p>
  
  <p>Can someone maybe squash (for lack of a better word) the many diverse opinions on the topic and maybe provide a more definitive answer to the question? I'm sure the diverse opinions have their place and I'm not attempting to discredit them. But having a definitive and possibly unanimous answer would be better; then I could sort through the other posts to determine what is best for my application.</p>
  
  <p>Of course, if this question has no definitive answer; just tell me that and I'll attempt to derive my own answer from the other posts.</p>
  
  <p>Thanks</p>
  
  <p>===========================================================</p>
  
  <h1>            UPDATED RESPONSE TO ANSWERS PROVIDED</h1>
  
  <p>Caching and Cookies seem to be a general preference from the responses however I've also noted the statement that caching its not an ideal candidate to use across multiple web server because synchronization can be a potential issue.</p>
  
  <p>Giving credit to Tim, it's stated that Database storage is optimized and users have the option to return at a later time and continue where they left off.</p>
  
  <p>That is an excellent point, but keeping foresight on probabilities; its likely a reasonable given that some users may not return leaving unneccessary data in the database.</p>
  
  <p>So keeping the DB optimized and clean (which "to me" is of equal relevance) would require implementing a maintenance task to automatically expire those records based on a set threshold of time to account for those circumstances. Although a maintenance task is not an unquestionable option, I still think this adds just a bit more work to the task simply for the intent purpose of serving as temporary storage.</p>
  
  <p>Nonetheless, I do respect Tim's recommendation and believe it deserves merit on countering my initial opinion to a degree; that a database would not seem to be a viable option for storing Temporary data; so I think the compromise would be to store the data in a database (given the scenario of a Shopping Cart or similar) perhaps after a checkout. This way as you previously stated, the data may be persistently tracked upon subsequent visits so you have a record of transactions. But more importantly, it would be data of those transactions having real relevance to persist to the database.</p>
  
  <p>It was also stated that although Session is faster than Database; but notwithstanding to have its caveats that can to some degree be mitigated by other mechanisms such as leveraging the SessionStateBehavior attribute, just serving as one example. </p>
  
  <p>BUT... I think Erik kind of drove the point home with the Dunning-Kruger Effect. Although, from the content and explanations for proposed answers given here; I seriously doubt the expertise of any of the individuals who have responded is any way questionable. Nonetheless, I tend to agree on the fact of getting a unanimous opinion may be somewhat of a higher than reasonable expectation on my part.</p>
  
  <p>What I was more specifically looking for was a general consensus for a technique that would comfortably accomodate a diverse number of scenarios. In other words, something that would accomodate not only my particular scenario but also provide the element of scalability to larger environments with potentially heavier traffic. This way a change in the programming would be either alleviated altogether or minimal at best.</p>
  
  <p>==================================================</p>
  
  <h1>        Summary based on the feedback:</h1>
  
  <ol>
  <li><p>Session variables seem to accomodate smaller case scenarios and when applicable, but they have some potential for persistence concerns among other notable discrepancies as stated very thorougly by Erik. So this option obviously will not fit a scalable model.</p></li>
  <li><p>Caching is preferable over Session variables but again not neccessarily the "best" scalable option due to among other things to the potential synchronization complexities in web server farm environments as previously pointed out. But an option nonetheless.</p></li>
  <li><p>Database storage is scalable but for the intent purpose of temporary volatile storage is probably not the most elegant option from a database perspective as it would require periodical cleanup. Personally, having a strong foundation in database concepts earlier in my career this probably is not going to be something that many developers will likely agree with; but using the database for this purpose may suffice for Web Development from a programmers perspective; however from perspective of the DAL and DB development this (to me) has the potential for mandating an additional DB task to enforce an efficient backend.</p></li>
  <li><p>Cookies seem to be a nice option having the combined "desirable" elements of Session variables and caching.</p></li>
  </ol>
  
  <p>==================================================</p>
  
  <h1>                   CONCLUSION</h1>
  
  <p>Based on the answers; I think COOKIES and CACHING seem to be generally well rounded proposals for best practice across the board in combination with database storage when continued persistence is required after the fact; as potentially good candidates for scalability of the ones presented. </p>
  
  <p>The ultimate choice between the 2 would seem to be based on the amount and type of data requiring storage (e.g. sensitive vs non-sensitive and whether or not there is any concern that the client may alter the data on their end); in addition to special considerations for COOKIES in the fact that they may be disabled by the clients.</p>
  
  <p>Obviously, there is no one size fits all solution as clearly pointed out and concluded from the answers provided but in terms of scalability; I may be wrong but these seem to be the BEST choices available.</p>
  
  <p>Because all the responses are good; I'm fairly going to credit all the posts as useful and going to accept Erik's answer as a well rounded overall scalable solution. I wish I could select more than one accepted answer as I believe Tim's response was also very well layed out and concise.</p>
  
  <p>Gupta's response was good also, but I wanted more elaboration of the proposed answer and not a repeat of previous posts.</p>
  
  <p>Thanks Guys!</p>