A Web.com Partner

Apache Optimization: KeepAlive On or Off

Apache is the most widely used web server on the Inter­net. Know­ing how to get the most out of Apache is very impor­tant for a sys­tems admin­is­tra­tor. Apache Optimization is always a bal­anc­ing act. It’s a case of sac­ri­fic­ing one resource in order to obtain sav­ings in another.

What is KeepAlive

HTTP is a ses­sion less pro­to­col. A con­nec­tion is made to trans­fer a sin­gle file and closed once the trans­fer is com­plete. This keeps things sim­ple but its not very efficient.

To improve effi­ciency some­thing called KeepAlive was intro­duced. With KeepAlive the web browser and the web server agree to reuse the same con­nec­tion to trans­fer mul­ti­ple files.

Advan­tages of KeepAlive

Improves web­site speed: It reduces latency asso­ci­ated with HTTP trans­fers and deliv­ers a bet­ter user experience.

Reduces CPU usage: On the server side enabling KeepAlive reduces CPU usage. Con­sider that a typ­i­cal web page has dozens of dif­fer­ent files such as images, stylesheets, javascript files etc. If KeepAlive is dis­abled a sep­a­rate con­nec­tion must be made for each of those files. Cre­at­ing and clos­ing con­nec­tions has an over­head and doing it for every sin­gle file wastes CPU time.

Dis­ad­van­tages of Keepalive

Increases mem­ory usage: Enabling KeepAlive  increases mem­ory usage on the server. Apache processes have to keep con­nec­tions open wait­ing for new requests from estab­lished con­nec­tions. While they are wait­ing they are occu­py­ing RAM that could be used to ser­vice other clients. If you turn off KeepAlive fewer apache processes will remain active. This will lower mem­ory usage and allow Apache to serve more users.

When should you enable KeepAlive?

Decid­ing whether to enable KeepAlive or not depends on a num­ber of dif­fer­ent factors:

Server resources: How much RAM vs. how much CPU power you have? RAM is often the biggest lim­it­ing fac­tor in a web­server. If you have lit­tle RAM you should turn off KeepAlive because hav­ing Apache processes hang­ing around while they wait for more requests from per­sis­tent con­nec­tions is a waste of pre­cious mem­ory. If CPU power is lim­ited then you want KeepAlive on because it reduces CPU load.

Types of sites: If you have pages with a lot of images or other files linked into them, KeepAlive will improve the user expe­ri­ence sig­nif­i­cantly. This is because a sin­gle con­nec­tion will be used to trans­fer mul­ti­ple files.

Traf­fic pat­terns: The type of traf­fic you get. If your web traf­fic is spread out evenly through­out a day then you should turn on KeepAlive. OTOH, if you have bursty traf­fic where a lot of con­cur­rent users access your sites dur­ing a short time period KeepAlive will cause your RAM usage to sky­rocket so you should turn it off.

Con­fig­ure Apache KeepAlive settings

Open up apache’s con­fig­u­ra­tion file and look for the fol­low­ing set­tings. On Cen­tos this file is called httpd.conf and is located in /etc/httpd/conf. The fol­low­ing set­tings are noteworthy:

KeepAlive: Switches KeepAlive on or off. Put in “KeepAlive on” to turn it on and “KeepAlive off” to turn it off.

Max­KeepAliv­eRequests: The max­i­mum num­ber of requests a sin­gle per­sis­tent con­nec­tion will ser­vice. A num­ber between 50 and 75 would be plenty.

KeepAlive­Time­out: How long should the server wait for new requests from con­nected clients. The default is 15 sec­onds which is way too high. Set it to between 1 and 5 sec­onds to avoid hav­ing processes wast­ing RAM while wait­ing for requests.

Other set­tings

KeepAlive affects other set­tings in your Apache con­fig­u­ra­tion file even though they are not directly related. Here are the set­tings in an Apache pre­fork MPM webserver:

Max­Clients: Max­Clients is the max­i­mum num­ber of child processes launched by Apache to ser­vice incom­ing requests. With KeepAlive enabled you have will have a higher num­ber of child processes active dur­ing peak times. So your Max­Clients value may have to be increased.

MaxRe­questsPer­Child: The num­ber of requests a child process will serve before it is killed and recre­ated. This is done to pre­vent mem­ory leaks. When KeepAlive is turned on each per­sis­tent con­nec­tion will count as one request. That effec­tively turns MaxRe­questsPer­Child into a max­i­mum con­nec­tions per child value. As a result you can set a lower MaxRe­questsPer­Child value if you allow KeepAlive. If you don’t allow KeepAlive you should increase the MaxRe­questsPer­Child value to pre­vent exces­sive CPU usage.

Note :- There is no one uni­ver­sal solu­tion to tun­ing Apache. It all depends on the resources at your dis­posal and the type of sites you have. When used prop­erly KeepAlive can improve the user expe­ri­ence at min­i­mal cost in terms of server resources. But it can also be a lia­bil­ity when you are faced with a large num­ber of con­cur­rent users.

x
Chat