13

By default Connection Header is set to Keep-Alive in browers, to make it possible to keep connection open for further requests from browser.

When I make Connection header to close, what may be the difference ? Will that affect any performance issue ?

(one addition: I am setting header from xmlhttprequest)

3
  • 3
    Why would you want to tamper with something like that?
    – bzlm
    Oct 19, 2011 at 14:38
  • 1
    i am getting Empty response from server for the request. i am taking this into consideration then
    – ajduke
    Oct 19, 2011 at 14:40
  • Maybe the statusCode: 204 no content was being set? This can interfere.
    – MarkHu
    Dec 22, 2016 at 7:16

3 Answers 3

20

When you make requests with "Connection: keep-alive" the subsequent request to the server will use the same TCP connection. This is called HTTP persistent connection. This helps in reducing CPU load on the server side and improves latency/response time.

If a request is made with "Connection: close" this indicates that once the request has been made the server needs to close the connection. And so for each request a new TCP connection will be established.

By default HTTP 1.1 client/server uses keep-alive whereas HTTP 1.0 client/server doesn't support keep-alive by default.

1
  • I think your answer about Connection: close in the request is wrong. According to HTTP/1.1 RFC, Connection: close in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent' after the current request/response is complete.
    – Adam Gu
    Jul 25, 2018 at 7:32
0

It affects performance, because most expensive resources create a socket between two machines. So the client needs to establish a new connection in every request.

7
  • That's assuming it's possible to change it from within an xmlhttprequest.
    – bzlm
    Oct 19, 2011 at 14:42
  • I need to along with this is that, is this create a request/response stream to server and keep this stream open forever for further communication with server or something else
    – ajduke
    Oct 19, 2011 at 14:44
  • @bzlm yes i am changing it from xmlhttprequest
    – ajduke
    Oct 20, 2011 at 11:57
  • @neworld so when one should set Connection to close ? can you give me some example .
    – ajduke
    Oct 20, 2011 at 11:59
  • @this-abhi: I think, you dont need close connection self. It's unnecessary. But I you totaly know you generate last request, you can close.
    – neworld
    Oct 20, 2011 at 17:09
0

this article has graphically demonstrated what would happen in such a when the connection header is set to closed and also keep-alive. It helped me understand it and I hope it helps you too. The Benefits of Connection Keep Alive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.