views:

99

answers:

4

I'm in need of some clarification. I've been reading about REST, and building RESTful applications. According to wikipedia, REST itself is defined to be Representational State Transfer. I therefore don't understand all this stateless gobbledeygook that everyone keeps spewing.

From wikipedia:

At any particular time, a client can either be in transition between application states or "at rest". A client in a rest state is able to interact with its user, but creates no load and consumes no per-client storage on the set of servers or on the network.

Are they just saying don't use session/application level data store???

I get that one goal of REST is to make URI access consistent and available, for instance, instead of hiding paging requests inside posts, making the page number of a request a part of the GET URI. Makes sense to me. But it seems like it is just going overboard saying that no per client data (session data) should ever be stored server side.

What if I had a queue of messages, and my user wanted to read the messages, but as he read them, wanted to block certain senders messages coming through for the duration of his session? Wouldn't it make sense to store this in a place on the server side, and have the server only send messages (or message ID's) that were not blocked by the user?

Do I really have to send the entire list of message senders to block each time I request the new message list? The message list pertinent to me wouldn't/shouldn't even be a publicly available resource in the first place..

Again, just trying to understand this. Someone please clarify.

Update:

I have found a stack overflow question that has an answer that doesn't quite get me all the way there: http://stackoverflow.com/questions/2641901/how-to-manage-state-in-rest which says that the client state that is important should all be transferred on every request.... Ugg.. seems like a lot of overhead... Is this right??

+5  A: 

By stateless it means that the web server does not store any state about the client. That does not preclude other services that the web server talks to from maintain state about business objects, just not about the clients connection state. The clients state should not be stored on the server, but passed around to everyone that needs it. That is where the ST in REST comes from, State Transfer. You transfer the state around instead of having the server store it. This is the only way to scale to millions of users.

The load of session management is amortized across all the clients, the clients store their session state and the servers can service an order of magnitude or more clients in a stateless fashion.

fuzzy lollipop
That seems like a pretty bold statement that it is the *only way* to scale to millions of users. Why can't server side sessions be just another service?
Zak
@Zak: Because millions of sessions is millions of sessions. The point is to avoid the overhead of all this session management.
S.Lott
it is not boldness it is experience
fuzzy lollipop
+3  A: 

Are they just saying don't use session/application level data store???

No. They aren't saying that in a trivial way.

They're saying do not define a "session". Don't login. Don't logout. Provide credentials with the request. Each request stands alone.

You still have data stores. You still have authentication and authorization. You just don't waste time establishing sessions and maintaining session state.

The point is that each request (a) stands completely alone and (b) can be trivially farmed out to a giant parallel server farm without any actual work. Apache or Squid can pass RESTful requests around blindly and successfully.

What if I had a queue of messages, and my user wanted to read the messages, but as he read them, wanted to block certain senders messages coming through for the duration of his session?

If the user wants a filter, then simply provide the filter on each request.

Wouldn't it make sense to ... have the server only send messages (or message ID's) that were not blocked by the user?

Yes. Provide the filter in the RESTful URI request.

Do I really have to send the entire list of message senders to block each time I request the new message list?

Yes. How big can this "list of message senders to block" be? A short list of PK's?

A GET request can be very large. If necessary, you can try a POST request even though it sounds like a kind of query.

S.Lott
It seems this violates the lazy programmer principle. Why keep sending the same state over and over if you can just do it once? It seems like you will have to do all authentication over again each time, as well as all role lookup, filter setup, etc...
Zak
BTW, thank you for the answer. It seems like I'm hearing, "because that is what we have found works well in practice." And that is always a very good reason :)
Zak
@Zak: I have no idea what "lazy programming" is. Sending things "over again each time" is simpler -- do the same thing -- no weird cache management or server affinity or anything. It is simpler to have each request totally stand in it's own. And -- as a lazy programmer -- I prefer that. Just process the request and be done with it.
S.Lott
looking up something over and over is way more expensive than having it given to you when you need it. Plain and simple Database systems and especially RDBMS systems will ALWAYS be the bottleneck in a web based application. It is less complicated, and less code, and less code means less bugs, and less bugs means less maintenance, and that means less money to support the system. If don't believe us go implement a system that has 12 million concurrent users and get back to us on how your server side session management for 12 million current users works out.
fuzzy lollipop
+1  A: 

You are absolutely right, supporting completely stateless interactions with the server does put an additional burden on the server. However, if you consider scaling an application, the computation power of the clients is directly proportional to the number of clients. Therefore scaling to high numbers of clients is much more feasible.

As soon as you put a tiny bit of responsibility on the server to manage some information related to a specific client's interactions, that burden can quickly grow to consume the server.

It's a trade off.

Darrel Miller
+1  A: 

Statelessness means that every HTTP request happens in complete isolation. When the client makes an HTTP request, it includes all information neccessary for the server to fulfill that request. The server never relies on information from previous requests. If that information was important, the client would have sent it again in this request. Statelessness also brings new features. It’s easier to distribute a stateless application across load-balanced servers. A stateless application is also easy to cache.

There are actually two kinds of state. Application State that lives on the client and Resource State that lives on the server.

A web service only needs to care about your application state when you’re actually making a request. The rest of the time, it doesn’t even know you exist. This means that whenever a client makes a request, it must include all the application states the server will need to process it.

Resource state is the same for every client, and its proper place is on the server. When you upload a picture to a server, you create a new resource: the new picture has its own URI and can be the target of future requests. You can fetch, modify, and delete this resource through HTTP.

Hope this helps differentiate what statelessness and various states mean.

CodeToGlory