Posts Tagged ‘http’

Optimizing your web server: Part 2 – Keep Alives

November 22nd, 2009

Once your site begins to grow, it will most likely contain dozens of images, css and js files, and many other requests.
Keep alives allows those requests to happen over the same tcp/ip connection.

So what is keep alive exactly?
As mentioned before, keep alive is an option in the apache config, this was added to the http protocol to solve a huge performance problem.

In the original http protocol a new tcp connection needed to be set up for each request(image, css, html file).
Due to the nature of the tcp protocol it takes a while to set up a connection(3 packets are always send, SYN/SYN-ACK/ACK). If the latency to the server is 200ms, then it takes about 600ms before a connection is set up and data can be requested.

By using keep alives, this can be minimized.
So on almost all of the webservers it gives a performance boost, as sites nowadays exist out of allot of files.

Enabling keep alives is pretty easy.
Apache has 3 settings that involve keep alive:

  • KeepAlive
  • KeepAliveTimeout
  • MaxKeepAliveRequests

The first “KeepAlive” has has two settings, On or Off, obviously we’re going to put in on ‘On’.

The second “KeepAliveTimeout”, is the number of seconds a connection stays open for the client.
Setting this greatly depends on your website. If it loads in less then a second, settings this on 2 or 3 seconds is enough.
I’ve put this on 5(default is 30) in my configuration and the site in question loads under one second.

The third “MaxKeepAliveRequests”, are the maximum number of requests that can be made on an open connection. This can be put on ‘0′ meaning unlimited, or if you really want to put a number, take a look at how many requests your site makes.

Optimizing your web server: Part 1 – Gzip
Optimizing your web server: Part 2 – Keep Alives
Optimizing your web server: Part 3 – Opcode Caching
Optimizing your web server: Part 4a – PHP
Optimizing your web server: Part 4b – XDebug Profiler