To KeepAlive or not to KeepAlive, that is the question

What do you guys do about KeepAlive?

I'm using Apache on a Ubuntu 10.04 LTS VPS with 512mb of memory. My website gets a fair amount of hits. Some requests involve multiple files too – the PHP file, and related image, CSS, and/or Javascript files. I'm trying to maximize performance so I can get the most bang for my buck, and delay upgrading to a larger Linode.

In a situation like this, what do you guys do (enable or disable KeepAlive?), and what do you recommend?

5 Replies

The usual rule of thumb is not to have keepalive. The idea is if you have a connection open to a client and you're not sending anything, that's wasted resources. This isn't the general consensus though, some people do argue for it - although even in that case they'll tweak it down to some optimal level.

In the end, it really depends on your situation. You really should look at your traffic and adjust for that.

Also, have you tried caching? That's usually the first thing to do and generally gives the greatest return.

Apache gets a worse rap that it deserves for memory consumption. That said, it isn't as memory efficient as it should be.

First, if you are running mod_php, then every worker carries memory overhead of having it loaded, whether the request is for a static file, or not.

Second, whether a request is for a static file or a dynamic page, the worker that processed the request is occupied until all the data is sent to the client.

These two issues are combined and aggravated when using keep-alives.

I think the simplest solution: Continue using apache, it works, its well understood, but let it spend its time and memory on servicing requests, rather than feeding data back out to clients. The way you do this is to put nginx on port 80 on your public IP, apache on localhost, and then set nginx to reverse proxy to apache and enable buffering of results.

You can try and set nginx up to handle requests for static files, and only proxy dynamic requests to apache, but I don't think its worth it.

I haven't tried caching. I don't know that it would work with my site. Stall results would be very bad given its nature.

I leave it on, but turn down MaxKeepAliveRequests and KeepAliveTimeout

@eas:

First, if you are running mod_php, then every worker carries memory overhead of having it loaded, whether the request is for a static file, or not.

Second, whether a request is for a static file or a dynamic page, the worker that processed the request is occupied until all the data is sent to the client.

These two issues are combined and aggravated when using keep-alives.

I think the simplest solution: Continue using apache
… but switch from mpmprefork + modphp to mpmworker + modfastcgi (NOT fcgid!) and have for example a hundred worker threads and PHPFCGICHILDREN=25. This way you have only one webserver to worry about.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct