27

In any default installation, Apache 2 comes with keepAlive off, but looking at another server, the keepAlive module was turned on.

So, how do I know if keepAlive is right for me? Where can I find some good examples about configure this?

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Gabriel Sosa
  • 1,200
  • 1
  • 11
  • 13

3 Answers3

33

There are 2 good answers already, but the perhaps most important real-life issue is not mentioned yet.

First off, the OP might want to read the 2 preceding answers and this little blog post to understand what keepalives are. (The author doesn't elaborate on the part about TCPI/IP getting "faster" the longer the connection is open. It is true, longer-lasting connections benefit from IP window scaling, but the effect isn't significant unless the files are large, or the bandwith-delay product is unusually large.)

The big argument against HTTP Keepalive when using Apache is that it blocks Apache processes. I.e. a client using keepalives will prevent 'his' Apache process from serving any other clients, until the client closes the connection or the timeout is reached. In the same span of time, this Apache instance could have served many other connections.

Now, a very common Apache configuration is the Prefork MPM and a PHP / Perl / Python interpreter, and application code in the mentioned language. In this case each Apache process is "heavy" in the sense that it occupies several megabytes of RAM (Apache linked with interpreter and application code). This, together with the blocking of each keepalive'd Apache instance, is inefficient.

A common workaround is to use 2 Apache servers (both on the same physical server, or on 2 servers, as needed) with different configurations:

  • one "heavy" with mod_php (or whatever programming language is used) for dynamic content, with keepalives off.
  • one "lightweight" with a minimal set of modules, for serving static content (image, css, js etc), with keepalives on.

You can then expand on this separation of dynamic and static content when needed, for example by:

  • using an event-driven server for static content, such as nginx.
  • using a CDN for static content (could do all static content serving for you)
  • implementing caching of static and/or dynamic content

Another approach with regards to avoid blocking Apache is to use a load balancer with smarter connection handling, such as Perlbal.

.. and much more. :-)

  • 2
    Are these answers still relevant 8 years later? – TheStoryCoder Aug 27 '17 at 11:06
  • 1
    Yes, still relevant if you are using the prefork MPM. Note that Apache httpd 2.4 (eg. in RHEL7) uses KeepAlive On by default (but does not explicitly list it in its configuration -- at least in RHEL7). – Cameron Kerr Dec 07 '18 at 01:14
6

Keepalives can be good in some cases, they can be very bad in others. They reduce the time and effort of setting up a new connection, but they tie up server resources for the duration of keepalive timeout. Examples:

  • Pages with many small objects, clients on dialup - keepalive should be on.
  • Pages with a few large objects - keepalive won't be an advantage.
  • Server with very high number of unique visitors - keepalive should be off (otherwise, sockets and threads will be sitting in memory waiting for the keepalive timeout and not serving new clients).

As you can see, KeepAliveTimeout will also play a large role in optimization of your server performance.

Look at your usage pattern and decide for yourself.

Max Alginin
  • 3,284
  • 14
  • 11
-1

You should definitely use KeepAlive On.

See:

http://httpd.apache.org/docs/2.0/mod/core.html#keepalive

That way a single TCP connection will be re-used by the browser to send multiple queries. Usually a website has many components (HTML page, javascript code, images). As long as these resources are in the same domain, therefore can be served by the same server, a KeepAlive connection gives a huge boost in performance since the browser won't have to establish a new TCP connection.

A browser typically opens around 3 parallel connections to a domain. So let's say you have 18 objects in your site. The browser would open 3 connections, and it would download 6 objects in each connection - using the KeepAlive mode. Without KeepAlive, it would have to open 18 TCP connections, which is very slow.

Most, or all modern browsers are HTTP/1.1 compliant so this should just work.

Certain HTTP proxies like Squid are not HTTP/1.1 compliant, but they request the use of a KeepAlive connection anyway.

Yves Junqueira
  • 671
  • 3
  • 7
  • 1
    This is only from client side consideration, while I suppose server side resource usage is also important. – Morgan Cheng Aug 14 '10 at 10:22
  • Server side resource usage is more important than user-perceived latency? – Yves Junqueira Mar 10 '11 at 22:46
  • 2
    I also believe in turning KeepAlive On, however Apache's default timeout of 15 seconds are far too generous since it keeps processes blocked for too long. I usually set the timeout to around 2 seconds, which results in KeepAlive being used during about one pageload. – Martijn Heemels May 04 '12 at 22:28