What is Connection Keep-Alive?

Asked

Viewed 18,896 times

25

When I check the headers I’m sending on a php page, which is installed locally, I always see this Connection: Keep-Alive.

Example:

var_dump(getallheaders());

Exit:

array (size=7)
  'Host' => string '127.0.0.1' (length=9)
  'User-Agent' => string 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0' (length=76)
  'Accept' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' (length=63)
  'Accept-Language' => string 'pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3' (length=35)
  'Accept-Encoding' => string 'gzip, deflate' (length=13)
  'Cookie' => string '_ga=GA1.1.1775431020.1436189521' (length=31)
 
  'Connection' => string 'keep-alive' (length=10)
  • What does it mean Keep-alive?
  • What’s he good for?

1 answer

31


A connection Keep-Alive means a persistent connection, or a persistent life connection, between the client and the server. Preventing the connection from breaking intermittently.

The connection HTTP default is usually closed after each order has been completed, which means the server closes the connection TCP after delivery of the reply. In order to keep the connection open for multiple requests, the connection header keep-alive can be used.

In the image it is clear how Keep Alive works.

Exemplo Keep Alive

Perks:

  • Speed up the website: Reduction in latency in HTTP downloads.
  • Reduces CPU usage: Consider that the website has multiple images, files, a connection will be required for each file, increasing the use of CPU, using keep-alive, only one connection is made, thus reducing the use of CPU.

Disadvantages

  • Increases the use of memory: Enabling Keepalive increases memory usage on the server. Apache processes have to keep open connections waiting for new established connection requests.

References:

W3
Maxcdn
Varvy
Abdussamad

  • 2

    From the explanatory text of W3 (https://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01#Connection) the image (not the text) seems to describe the behavior incorrectly. She gives the idea that the form of packet traffic is different on the way when in fact there is only a reduction of time on the server because it does not close the connection. Another misconception in the image is that we understand that Keepalive is a protocol other than HTTP. The right one would be "With Keepalive" and "No Keepalive".

  • 3

    Take the liberty of editing the answer by changing the image caption. It really gets the impression that HTML and Keep Alive are separate protocols.

  • Seeing the example image is much easier to understand. + 1 For it.

  • 2

    A small addendum, since HTTP/1.1 connections are persistent by default, that is, unless the client is HTTP/1.0 there is no big reason to send the header Connection: keep-alive to the server.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.