What's the difference between stateful and stateless?

  • The books and documentation on the MVC just heap on using the Stateful and Stateless terms. To be honest, i am just unable to grab the idea of it, what the books are talking about. They don't give an example to understand any of the either state, rather than just telling that HTTP is stateless and with ASP.NET MVC microsoft is going along with it. Am I missing some fundamental knowledge, as i can't understand what is stateful and why is stateful and same goes for stateless.

    A simple and short example that talks about a control like button or textbox can be simplify the understanding i suppose.

  • jgauffin

    jgauffin Correct answer

    9 years ago

    Stateless means that HTTP doesn't have built in support for states; e.g. you can't store if a user has logged in or done something else.

    The most common solution is to use sessions to overcome that problem. This means that you have to be able to include a session identifier in each response or request. This is either done by creating a session cookie or by including the session identifier in all links.

    WebForms tries to make all that transparent (using ViewState) while MVC forces you to handle it manually.

    In your example you mentioned Buttons and TextBoxes. The easiest way to let them maintaining their state is simply to stop posting back the entire page. MVC got excellent support for ajax (through jQuery) and I suggest that you use ajax if you just want to do something on the current page.

    this means if i am logged in a website, every page i visit just rechecks for authentication before rendering the content via session identifier or a session cookie ??

    yes, that's correct.

    so is stateless better or worse?

    @think123: It's more performant as you do not have to manage state (i.e. load balancing etc is a lot easier). It's how ever more complex since you have to manage an artificial state.

    I think better or worse depends on what it is your are trying to do.

    @jgauffin: Performant is the wrong word here. Stateless is *less* performant since you don't have the opportunity to cache state and have to look it up repeatedly. What it is is more *scalable*; that's where the load balancing comes in, and the gains from scalability can offset the losses in performance when your system gets big enough.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM