Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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.

share|improve this question

migrated from Aug 16 '11 at 0:39

This question came from our site for professional and enthusiast programmers.

6 Answers 6

up vote 25 down vote accepted

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.

share|improve this answer
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 ?? – Pankaj Upadhyay Aug 15 '11 at 6:43
yes, that's correct. – jgauffin Aug 15 '11 at 7:03
so is stateless better or worse? – think123 Apr 2 '14 at 7:19
@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. – jgauffin Apr 3 '14 at 6:53
@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. – Mason Wheeler Aug 7 at 19:28

Addition to @coder exact answer.

The idea of state is to remember previous data.
For example, you have a list control on the server with values "A,B,C" and "A" is selected. The list goes to the client browser. You select "B". And post back to the server. How would you know that the value is changed?

    Microsoft uses term ViewState in ASP.NET. There is a huge misunderstanding among developers about it.
    ViewState holds all initial state of the list in <input type="hidden" value="base64 encoded" />: values "A,B,C" and mark "A is selected".
    Then with post back browser sends ViewState and "B is selected" to the server. ASP.NET restores initial state of the list and applies new "B" selection. This was done to lure WinForms developers (mentioned by @Ronald). On the web server you can subscribe to list changes listObject.Changed += OnChanged.

    The problem with ViewState is the size. For years .NET developers forced to transfer kilobytes of useless information, like states of 20 controls for every roundtrip.
    The new approach is to send only new and small "B" value.
    Or if you want to track the change from "A" to "B" then implement it by your own. Using javascript and sending "Was A, Now B". Or save and retrieve state by ID in SQL Server.

  • ASP.NET MVC and ASP.NET implements state for Authentication and Cashing. So it is not correct to say that ASP.NET MVC fully stateless.
  • Memory mentioned in the answers means "remembering", not computer memory. State can be implemented by storing data in File system, SQL server or Computer Memory.
share|improve this answer
please be constructive and explain error before putting minus – Artru Aug 8 at 23:47

Stateless - There's no memory (state) that's maintained by the program

Stateful - The program has a memory (state)

To illustrate the concept of state I'll define a function which is stateful and one which is stateless


//The state is derived by what is passed into the function

function int addOne(int number)
    return number + 1;


//The state is maintained by the function

private int _number = 0; //initially zero 

function int addOne()
    return _number;

As others have said http is inherently stateless. So state must be built into your applications.

Imagine a request over the web where you have a client browser communicating to a server process. To maintain state over the stateless http protocol the browser will send typically send a session identifier to the server on each request. For each request the server will be like "ah, its this guy". State information can then be looked up in server side memory or in a database based on this session id.

In a purely stateless environment you wouldn't need this session id. Each request would contain all the information the server would need to process. But many applications need to maintain state to keep track of whether or not a session is authenticated to view certain content or to keep track of what a user is doing. You wouldn't want to send user credentials over the wire for each request.

share|improve this answer
quick question: so if the session Id is used (i.e. stolen) by another user, the server would have no idea this is someone else? – Mihai Aug 11 '14 at 14:53
That's correct. There are ways to protect a user's identity such as using https or httponly cookies. But if a user's computer is compromised then an attacker could fool the server into thinking they are the user. – coder Aug 12 '14 at 15:37
+1 for a clear and crispy code example. I am ever more convinced that most complex IT terms can only be understood in context. – Sebastian May 20 at 19:12
The possibility of a session ID being stolen is one reason that you have to be careful and applications like a CMS or something like OAuth build in ways to make doing that a lot harder. – Elin May 20 at 19:13
For each request the server will be like "ah, its this guy" - better said that in every other example I've seen – Rafael Eyng May 25 at 19:42

stateless means there is no memory of the past. Every transaction is performed as if it were being done for the very first time.

statefull means that there is memory of the past. Previous transactions are remembered and may affect the current transaction.

share|improve this answer
This should have been awarded the answer. It is by far the clearest and most precise answer here. – Mawg Dec 4 '14 at 16:07
Simple and accurate. – Anonymous Lettuce Mar 19 at 13:57
It is accurate for anyone who already know the difference. – Rafael Eyng May 25 at 19:43

A stateful operation modifies or requires some state of the system, and a stateless operation does not.

Example of a stateful textbox would be a previously edited comment on StackExchange - the textbox needs to display your previous comment and know the post-thread it was involved with to accept and process your input.

A generic email comment form with a mailto: tag would be a stateless textbox - it accepts your input and tosses it to your workstation's mail application without retaining any information.

share|improve this answer
A typical "leave your email address and a message and we'll get back to you" form is also stateless. When you submit the form the server dosen't care where you got it from, and it just copies the data into some customer service todo list and forgets about it. – StarWeaver Dec 4 '14 at 11:53

In my simple opinion the difference between ASP.NET (stateful) and ASP.NET-MVC (stateless) can be isolated to the fact that the first provides server-side controls and the other do not.

It is worth noting that the ASP.NET web forms approach is geared towards transitioning old VB and VC++ programmers who are used in the event model driven model to have a good quick way of learning web programming following the same event model paradigm, like you click a button and voila you trigger an event! All you need to do now is write your code in the event handler. Because of that ASP.NET needed to have concepts like view state and postbacks to monitor the state of the server side controls each round trip.

ASP.NET-MVC, however, does not use server-side controls so it doesn't have to maintain state. The MVC model separates the problem domain into the three partitions so that data is delivered to the client in a streamlined manner.

In summary, the server-side controls are what makes them different in as far as being stateful and stateless.

share|improve this answer

protected by gnat Aug 7 at 16:26

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.