How to Work Out Max Clients for Your VPS or Dedicated Server running Apache

If you have a reasonably powerful server then the chances are your probably not getting the most out of it. This is more so important for sites that have a lot of consistently heavy traffic.

You can have all the resources in the world, but if Apache doesn’t reflect that then your wasting your money, or your not spending enough of it ;-).

It is equally true that if you don’t have enough resources then you will hit what I like to call THE WALL. The wall for me is when you run out of resources such as RAM, at this point the server has no choice but to then start swapping on to a hard drive, this is an expensive process and too many of these will cause the CPU to spike and if the requests are consistently pushing you in to swapping then problems are likely to occur.

Problems such as the server or Apache crashing as a result of the expensive processes accumulated by swapping.

So we need to determine whether our resources are being utilized, or if there simply isn’t enough of them.

Logging in to WHM

logging in to whmYou can log in to your Web Host Manager (WHM) via

  • http://sitename.com:2086
  • http://sitename.com/whm

This will take you directly to WHM, you may get a warning This Connection is Untrusted, but don’t worry about that, just select “I understand the risk” and be on your way.

The username for your WHM panel is always root, the password will likely be the one you were given in your initial email when the server was setup unless you changed it.

Once logged in you will see a sidebar with a long list of various options related to your server. If you are familiar with WHM you will know where everything is, or at least approximately where they are. If not, it does have a search function ( top left).

Max Clients

By default an Apache web server will be set to 150 max clients.

We can see this by going to Service Configuration > Apache Configuration

Once loaded, select Global Configuration

apache global configuration

Inside Global Configuration you will see a list of available options for you to change.

Scroll down until you see Max Clients, if this has not been changed, then it should still be at 150.

Before we move on to how to actually work this value out, we should first know what max clients is.

What is max clients?

The text book definition is

“This directive sets the limit on the number of simultaneous requests that will be served. This interface allows up to the value of the ServerLimit setting.”

Simultaneous requests simply means if 150 people all visited your site at the same time, they would all be served at pretty much the same time, but if 200 people showed up then 150 people will be served but 50 people will have to wait until a slot is open, think of waiting outside a popular restaurant for a seat to clear.

So we can agree here that for most of us the default value of 150 will do the job just fine.

Just so we know what I mean by available slots, if you go to Server Status > Apache Status you will see something that looks remarkably similiar to this.

apache service status

If you scroll further down on Apache Status you will see a Key that will tell you what the letters and symbols mean.

The above example tells me there are

  • 6 requests at the moment I refreshed the page
  • 119 idle workers represented by “_” (Waiting for Connection)
  • 5 connections closing represented by “C”
  • 1 sending reply represent by “W”

Ok so now we have learned at little about max clients, what its for etc, but now we need to work out whether this value is sufficient enough.

If Max Clients is set too high, or too low, then this can lead to problems on your server. But working out what value to set is often deemed a little confusing at times. I assure you its not, it is pretty simple, but you will need to login via SSH as root to do this.

If root SSH is disabled you will need to contact your host to enable it.

The first step in this process is to gather up some information, after all, we can’t work anything out unless we have some numbers.

Collecting Our Data

Here is a list of information that we need to find out.

  1. How much RAM does our server have?
  2. How much RAM does our operating system (OS) use?
  3. How much RAM does MySQL use?
  4. How much RAM does an average Apache process use?

Once we have collected the above information, we can then work out whether our max clients is too high, or too low.

Remember the out of the box Apache install doesn’t care how much Ram you have, whether it’s 1GB or 16GB, the defaults are the same, so as an owner of a VPS or Dedicated server it is important that you ensure that the values in your Apache config reflect the resources available to you.

You can do this by yourself, but if you are not confident enough to do this, you can hire someone to do this for you. I do perform this service for all my clients running VPS and Dedicated servers, so if you need some help here, please contact me.

The equation for working out max clients is:

max clients = total amount of RAM – ( Max RAM allocation to MySQL + amount of RAM used by OS) / average size of Apache process

I tried to make it look pretty, honestly 🙂

How much Ram does our Server have?

If your logged in to WHM, this part is easy.

Go to Server Status > Service Status

You will be finely presented with the following…

whm service status

…and there it is.

3.5GB of available RAM, now luckily for me this server automatically deducts the RAM used by the OS, so in reality I have 4GB of RAM with 512mb assigned to the OS.

So that’s points 1 and 2 covered.

But what if your server doesn’t automatically deduct the RAM used by the OS?

How to find out how much RAM our OS uses?

You can use SSH to find out this value.

There are a few ways to log in via SSH, one is by username and password, the other with a public or private SSH key.

I am not going to get in to using SSH in this post, so you must be comfortable with using SSH connections if you need to find this out.

Once logged in to SSH you need to stop Apache and MySQL

use the following commands.

service httpd stop

service mysql stop

Then run this command

ps aux | awk '{sum1 +=$4}; END {print sum1}'

This will output a number

In my case 12.2mb, but I already know that 512mb is assigned to the OS.

Once you have that figure you can start apache and mysql again.

service httpd start

service mysql start

How much RAM does MySQL use?

There is another calculation that can be used to work this out, but it’s not pretty, so an easier way is to use mysqltuner.

Again using SSH, install mysqltuner.pl using the following commands.

cd /usr/local/bin
wget http://mysqltuner.pl/mysqltuner.pl
chmod +x mysqltuner.pl

Then run the script.

/usr/local/bin/mysqltuner.pl

The output from the script will look something like like this.

——– Performance Metrics ————————————————-
[–] Up for: 2m 31s (554 q [3.669 qps], 65 conn, TX: 18M, RX: 80K)
[–] Reads / Writes: 90% / 10%
[–] Total buffers: 976.0M global + 2.0M per thread (400 max threads)
[OK] Maximum possible memory usage: 1.7G (52% of installed RAM)
[OK] Slow queries: 0% (0/554)
[OK] Highest usage of available connections: 0% (3/400)
[OK] Key buffer size / total MyISAM indexes: 96.0M/4.5M
[!!] Key buffer hit rate: 94.4% (339 cached / 19 reads)
[OK] Query cache efficiency: 25.3% (102 cached / 403 selects)
[OK] Query cache prunes per day: 0
[OK] Sorts requiring temporary tables: 0% (0 temp sorts / 23 sorts)
[OK] Temporary tables created on disk: 15% (24 on disk / 157 total)
[OK] Thread cache hit rate: 95% (3 created / 65 connections)
[OK] Table cache hit rate: 49% (896 open / 1K opened)
[OK] Open file limit used: 10% (696/6K)
[OK] Table locks acquired immediately: 100% (349 immediate / 349 locks)
[OK] InnoDB data size / buffer pool: 44.2M/512.0M

Note – this is not a tutorial for MySQL optimization, that will come in a later post.

MySQL tuner conveniently informs us of the maximum amount of RAM MySQL can use, in this case 1.7GB which is 52% of available RAM. Bare in mind this server has 512mb assigned to the OS.

How to work out the average Apache process size?

Alright so we are almost there.

Now we need to work out the average Apache process size and to do that we can run this command for CentOS

ps -ylC httpd | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'

and this will output nice numbers for you.

Apache Memory Usage (MB): 85.2422
Average Proccess Size (MB): 12.1775

So now we have all the ingredients we need to work out our max Clients.

Let’s crunch the numbers

Just to remind us of the formula.

max clients = (total amount of RAM – ( Max RAM allocation to MySQL + amount of RAM used by OS) )/ average size of Apache process

  • Total RAM = 4GB (4096mb)
  • Max RAM allocated to MySQL = 1.7GB (1740mb)
  • Max OS RAM = 512mb
  • Average size of Apache process = 12.2mb (rounded up to the nearest decimal point)

Let’s enter the numbers in

(4096 – (1740 + 512) )/ 12.2

Simplify it a little

4096 – 2252 / 12.2

Simplify further

1844 / 12.2 = 151

So with this current set up we can see that this server is pretty much spot on the default in terms of the value set for Max Clients.

The above results were for a server with 4GB of ram where 52% of that RAM is dedicated to MySQL.

Note – there is no single config that will give you the right results every time, on every server. There are simply too many factors to consider when determining whether a server is set up optimally or not.

Factors to consider include but are not limited to

  • Amount of traffic
  • Size of processes
  • Amount of RAM
  • Types of queries involved
  • Type of software installed, example, a busy forum will typically require a larger allocation of memory to the database than say a typical WordPress install. (Which relates to amount of traffic)
  • CPU power – in short, a better CPU will crunch more data in the same time frame as a lower end CPU.
  • Overall configuration of Apache
  • Overall configuration of my.cnf (config for mysql)

and so on…

The fact is, every server requires it own unique set up in order to get the maximum performance from it.

If your server is getting bottle necked by either the database or Max Clients then it is time to move on up in the world and buy a bigger server, or increase the resources available to it Primarily RAM.

The ideal server should be able to accept enough simultaneous connections according to your traffic whilst being able to cache and store temporary tables in memory, without having to swap at all.

Finding that balance can at times be a time consuming task as you want to give MySQL as much RAM as possible whilst still being able to meet your demands for Apache processes and other system related tasks.

There are many variables to consider when tuning a server, this is just one of them.

Bonus Content

As an extra, just to give you an idea of how different servers have different set ups. I applied the above to a server with 15GB RAM.

Here are the numbers.

Remember in this post we are just focusing on Max Clients.

  • Total RAM = 15GB (15360mb)
  • Max RAM allocated to MySQL = 1.8GB (1843mb)
  • Max OS RAM = 512mb
  • Average size of Apache process = 15.2mb (rounded up to the nearest decimal point)

(15360 – (1843 + 512 )) / 15.2

Simplified

15360 – 2355 / 15.2

Simplified further

13005 / 15.2 = 855

So this server with the current setup would be able to handle 855 simultaneous requests, but you can see here that we can afford to give more memory to MySQL so once the database is tuned we can run the test again. But tuning MySQL is for another day.

Meet the Author

Matthew Horne

Matthew Horne is web developer who specializes in optimized development. He also builds custom solutions instead of reverting to plugins. Matthew Has a strong understanding of PHP, JavaScript, jQuery.

9 comments… add one
  • crispee May 29, 2013, 2:58 pm

    your formula is missing some brackets 😉
    This: 4096 – (1740 + 512) / 12.2 = 3910.754…
    But like this: (4096 – (1740 + 512)) / 12.2 = 150.491…

  • seekdl Jul 5, 2013, 7:44 pm

    Really informative, i had lots of trouble tweaking apache but this helped me a lot.

  • If i use Varnish, can it differ?

    • Matthew Horne Sep 23, 2013, 5:15 am

      Varnish is given a preset amount of RAM to work with so whatever that allocated RAM is you can deduct that in your calculation.

  • Shubham Tewari Jan 6, 2014, 9:37 pm

    Thanks a lot. It was really helpful for a newbie like me! TFS.

  • takahiro masuda Nov 14, 2014, 1:58 am

    Good info on here. Gotta save all those commands to check memory.

  • dan Feb 11, 2015, 6:50 am

    So if you have a 32bit centos system and have more then 4gb of ram can it still be used by mysql apache e.g. 16gb ram setting.

    Would love to get feedback

    • Matthew Horne Feb 13, 2015, 3:00 pm

      32bit systems are physically limited to about 4GB.

      “Every byte of RAM requires its own address, and the processor limits the length of those addresses. A 32-bit processor uses addresses that are 32 bits long. There are only 4,294,967,296, or 4GB, possible 32-bit addresses.”

      However if your server supports Physical Address Extension (PAE) it can support up to 64GB of RAM.

      Your host is unlikely to issue a server that isn’t able to use its own resources. The Software may not be configure to take full advantage, but it would be a bit fraudulent to offer a server that cant physically use its own resources if set up correctly.

Leave a Comment