Is it possible to get responses out of order with HTTP?

Section 8.1.2.2 Conveyor processing says:

"The server MUST send its responses to requests in the same order as the requests were received."

So, I thought: if I send several AJAX requests from my browser, they will still be processed in the order in which they were received by the server.

But then I read this post from Alex Maccaw, where he states:

"The last problem is related to Ajax requests that are sent in parallel. If a user creates a record and then immediately updates the same record, two Ajax requests will be sent at the same time, POST and PUT. However, if the server processes the 'update request 'before he “creates,” he will worry. He has no idea which record needs updating because the record has not yet been created.

The solution to this is for Ajax pipelined requests that pass them sequentially. the spine does this by default, in the queue are POST, PUT and DELETE Ajax requests, so they are sent one on time. The next request is sent only after the previous successfully returned.

So how can I programmatically create the script that Alex McCau is talking about?

+6
source share
3 answers

I think in short, the answer to your question is yes.

HTTP 1.1 does not prohibit opening multiple TCP connections on the same server (in fact, it recommends two), and all modern browsers do this (in fact, most of them are six or more). See Maximum Number of Parallel http Connections in a Browser? . A request-response cycle can be performed on each of these connections, and some of the request-response cycles can be much faster than others for various reasons. Network congestion, request complexity, speed and loading of a specific "working" of your request are processed, etc. This means that the request-response cycle for a later-started request can easily end earlier than the cycle to start the request earlier.

"The server MUST send its responses to requests in the same order as the requests were received."

This operator is used only for pipelining several HTTP requests, that is, sending multiple requests over the same TCP connection without waiting for a response to each request. This does not apply to opening multiple TCP connections on the same server.

Usually you only have one tcp connection request going at a time. The client expects a response, and when it receives a response, it may reuse the connection for a new request. Thus, with regard to regular (non-pipelined) http, there is even a concept of "response order", since only one request-response cycle occurs in a TCP connection.

In pipelining, multiple HTTP requests are launched over the same TCP connection. It is important to return the answers in order, because the way the answers correspond to their initial requests. (Coordination of responses to requests could be done in different ways, for example, by providing a hash of a complete request for each response or so, but it is not).

It's also good to know that the default support for HTTP pipelining is not wide. The Chromium project is reluctant to include: https://insouciant.org/tech/status-of-http-pipelining-in-chromium/ . Firefox still does not support it. https://bugzilla.mozilla.org/show_bug.cgi?id=264354

Apple, on the other hand, has turned on iOS5 safari support, perhaps because waiting for a response request on a mobile phone is a big problem. http://www.guypo.com/mobile/ios5-top10-performance-changes/ Android browser for browsers too. At least for the version before Chrome. http://www.guypo.com/mobile/http-pipelining-big-in-mobile/

Alex Mackaw wrote in a post about Spine, which you quote:

The solution to this is the pipeline of Ajax requests that transmit them in serial.

I think the term pipeline is somewhat confusing in this context. First of all, the “pipelining” that Spine does is something completely different from the ability to pipelining requests in HTTP. Secondly, I think I would name this feature of the Spine request queue. Spine queues requests and processes the items in the queue so that they are added.

In general, I think that the term “pipelining” is best used when things are done purposefully faster, and “ordering” is best used, making things purposefully slower (to prevent race conditions or lighten the load on the processor, for example, in a queue).

+7
source

They will not be processed synchronously unless WebServer has only one thread to handle requests that can only happen intentionally (possibly in a dev environment). In most cases, the web server has hundreds of threads available to process requests as they arrive, and since one request may take longer than others, the responses may fail. That is why it is called A (asynchronous) JAX.

1 request may take a full second to respond, while request 2 and 3 takes 25 ms to respond, so the first response request comes after the 2nd and 3rd requests.

You can force a synchronous cycle, but at the cost of all the stop until the request is returned and processed (even JS loaders will stop).

See this article on forcing synchronous requests (SJAX). http://www.hunlock.com/blogs/Snippets:_Synchronous_AJAX

+1
source

RFC 2616 uses the term "pipelining" to refer to this:

  • HTTP requests and responses can be pipelined in the connection. Pipelining allows the client to execute multiple requests without waiting for each response, allowing one TCP connection to be used much more efficiently, with much less time elapsed.

Thus, pipelining means using a persistent HTTP connection to execute multiple requests. This is the functionality that should be supported by the browser, and currently it is either not supported or disabled by default for almost all browsers. So, right now, when you make some simple AJAX requests, there is always a chance that the answers will return out of order.

In most cases, this is not a problem. When this is the case, you can take appropriate steps to "pipeline" the requests manually; they still go on separate connections, but your code is designed to wait for the pending request to complete before issuing the next one. In the article you link to, this functionality is built into Spine.

+1
source

Source: https://habr.com/ru/post/943911/


All Articles