Forum Moderators: phranque

Message Too Old, No Replies

KeepAlive On or Off for better performance

Need help, thanks

         

AjiNIMC

7:05 am on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I was reading
[httpd.apache.org...] and found this line,

In some cases this has been shown to result in an almost 50% speedup in latency times for HTML documents with many images. To enable Keep-Alive connections, set KeepAlive On.

This can be a good improvement, should I do it for my server too? Any disadvantages?

Thanks,
Aji

jdMorgan

3:13 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

I bet you would have preferred a simple answer... ;)

Jim

AjiNIMC

1:01 pm on Mar 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah true, for images (and static files) I am planning to have a different web server with a 10-15 sec timeouts.

Thanks Jim, you are the best. Does any shared web hosts do it? Is there a way to check which all servers do, being at a client side?