Jump to content

correlation between stateless and async

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
danisroyi

danisroyi

    Newbie

  • Members
  • Pip
  • 4 posts
I have a generic question which is related to the HTTP protocol (or any other protocol for that matter)
I know that HTTP is stateless.
Does this mean that it is also async ? Is there any correlation between stateless/statefull and async/sync ?
Or are they independent mechanisms ?

Thanks in advance,

Dani

#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
They are different concepts and not need to be related.

HTTP is stateless because any information processed in one request is destroyed just after the answer is returned to the client. Any subsequent request cannot use previous information unless the client sends it again (cookies and some other things are used for this purpose).

Asynchronous in general means that some request is made and the client can do other things while it is waiting for the answer. On the other hand, synchronous means that the client stops working (it is blocked) until the server sends the answer.

You can make synchronous and asynchronous requests to an HTTP server, but both will be stateless.

#3
danisroyi

danisroyi

    Newbie

  • Members
  • Pip
  • 4 posts
Thanks !
That cleared it out for me.

#4
danisroyi

danisroyi

    Newbie

  • Members
  • Pip
  • 4 posts
After digesting your very clear answer I have anthor one:

When would you preffer to implement a sync request and when a async request?
What could be the advantage/disadvantage of each?

#5
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
It depends on what you need to do...

Synchronous or asynchronous is something only relevant on the client. From the server perspective, it will be indistiguishable. Programming synchronously is quite easy. Yo make the request and know that your program won't continue executing until the request has finished so your next line of code after making the request can be sure that the data is ready and can be processed. In a very high level pseudo-code:

page = request_page(url);

draw_page(page);
Depending on the operating system, the type of call and some other things, this request may not be stoppable, what might be a really big problem. Supose the server where you have sent the request is unresponsiveness and the user wants to close the program. He cannot, the program is blocked waiting that the server sends something. You will have to wait a timeout or kill the process.

The same situation using asynchronous communication is harder to implement but offers a greater level of control to the user. When you do an asynchronous request, the operating system returns the control to your program inmediately (even if the request has not been sent yet). This means that the next line after making the request is not guaranteed to have the data available. In fact, the operating system will call some specific function when the requested data will be available. In pseudo-code:

info = request_page_async(url, callback);

...do other things...
Notice that there is a new argument to the request call and the return value is not a page, it's some sort of control information about the request in progress. The code after the request doesn't have nay page, so it cannot do anything related to the page. The new argument (callback) tells the operating system what function to call when the page is received (this function receives the result of the request as a parameter).

function callback(page)

{

    draw_page(page);

}
This happens totally asynchronously and, many times, in another thread.

This has the advantage that if the user wants to close the program, generally there is a function to cancel a pending request using the control information returned by the asynchronous call:

cancel(info);
Another great advantage of using asynchronous request is that a single thread can handle multiple requests, which optimizes resources and speeds up the application. In a synchronous model you need one thread for each request (very high resource usage) or serialize the requests (very slow).

This is a very high level aproximation. There are more details to take into account when implementing these things. Sometimes it can be very complex to do all what you want to do...

In general you write synchronous programs when the number of requests is small and you don't need to do anything else while waiting. Or when the effort to implement an asynchronous solution does not compense the benefits... it always depends on the specific circumstances of each problem.

#6
danisroyi

danisroyi

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you for the very well detailed and clear explanation.