Your web server can be set up to keep connections alive for a short time between requests, just in case there's another request to come in. This can have dramatic performance improvements. Below image shows the Web Page Test waterfall for loading this page with Keep-Alive off and on:
As you can see the top image has a lot more orange (Initial Connection) setup and because this site uses HTTPS we get also see purple (SSL Negotiation) sections, so a double hit. This results in an extra 0.4 milliseconds (or 33seconds), even just for the first 6 resources shown above (one of which is a Google Analytics resource so will need a separate connection anyway even after I have re-enabled Keep-Alive). What's also interesting is that Web Page Test has only given me a green B rating when I have Keep-Alive off. This is because this page loads a few other things (Google Analytics, Twitter buttons and the Disqus commenting system) and all of these have Keep-Alive set to on. So overall the page has mostly Keep-Alives on, however the one I have control over is not on! So always be suspicious if this is not set to A.
How to set it up
Keep-Alive is something that should be on by default on most web browsers. You can however tweak the settings if necessary. When Keep-Alive is turned on the web server will return a HTTP-Header to the web browser saying to keep the connection alive, and how long for.
Connection:Keep-Alive Keep-Alive:timeout=5, max=100
This means allow the connection to be kept alive, for 5 seconds and for a maximum of 100 resources. These settings are controlled by settings on the web server. The default config for Apache, for example, is shown below and these are pretty good settings for most sites:
# # KeepAlive: Whether or not to allow persistent connections (more than # one request per connection). Set to "Off" to deactivate. # KeepAlive On # # MaxKeepAliveRequests: The maximum number of requests to allow # during a persistent connection. Set to 0 to allow an unlimited amount. # We recommend you leave this number high, for maximum performance. # MaxKeepAliveRequests 100 # # KeepAliveTimeout: Number of seconds to wait for the next request from the # same client on the same connection. # KeepAliveTimeout 5
Note that these settings can only be set at the server or vhost level and not in .htaccess. Some websites claim you can just set the header manually if you do only have access to the .htaccess file but that is not true: although that might make the the client think the server supports keep-alive, the server will just go ahead and close the connection anyway.
Keep-Alive is a standard part of HTTP/1.1 which every major web browser and web server has supported shortly after it came out in 1997.
There is very little downside to using Keep-Alive and it's a standard performance improvement which is usually enabled by default. Each connection does use up resources of course (on both the client side, and on the server side) so you would not want to keep the connections alive forever or your web server will become overloaded, but with the settings above for example this should not be a problem. You can knock the KeepAliveTime out setting down a bit if it does start to prove a problem.
HTTP Keep-Alive is a standard that pretty much all web servers should (and usually are) using. The performance benefits are huge, and the downsides low. A similar option, to reuse SSL connections, is SSL Session Resumption.
Want to read more?
This page was originally created on and last edited on .Tweet