Setting Apache2 ulimit for Maximum Performance - Web Performance
Menu

Setting Apache2 ulimit for Maximum Performance

As strange as it seems, when you start up the standard RedHat or CentOS release, it is not configured for running Apache2 with any sort of performance! In fact, even on a large server you’ll be lucky to handle 100 concurrent users on even simple web pages. So many websites have very few visitors so often its not noticeable, but even if you adjust all of the other apache2 settings, if you don’t bother to adjust the ulimits, the site will be handicapped by really bad performance.

Web Performance Consulting

First, in running the tests I decided to stick with the default prefork MPM. There’s dozens of sources on the web for configuring Apache prefork for maximum performance that I won’t repeat here. Typically, though, the first thing to do is set a high number of MaxClients, which really isn’t that high considering a “client” is a socket connection, and modern browsers can open eight socket connections at a time. This turns the “8192” below into only 1,024 actual concurrent users. Of course its possible to set MaxClients higher than that, but with a typical test scenario only 500 clients can potentially max out the available bandwidth and CPU, so this isn’t a bad place to start:

StartServers 20
MinSpareServers 25
MaxSpareServers 100
ServerLimit 8192
MaxClients 8192
MaxRequestsPerChild 10000


So, what type of performance do you get with these settings? The testcase I used was browsing our company website, configured as static pages, with 4 second think times and a page-load goal of 2 seconds per page. Even though it should have been able to handle at least 512 browsers, the webserver started crashing at less than 100 concurrent users.

As it turns out, despite setting MaxClients to 4096, the system never actually reaches that level because the compiled in limit for the number of processes is 1024, as can be seen below:

[root@faith ~]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 59400
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 1024
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

The fix on RedHat/CentOS is to edit the file /etc/security/limits.conf and these lines, adjusting the limits based on your system capacity and needs.

* soft nofile 10240
* soft nproc 10240

Restarting Apache and Re-running the tests resulted in a huge increase in performance, increasing the capacity of the server to around 600 concurrent users.

8 Comments

9 December 2012 Lukas Rysiak

your ulimit is from after the change – 10240 limits, not 1024.
Could you re-run this test with smaller page to see the actual apache limit – not NIC?

10 December 2012 Michael Czeiszperger

I don’t understand the question. The before and after stats are given in the article.

15 February 2014 ekopranoto

what tools used for graph ?

10 March 2015 Michael Czeiszperger

The charts are from within Load Tester, and create by JFreeChart.

26 March 2015 mlist

Hi Michael

before all my best compliment for the clearness of your post. I have some doubt that I hope you will help to fire out

1) I read tens of howtoo but you are the only one that explains the “browser open up to 8 connections” factor.
I have a virtual machine with 8GB of ram; about 4 GB used by other applications and Apache process memory (res) is about 50mb
Apache should be able to manage 80 simultaneous connections and is prefork MPM
So, my calculation about MaxCliens is:

((8-4) * 1024) / 50 = 81

but, if I have correctly understood, due to the “8 connections factor”, I should multiply 81*8 = 648. This means that I would also need 64GB of RAM? (calculated on 648*8/81)
Correct?

2) Why browser open so many connections?

3) KeepaliveTimeout
On some websites I read that decreasing the timeout to 2/4 seconds, performance are hugely increased.
In your opinion, is this always true or depend on the website? For example, supposing that I’m using an exam training webapplication in which users spend up to 2/3 minutes before to change the page, wouldn’t make sense to increase Keeepalive up to 120 seconds instead to reduce it?

4) In case of worker MPM, what should be a good configuration about MaxClients and ThreadsPerChild?

Thanks
Marco

26 March 2015 Michael Czeiszperger

1) Apache can manage its memory for that number of connections, so you wouldn’t need 64G of RAM. Its pretty difficult to predict, just try the new settings and see what happens.

2) Browsers open multiple connections for performance reasons. As it turns out, 6 sockets, for example, simultaneously downloading connect from a web server is faster than 1, even for static content. The reasons are too complicated to go into here. Note that 8 is the max we’ve seen, different versions of each browser open different numbers of simultaneous socket connections.

3) I recommend testing the keep alive timeouts and see what value works best for your application.

4) Sorry, Google is your friend 🙂 Again, since we’re a testing company, we recommend starting from a ballpark figure then get a feel for what happens yourself by sending test loads at your web server.

26 March 2015 mlist

Michael thanks again for your kind reply.

just last question about point number 1.
I have understood that is difficult to predict and that I need test, but, can you at least tell me if my math does make any sense or if 8GB could be even enough? Does it just depend on the application type or are there other factors?

17 April 2019 Apache Performance – Web Development

[…] Ulimit https://www.webperformance.com/load-testing-tools/blog/2012/12/setting-apache2-ulimit-for-maximum-pr… […]

Add Your Comment

You must be logged in to post a comment.

Resources

Copyright © 2024 Web Performance, Inc.

A Durham web design company

Ă—

(1) 919-845-7601 9AM-5PM EST

Just complete this form and we will get back to you as soon as possible with a quote. Please note: Technical support questions should be posted to our online support system.

About You
How Many Concurrent Users