Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stateless vs Stateful

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 :)

like image 431
Team-JoKi Avatar asked Mar 16 '11 17:03

Team-JoKi


People also ask

What is difference between stateful and stateless?

Stateless Protocol is a network protocol in which Client send request to the server and server response back as per the given state. Stateful Protocol is a network protocol in which if client send a request to the server then it expects some kind of response, in case of no response then it resend the request.

What is difference between stateful and stateless application?

Stateful services keep track of sessions or transactions and react differently to the same inputs based on that history. Stateless services rely on clients to maintain sessions and center around operations that manipulate resources, rather than the state.

Is HTTP stateful or stateless?

HTTP and HTTPS both are stateless protocols. The S in HTTPS stands for Secure and it refers to use of ordinary HTTP over an encrypted SSL/TLS connection.

Is REST API stateless or stateful?

A. REST APIs are stateless because, rather than relying on the server remembering previous requests, REST applications require each request to contain all of the information necessary for the server to understand it. Storing session state on the server violates the REST architecture's stateless requirement.


2 Answers

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

like image 57
Ankit Avatar answered Sep 19 '22 18:09

Ankit


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.

like image 37
Lucas Wilson-Richter Avatar answered Sep 20 '22 18:09

Lucas Wilson-Richter