How to Reduce Server Load in Linux

Last updated on September 4th, 2024 at 04:22 am

Server load is one of the most important metrics to run a fast and smooth website/app. If your server is overloaded with too many requests, then it will eventually crash and stop working. Along with it, many of your applications and services will also stop working. So it is important to regularly monitor server load, check which process is taking high load and take steps to troubleshoot high server load. This will improve website performance, improve user experience and reduce server costs. Here are the key tips to reduce server load for your website.

What is Server Load

Whenever you hear the term ‘server load’ it means a combination of CPU usage, memory usage and disk usage. It your CPU is using a large fraction of any of these resources, there is a good chance that it will soon stop working. Please note, every CPU is capable of handling occasional burst of CPU usage, or disk usage or memory consumed. But if it is sustained for a long time, then it will cause problems.

Therefore, it is more useful to track ‘average server load’ that is measured over a specific time interval, instead of measuring server load at any given point of time. It is also important to remember, that all applications, over time, expand their footprint meaning gradually consume more CPU, disk and memory. So you will need to regularly keep an eye on your server load and take active measures to reduce it.

How to Calculate Server Load

Server load is a combination of CPU, memory and disk usage. You can check server load in Linux using ‘w’ or ‘uptime’ commands. Linux will output average load value.

$ uptime 
10:40:40 up 18 days, 1:07, 1 user, load average: 0.15, 0.10, 0.10

You can also use third-party tools like Solarwinds and Nagios to check server load in Linux. They automatically calculate server load on a regular basis and plot results on intuitive graphs and dashboards that you can share with your team.

How to Reduce Server Load in Linux

Here are some important tips to reduce server load.

1. Optimize Website Images

Images are responsible for up to 50% of a website’s bandwidth. Compressed images reduce server load by decreasing its CPU usage. There are many image compression tools that can compress JPG, PNG, GIF images by up to 80%.

Use CSS effects instead of images, wherever possible. Similarly, use CSS sprites instead of separate icon images. CSS sprite is a collection of images put in a single image. So you can have a single CSS sprite that contains all your image icons. This will reduce your server load considerably, since it has to serve only one file, instead of separate icons.

Also, consider using new-age image formats such as webp which are up to 26% smaller than PNG files.

Avoid using high-resolution images on your website. A maximum image resolution of 1200×800 px is enough to look great on all devices.

Also disable image hotlinking on your website, so that other websites do not render images by linking to yours.

2. Compress HTML Resources

As we develop our site, we keep adding more and more static files (CSS, JS and HTML) files to it. Each of these files takes up some disk space and CPU processing. Over time, they take a huge amount of resources on your server. Therefore, it is advisable to serve the compressed versions of these files, instead of their original versions.

There are many third-party tools like Minify and HTML Minifier that allow you to compress and minify CSS, JS and HTML files for your websites. These tools automatically remove any comments & whitespaces, and replace long variable names with shorter ones to reduce the file size.

3. Compress response data

When users request web pages on your site, by default, all web servers send the response data as it is, without any optimization. However, most popular servers such as Apache and NGINX allow you to compress data before sending it to the client browser, which is automatically uncompressed and rendered by the browser for its user. This is very useful especially, if you are sending large amount of data to populate tables and graphs on your site.

In fact, they even allow you to specify compression algorithm (gzip, bzip2, etc) to be used for data compression. Make sure that you have enabled data compression on your web server. Here’s how to enable GZIP compression in Apache and NGINX.

4. Host Images on Third-party websites

Typically Ecommerce websites, photo sharing apps and social media platforms tend to have a lot images on their servers. They consume a lot of disk space. Serving these images can take a toll on the processing power of your server. Therefore, you may want to host these images on a third-party service such as Flikr or ImageShack, or even try a CDN provider.

In this case, whenever users request a web page on your site, all requests for images on it, will be sent to the third-party hosting these images and they will be served from that location. This will eliminate the server load due to images on your site.

5. Review Most Frequent Code

Every website and application has a set of features that are most frequently called upon by most users. For example, the product catalog, product details page and checkout pages are the most frequently accessed pages on an Ecommerce site. They need to be highly optimized otherwise they can bring down your server, by consuming too many unnecessary resources. Use web analytics tools to identify such pages.

Review the back end code that goes behind the processing of requests to these pages. Look for loops, variables and files that can be optimized or removed, as these tend to consume a lot of memory as well as increase CPU usage.

6. Enable Web browser Caching

By default, every web browser caches frequent and recent images and static files. It is called browser caching. However, it is done using browser’s own algorithms. But you can also control what content is cached on web browser, from your web server. You can do this by setting specific response headers in your server. You can configure one or more specific URLs to be cached on web browser. Here are the steps to enable browser caching using NGINX server.

7. Restart Server

No matter how optimized your code and data, all web servers increase their footprint over time. They will gradually consume more CPU processing, memory and disk space. This is because of creation of temporary files and data, memory leaks and other server inefficiencies. So a simple way to reduce server load is restart it periodically, say, once a month or fortnight. This will completely reset its CPU usage, disk space and memory requirements. System administrators run a cron job or scheduled task to regularly restart their web servers.

8. Free System Resources

Sometimes your server becomes slow because your system resources such as disk space, memory and CPU are completely used up. It may even be that some processes are running in background, even after they completed execution. In such cases, it is better to kill unnecessary or unresponsive processes. This will not only free up memory but also reduce CPU usage. You can also free up disk space. It is also important to regularly monitor processes running on your system.

Conclusion

The key to reducing server load is to systematically compress every aspect of your website, and streamline most frequently used code on it. Secondly, it is important to regularly monitor your site’s server load to identify and fix any red flags early on. Implementing the above tips should ease your server load quite a lot. They can be used on all websites irrespective of its platform. However, if none of the above tips are working for you, then you really need to upgrade your CPU processor or memory or disk, depending on which one is getting saturated.