72

Explain the differences between stateless and stateful systems, and impacts of state on parallelism.

Stack Guru
  • 863
  • 2
  • 8
  • 7
  • You could look at this:http://www.cs.swarthmore.edu/~newhall/cs85/s08/trilok.pdf – James Black Mar 25 '11 at 17:38
  • possible duplicate of [Stateless vs Stateful - I could use some concrete information](http://stackoverflow.com/questions/5329618/stateless-vs-stateful-i-could-use-some-concrete-information) – ax. Mar 25 '11 at 17:43
  • 8
    No, on Scott Hanselman's site it was an interview question for good senior engineers and I wanted to know the answer. – Stack Guru Mar 25 '11 at 19:00
  • Question straight out of http://www.hanselman.com/blog/NewInterviewQuestionsForSeniorSoftwareEngineers.aspx – hyankov Jan 26 '17 at 16:31
  • 1
    So how did the interview go? – 0xc0de Sep 27 '18 at 13:00

2 Answers2

84

A stateless system can be seen as a box [black? ;)] where at any point in time the value of the output(s) depends only on the value of the input(s) [after a certain processing time]

A stateful system instead can be seen as a box where at any point in time the value of the output(s) depends on the value of the input(s) and of an internal state, so basicaly a stateful system is like a state machine with "memory" as the same set of input(s) value can generate different output(s) depending on the previous input(s) received by the system.

From the parallel programming point of view, a stateless system, if properly implemented, can be executed by multiple threads/tasks at the same time without any concurrency issue [as an example think of a reentrant function] A stateful system will requires that multiple threads of execution access and update the internal state of the system in an exclusive way, hence there will be a need for a serialization [synchronization] point.

sergico
  • 2,435
  • 1
  • 25
  • 36
  • 1
    Suppose I have an application that stores data about a User transaction which may be like balance credit ,withdrawal and many more.Then is this application stateless/stateful ? Because this application fetches the data about user which may be inserted recently. – Gani Jan 07 '19 at 05:28
52

A stateful server keeps state between connections. A stateless server does not.

So, when you send a request to a stateful server, it may create some kind of connection object that tracks what information you request. When you send another request, that request operates on the state from the previous request. So you can send a request to "open" something. And then you can send a request to "close" it later. In-between the two requests, that thing is "open" on the server.

When you send a request to a stateless server, it does not create any objects that track information regarding your requests. If you "open" something on the server, the server retains no information at all that you have something open. A "close" operation would make no sense, since there would be nothing to close.

HTTP and NFS are stateless protocols. Each request stands on its own.

Sometimes cookies are used to add some state to a stateless protocol. In HTTP (web pages), the server sends you a cookie and then the browser holds the state, only to send it back to the server on a subsequent request.

SMB is a stateful protocol. A client can open a file on the server, and the server may deny other clients access to that file until the client closes it.

Oren Hizkiya
  • 4,367
  • 1
  • 19
  • 33
Henish
  • 531
  • 4
  • 2