Stateless vs Stateful

136,675

Solution 1

I suggest that you start from a question in StackOverflow that discusses the advantages of stateless programming. This is more in the context of functional programming, but what you will read also applies in other programming paradigms.

Stateless programming is related to the mathematical notion of a function, which when called with the same arguments, always return the same results. This is a key concept of the functional programming paradigm and I expect that you will be able to find many relevant articles in that area.

Another area that you could research in order to gain more understanding is RESTful web services. These are by design "stateless", in contrast to other web technologies that try to somehow keep state. (In fact what you say that ASP.NET is stateless isn't correct - ASP.NET tries hard to keep state using ViewState and are definitely to be characterized as stateful. ASP.NET MVC on the other hand is a stateless technology). There are many places that discuss "statelessness" of RESTful web services (like this blog spot), but you could again start from an SO question.

Solution 2

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

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

Stateless:

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

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

Stateful:

// The state is maintained by the function

private int _number = 0; //initially zero

function int addOne()
{
   _number++;
   return _number;
}

Refer from: https://softwareengineering.stackexchange.com/questions/101337/whats-the-difference-between-stateful-and-stateless

Solution 3

A stateful app is one that stores information about what has happened or changed since it started running. Any public info about what "mode" it is in, or how many records is has processed, or whatever, makes it stateful.

Stateless apps don't expose any of that information. They give the same response to the same request, function or method call, every time. HTTP is stateless in its raw form - if you do a GET to a particular URL, you get (theoretically) the same response every time. The exception of course is when we start adding statefulness on top, e.g. with ASP.NET web apps :) But if you think of a static website with only HTML files and images, you'll know what I mean.

Solution 4

The adjective Stateful or Stateless refers only to the state of the conversation, it is not in connection with the concept of function which provides the same output for the same input. If so any dynamic web application (with a database behind it) would be a stateful service, which is obviously false. With this in mind if I entrust the task to keep conversational state in the underlying technology (such as a coockie or http session) I'm implementing a stateful service, but if all the necessary information (the context) are passed as parameters I'm implementing a stateless service. It should be noted that even if the passed parameter is an "identifier" of the conversational state (e.g. a ticket or a sessionId) we are still operating under a stateless service, because the conversation is stateless (the ticket is continually passed between client and server), and are the two endpoints to be, so to speak, "stateful".

Solution 5

Money transfered online form one account to another account is stateful, because the receving account has information about the sender. Handing over cash from a person to another person, this transaction is statless, because after cash is recived the identity of the giver is not there with the cash.

Share:
136,675

Related videos on Youtube

Team-JoKi
Author by

Team-JoKi

Enthousiastic junior developer

Updated on October 14, 2020

Comments

  • Team-JoKi
    Team-JoKi over 3 years

    I'm interested in articles which have some concrete information about stateless and stateful design in programming. I'm interested because I want to learn more about it, but I really can't find any good articles about it. I've read dozens of articles on the web which vaguely discuss the subject, or they're talking about web servers and sessions - which are also 'bout stateful vs stateless, but I'm interested in stateless vs stateful design of attributes in coding. Example: I've heard that BL-classes are stateless by design, entity classes (or at least that's what I call them - like Person(id, name, ..)) are stateful, etc.

    I think it's important to know because I believe if I can understand it, I can write better code (e.g. granularity in mind).

    Anyways, really short, here's what I know 'bout stateful vs stateless:

    Stateful (like WinForms): Stores the data for further use, but limits the scalability of an application, because it's limited by CPU or memory limits

    Stateless (Like ASP.NET - although ASP tries to be stateful with ViewStates): After actions are completed, the data gets transferred, and the instance gets handed back to the thread pool (Amorphous).

    As you can see, it's pretty vague and limited information (and quite focussed on server interaction), so I'd be really grateful if you could provide me with some more tasty bits of information :)

  • Team-JoKi
    Team-JoKi over 13 years
    Alright, thanks for the info, I had a look at the link and I found some interesting information! Although I'm still open for more ;)
  • NikolaDjokic
    NikolaDjokic over 13 years
    I've added another area that stateful and stateless is an important factor (RESTful web services).
  • Team-JoKi
    Team-JoKi over 13 years
    Thanks for the info! I'd vote up your answer but I don't have enough rep yet >_>
  • Nagappa L M
    Nagappa L M over 10 years
    many web apps are statefull beacause same sign up page gives differant results for user creditials...First time sign up will success...second time with same input signup will fail....because webapp has state of user somewhere stored...it may be database or differant storage
  • 7hi4g0
    7hi4g0 over 9 years
    Not sure if passing a session identifier on every request can be considered stateless. In my point of view, such case would be considered stateful. If, however, you always pass a token for the user but hold no other state whatsoever, than it's stateless. But feels stateful XD. This is so confusing.