Improving your server performance/latency with unique KeepAlive and KeepAliveTimeout settings in an Apache environment.
KeepAlive is a server variable located in your configuration file. For Apache its usually under the path /etc/httpd/conf/httpd.conf
I see this servoptimization techniques around this variable highly debated. The best setting is different on a per server basis. Here’s how I go about setting mine.
First, the only time I would ever keep it OFF entirely is if 80%+ requests are servering up simple plain looking HTML documents. How simple? Imagine opening up notepad and throwing some text in.
That’s maybe too simple, but you get the picture. Suffice to say I’ve yet to find a scenario in my own work where it wasnt beneficial to have on.
Sites that benefit most from “KeepAlive ON”
- Have not combined their CSS/JS into one file. Though if you did you could probably lower the KeepAliveTimeout setting a bit (explained later).
- Do not use a CDN (content delivery network) like the Amazon S3 cloud.
- Have a lot of images loading on the page.”Spriting” is the new big thing, you should look into it.
This is because KeepAlive ON keeps the last used TCP/IP connection for a set amount of time (KeepAliveTimeout) after the first request. If the connection is still open the client doesn’t have to repeat these steps for each item:
- Look up IP address of server in DNS.
- Establish a TCP/IP connection to server.
- Send the HTTP request.
And by “item” I mean every single js, css, and image that is loading on the page.
Setting a proper KeepAliveTimeout is the trickiest because:
- Setting it too high ties up your connections when a client already finished loading all page elements X seconds ago.
- Every user has a different connection speed – This should be a minimal issue these days since almost everyone is on DSL/broadband or better.
Setting it too low and having the client send those requests an extra time is better than a separate connection for every item. Thus my rule of thumb is put it a bit lower than you need it when in doubt.
Here we’ll do a simple task to help outline how to define what your optimal KeepAliveTimeout Setting is.
- Go to http://www.webpagetest.org and enter in your URL.
Note: Enter your url so that doesn’t 301 redirect. IE if you’ve forced your domain to be www.domain.com, entering in domain.com (without www) will do a 301 to the www version. This will skew your results a bit but you can always subtract it out too
Extra: Firebug: Net > All has a similar functionality as shown below. However this method utilizes your current connection. As mentioned above you might need to add a bit of a bias on the final times you record if your connection is not considered “the average”.
You might also want to tune your KeepAliveTimeout to your core “article” pages rather than the home page. You can check your analytics to see what area of the site gets hit the most. This is because most of us have different layouts for our front page than article pages.
- You’ll get a few charts in a table when it finishes.
- Do this 3 times and take the average of the “First View” number in the top left of the table.
- Add 2 seconds for a bit of headway on that number and you have your KeepAliveTimeout value.
Note: You might want to add more lead way IF you have hardware to support more max connections. Losing a bit of time before a connection closes probably isn’t as costly to you then as dropping it too soon if there is room to spare.
Applying KeepAlive settings on your server
This should cover the most common cases. Login to your server via SSH and edit in the modification:
cd /etc/httpd/conf/httpd cp httpd.conf http.conf.bak nano httpd.conf
Press ctrl+W and do a search for ‘KeepAlive’ to jump to it quickly.
KeepAlive ON KeepAliveTimeout X
Replace “X” with the number you came up with in the process above.
You probably also see MaxKeepAliveRequests in there too. You can see in my results I have about 25 requests. I usually set this high at 150 or 200 which essentially is unlimited requests from any normal page I’ve visited. If you have much more than 50 then your site is probably in bad need of optimization in other places!
Here is the Apache 2 documentation to reference all three server config settings mentioned here: http://httpd.apache.org/docs/2.0/mod/core.html
Entrupeners, Subscribe for the lastest tools, tips, and tutorials.