network-programminghttp-1.1

keep alive, what is the difference between HTTP1.1 and TCP


There are two headers relate to Http keep alive,

Connection: Keep-Alive
Keep-Alive: timeout=5, max=1000
  1. Which side sends HTTP1.1 "Keep-Alive: param"?
  2. When client and server use HTTP1.1, will client send TCP keep alive probe?
  3. In order to use HTTP1.1 keep alive, must client set Connection: Keep-Alive or Keep-Alive: param?

I have above qustions is because:

Recently I observed a problem using http client. After 2 hours since last time client sends request, once client sends request it would never hear response from server. In order to find reason, I did:

  1. having tcpdump runs on both client and server sides.
  2. client sends one request (and receives response)
  3. wait for 2 hours
  4. client sends another request (but does not receive any response) <-- this is the problem.

Between the two requests, pcaps on both sides show that no one sends FIN. And server does not receive the second request. I think something is messed up with HTTP1.1 keep alive transmission.


Solution

  • For Questions 1 and 3, see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive

    For question 2, see: HTTP Keep Alive and TCP keep alive

    If you have e.g. NAT between the client and server, 2 hours can be too long for an idle connection to stay functional, but then again if neither side sees a fin, then someone is not handling things according to spec.