Mike Q Mike Q - 1 year ago 61
HTTP Question

Is an HTTP request 'atomic'

I understand an HTTP request will result in a response with a code and optional body.

If we call the originator of the request the 'client' and the recipient of the request the 'server'.

Then the sequence is

  1. Client sends request

  2. Server receives request

  3. Server sends response

  4. Client receive response

Is it possible for the Server to complete step 3 but step 4 does not happen (due to dropped connection, application error etc).

In other words: is it possible for the Server to 'believe' the client should have received the response, but the client for some reason has not?

Answer Source

Network is inherently unreliable. You can only know for sure a message arrived if the other party has acknowledged it, but you never know it did not.

Worse, with HTTP, the only acknowledge for the request is the answer and there is no acknowledge for the answer. That means:

  • The client knows the server has processed the request if it got the response. If it does not, it does not know whether the request was processed.
  • The server never knows whether the client got the answer.

The TCP stack does normally acknowledge the answer when closing the socket, but that information is not propagated to the application layer and it would not be useful there, because the stack can acknowledge receipt and then the application might not process the message anyway because it crashes (or power failed or something) and from perspective of the application it does not matter whether the reason was in the TCP stack or above it—either way the message was not processed.

The easiest way to handle this is to use idempotent operations. If the server gets the same request again, it has no side-effects and the response is the same. That way the client, if it times out waiting for the response, simply sends the request again and it will eventually (unless the connection was torn out never to be fixed again) get a response and the request will be completed.

If all else fails, you need to record the executed requests and eliminate the duplicates in the server. Because no network protocol can do that for you. It can eliminate many (as TCP does), but not all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download