ViewState vs Session vs Cache


HTTP is a stateless protocol. This implies that every time a new request is sent from the client to the server the state information of the previous request is lost
When it comes to persisting data across PostBack(round trip to application server) which is basis of web application, there are various options available and they all have there own pro/con.
Lets have a detailed look at each of them.

1)ViewState:-
ViewState is the method that web application uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values that must be retained during PostBack are serialized into base64-encoded strings.
Since ViewState is serialised and stored on the page back to client, payload increases as ViewState increases , which means if you have more data in ViewState size of page will be more and it will take more time to reach and load on client side.Hence size of page is directly proportional to ViewState data.
Pros:-
Value persists on PostBack.
Con:-
It increases size of the page.
You cannot store sensitive data in ViewState as it can be decoded back to value stored.

2)Session:-
Session is a way to store data on server side, so instead of storing data on page which travels across PostBack you can store data on server side in session objects, value held in object is valid throughout session and hence multiple page can take advantage of it.

Like higher ViewState can slower down your page transfer and loading, if you start storing more values in session than your infrastructure can manage then your application may come to hault.
Sessions default implementation is in memory but you can store in out of app domain where need, one of the common use case for out proc session is when your application is load balanced and you cannot ensure which server is going to serve the request.
Pros:-
Value persists throughout session hence multiple request can be fulfilled.
You can store resource intensive data of lower payload, which will save you computational time and resource.
Cons:-
Improper session handling can doom your application server.
You need proper session planning in load balanced applications.

3)Cache:-
We just talked about advantage of session when it comes to storing values that are needed multiple times and it makes sense to have them stored in session.

As we know session is user specific, so value stored in session of one user cannot be used in another.
Lets consider a scenario where you have an object storing master data for country, as per your analysis you use this data every now and then and it doesn't change frequently.

If said data is stored in session it will have to be stored in every users session who will need it, that's where caching comes handy as object stored in cache can be used by any user. Which means you store data in cache on application load and serve every request for cached data form cache.
Cache just like session is by default stored in application domain, if you want out proc implementation you have to implement distributed caching. MemCache,NCache are good examples of distributed caching.
Pros:-
Data once stored can be used by everyone.
Good performance gain can be achieved by storing resource intensive data in cache.
Cons:-
Without proper caching strategy you wont be able to reap much of its benefit.
You need strategy to update cache else when the primary object changes.

Frameworks like Asp.Net web form had ViewState enabled by default however with newer implementation of MVC, ViewState is disabled and its responsibility of model to provide with needed data for the view, where data is to be passed explicitly between Controller-View-Controller then ViewBag/ViewData/TempData can be used.

Comments

Popular posts from this blog

RabbitMQ setup and cluster configuration on a windows network

Gitflow using source tree

Component analysis for application security

Analyse log using plug and play FileBeat

Introduction to Blazor

Azure

What Interview Is/Is Not for an interviewer

WCAG Accessibility

.NET MAUI and Blazor to create windows/web and mobile application (.NET 6.0)

Clean Architecture