Wednesday, January 21, 2009

Watch that Apache KeepAlive setting!

The Apache KeepAlive directive specifies that TCP/IP connections from clients to the Apache server are to be kept 'alive' for a given duration specified by the value of KeepAliveTimeout (the default is 15 seconds). This is useful when you serve out heavy HTML with embedded images or other resources, since browsers will open just one TCP/IP connection to the Apache server and all the resources from that page will be retrieved via that connection.

If, however, your Apache server handles small individual resources (such as images), then KeepAlive is overkill, since it will make every TCP connection linger for N seconds. Given a lot of clients, this can quickly saturate your Apache server in terms of network connections.

So...if you have a decent server that doesn't seem to be overloaded in terms of CPU/memory, yet Apache is slow-to-unresponsive, check out the KeepAlive directive and try setting it to Off. Note that the default value is On.

More Apache performance tuning tips are in the official Apache documentation.


hayden said...

I've been trying to find something difinitive on this topic. We have an image server it serves only, *.jpg, *.png, *.gif, *.css and *.js files.

We have keep alive set to 5 seconds. Would setting keep alive to off be faster?

Grig Gheorghiu said...

Hayden -- yes, try setting KeepAlive Off and see how it goes. Another thing that's obviously recommended is to use a CDN service to offload static files off of your servers.

abdussamad said...

Keepalive has a different affect on memory and CPU. Turning keepalive off actually increases CPU usage. I wrote a blog post with a detailed analysis of Apache keepalive.