Apache: Keepalive On or Off ?

Posted: 16/02/2012 in Apache
Tags: , ,

A good answer to the above question that i found on this forum
Keepalive keeps the current TCP/IP connection open for awhile after the HTTP transaction ends, allowing that same connection to be used for several subsequent HTTP transactions. So it reduces latency, and speeds up both the client and server connections.

To illustrate, here’s a non-keepalive session with the client requesting two objects:
Client: Look up IP address of server in DNS
Client: Establish TCP/IP connection to server
Client: Send HTTP request #1
Server: Serve requested resource
Server: Terminate HTTP transaction #1
Server: Terminate TCP/IP connection
Client: Look up IP address of server in DNS
Client: Establish TCP/IP connection to server
Client: Send HTTP request #2
Server: Serve requested resource
Server: Terminate HTTP transaction #2
Server: Terminate TCP/IP connection

And now again with Keep-alive:
Client: Look up IP address of server in DNS
Client: Establish TCP/IP connection to server
Client: Send HTTP request #1
Server: Serve requested resource
Server: Terminate HTTP transaction #1
Client: Send HTTP request #2
Server: Serve requested resource
Server: Terminate HTTP transaction #2
Server: Terminate TCP/IP connection

Obviously, eliminating three steps is going to make these transactions more efficient.

However, leaving the TCP/IP connection open ties it up, and precludes its use by another client (user) for whatever time remains in the keepalive “timeout” setting. So, this setting should be adjusted to a reasonable period in order to minimize wasted connection timeout time, and to release those server threads for useful work instead.

So generally, using keepalive is good, but you don’t want to “keep the connection alive” too long. How long is too long depends on the nature and size of the “typical” files retrieved from your site by a “typical” user. Never mind the details, even determining what is “typical” is difficult. So the best approach is often just to do a few trial-and-error tests, and see if you notice any improvement in page-load times and server responsiveness, while monitoring the processes to be sure you don’t run out of threads for new connections. Increase the keepalive time to the point of diminishing returns, and then decrease it to minimize wasted server threads. Most benefits will be seen even with short (10-15 second) keep-alive settings, with the maximum well below one minute.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s