How to choose from Viewstate, SessionState, Cookies and Cache

Web applications are stateless, means once a web page is rendered from server to client, nothing in the page remains on server and the next time user submits the page, the page will be called and created from scratch.

ASP.NET provides following solutions to solve this problem:

1- Viewstate
2- Session Variables
3- Application Variables
4- Cache
5- Cookies

Now the question arises that when to use what?

1- Viewstate

Gets a dictionary of state information that allows you to save and restore the view state of a server control across multiple requests for the same page.

Viewstate is a hidden fields in an ASP.NET page, contains state of those controls on a page whose “EnableViewstate” property is “true”.
we can also explicitly add values in it, on an ASP.NET page like:
Viewstate.Add( “TotalStudents”, “87” );
Viewstate should be used when we want to save a value between different round-trips of a single page as viewstate of a page is not accessible by another page.
Because Viewstate render with the page, it consumes bandwidth, so be careful to use it in applications to be run on low bandwidth.

2- Session Variable

Session variables are usually the most commonly used.
When a user visits a site, it’s sessions starts and when the user become idle or leave the site, the session ends.
Session variables should be used to save and retrieve user specific information required on multiple pages.
Session variables consumes server memory, so if your may have a huge amount visitors, use session very carefully and instead of put large values in it try to put IDs and references

3- Application variables

Application variables are shared variables among all users of a web application
Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications
Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.

4- Cache
Caching is a most important aspect of high-performance web application. Caching provides a way of storing frequently accessed data and reusing that data. Practically, this is an effective way for improving web application’s performance.
Cache is probably the least used state feature of ASP.NET.
Cache is basically a resource specific state persistence feature, means unlike session it stick with resource instead of user, for instance: pages, controls etc.
Cache should be used or frequently used pages, controls, and data structures
Data cache can be used to cache frequently used list of values e.g. list of products

6- Cookies

Cookies are some values saved in browsers for a particular website o publicly accessible
The purpose of cookies is to help websites to identify visitors and retrieve their saved preferences
Cookies are also used to facilitate auto login by persisting user id in a cookie save in user’s browser
Because cookies have been saved at client side, they do not create performance issues but may create security issues as they can be hacked from browser.

Advantage of Caching
1. Reduce hosting server round-trips
2. Reduce database server round-trips
3. Reduce network traffic
4. Avoid time-consumption for regenerating reusable content
5. Improve performance

What is the difference between cookies,Session and cache?

A computer cookie is a small text file which has a unique ID tag from a website. IT contains information that you use often like name, address, mail ids etc.
Whereas a cache is a place where computer/OS stored contents to be readily and efficiently accessible. Operating systems use caching to improve system performance

State management is a critical thing to master when coming to Web world from a desktop application perspective.

  • Session is used to store per-user information for the current Web session on the server. It supports using a database server as the back-end store.
  • Cookie should be used to store per-user information for the current Web session or persistent information on the client, therefore client has control over the contents of a cookie.
  • Cache object is shared between users in a single application. Its primary purpose is to cache data from a data store and should not be used as a primary storage. It supports automatic invalidation features.
  • Application object is shared between users to store application-wide state and should be used accordingly.

 

  1 comment for “How to choose from Viewstate, SessionState, Cookies and Cache

  1. Kumar
    September 11, 2014 at 12:43 pm

    remember the following points on your finger-tips:

    1- Viewstate is bandwidth hungry
    2- Session variables are memory hungry as per number of users
    3- Applications variables are shared
    4- Cache is memory hungry as per number of resources
    5- Cookies are the least secure
    by default the session data and cache data is stored in the Ram of the Web Server.

    Advantage of Caching
    1. Reduce hosting server round-trips
    2. Reduce database server round-trips
    3. Reduce network traffic
    4. Avoid time-consumption for regenerating reusable content
    5. Improve performance
    ---------Data Caching-------------
    ASP.NET also supports caching of data as objects. We can store objects in memory and use them across various pages in our application. This feature is implemented using the Cache class. This cache has a lifetime equivalent to that of the application.
    private void Page_Load(Object src, EventArgs args) {
    DataSet ds = (DataSet)Cache["mydata"];
    if (ds == null) {
    // Connect to the SQL Server and get our data using a DataAdapter
    SqlConnection conn = new SQlConnection(connstr);
    SqlCommand cmd = new SqlCommand(cmdstr, conn);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);

    // Create a new DataTable and fill it
    DataTable dt = new DataTable("Item");
    adapter.Fill(dt);

    // Now add our DataTable to a DataSet instance and add to cache
    ds = new DataSet("TOC");
    ds.Tables.Add(dt);
    Cache.Insert("mydata", ds, null, DataTime.Now.AddHours(1), NoSlidingExpiration);
    }

    // Bind our data
    myRepeater.DataSource = ds;
    myRepeater.DataBind();
    }

    http://www.codeproject.com/Articles/29899/Exploring-Caching-in-ASP-NET

    http://www.c-sharpcorner.com/uploadfile/annathurai/data-cache-in-Asp-Net/
    http://msdn.microsoft.com/en-us/library/aa478965.aspx

Leave a Reply