You Own a Server? Is it Ready for Black Friday? (Part 2)
In the previous article we told you how to prepare your online store for Black Friday by finding a suitable hosting service. We also gave you a few optimization tips.
These services provide you with full root access that enables you to analyze them in a more detailed way. On the other hand, CPU time is the parameter that helps you analyze your resources. For that reason the following lines are going to be more technical in nature.
There is an index called Load Average in Linux that shows total value of the CPU load. The current Load Average is displayed with the "top" command.
The lower the numbers, the better (watch out the second and third number). Generally a threshold value means that the Load Average should not exceed the number of CPU cores on the server. This rule might give you an overall idea whether your server is overloaded. The threshold value means that the Load Average should not exceed the number of cores multiplied by 2. Higher numbers might represent overloaded websites.
If CPU load increases linearly depending on the number of users, we can suppose that if the number of our website visits doubles, the load will also double. In such cases the Load Average value goes very high. So you can either increase the number of CPU cores or optimize in other ways. Do not forget to increase proportionally the RAM along with the CPU cores!
Here is a simple example – you own a server with 4 CPU cores. Usually your website reaches about 5000 page views per day. So the Load Average value is 0.5. If we accept that on Black Friday you will reach a total of 30000 page views, we can calculate that:
0.5 * (30000 / 5000) = 3
Your Load Average value equals 3. You have a server with 4 CPU cores available so we accept that it could handle the load. However, we might expect most users to visit your website for a short period of time – for example from 10:00 to 15:00. In such case it is a great idea to ensure 100 % server interruptibility. So we now calculate:
3 * (1 + 100%) = 6
Now the CPU load is higher than the number of cores you have available and you need to increase their number (in your case with at least 50% – from 4 to 6). You also need to increase the RAM. If your server used to have 4GB RAM, now make it at least 6GB.
In many cases, however, increasing the number of visitors to your website leads to nonlinear increase of CPU load so it is possible that the server load increases more than expected. This is a specific situation depending on the web application used and its settings.
Our recommendations for calculating the necessary resource are just basic and do not cover all possible case scenarios. So it is better to consult an expert for a more detailed analysis.
For further optimization you might take the following steps:
- Check whether most of the load is coming from – the scripts or the database. Thus you will be able to focus on the more critical aspect. My tips for optimizing PHP and slow queries from our previous article are still 100% valid!
- Make sure that the server load is not caused by input/output disk operations. You can use the “iostat” command and check the value of “%iowait” in the result. If the value is above 10% it is a good idea to consider optimizing. Sometimes the best solution is adding more RAM to the server. It does miracles with iowait. But if the load is caused by many slow queries you should deal with them.
- If your system allows this, install web caching software that caches whole pages and works 'in front" of your web server. This way pages will be directly loaded from the cache without sending requests to scripts and the database. Varnish and Nginx (works as reverse proxy) are among the most popular caching solutions. са Varnish или Nginx (работещ като reverse proxy). Please note that installing such software might be very time consuming and the changes need to be implemented into the code by a developer.
- Try to use object caching in the system itself such as Memcache or Redis. You will most probably need a developer for the implementation.
- Create a separate static landing page, to redirect part of the users on Black Friday. You can set up a cron job to generate a landing page from your website's home page.
- Check your web server settings, the database and PHP. Very often when performing more complex optimizations we miss upgrading different settings on the server or database. This leads to inefficient increase of server resources. In case you expect more visitors, start your optimization with MaxClients in Apache, or max-connections and max_user_connections in MySQL.
To test your server behavior when it needs to handle many users, you can use Apache Bench. This software enables you to simulate numerous simultaneous visits on your website and to track whether the server can deal with them. You can find further information for Apache Bench on the official website: http://httpd.apache.org/docs/2.4/programs/ab.html
If you need further technical assistance, our high qualified sysadmins are always available to help you. Do not forget that most often optimization requires to be performed by experienced developers.