Slow server: common causes and fixes
"The site is slow" is one of the most frequent complaints and at the same time one of the vaguest. Slowness can come from the server, the database, the application, the network or even the browser. If the cause isn't found, a bigger server is usually bought. Sometimes that helps, often it doesn't.
The first step is to understand where the time goes. Only then is it worth changing anything.
First: is it the server or the page that's slow?
Open the browser's developer tools (F12) and look at the network tab. The first request, the one that fetches the HTML, shows how long the server took to prepare the response (TTFB, time to first byte).
- If the first request takes more than a second, the problem is in the server, the database or the application. That is what this post is about.
- If the first request is fast but the page still loads slowly, the problem is usually images, scripts or third-party services. That is covered in the post website speed: how to measure and improve it.
Common causes on the server side
1. Slow database queries
This is the most common cause. A query that ran fast with a thousand rows crawls with a million. Usually an index is missing, or the query reads more data than it needs.
How to find it: turn on the slow query log in the database (log_min_duration_statement in PostgreSQL, slow_query_log in MySQL and MariaDB). After a few hours you can see which queries take the time. Often one or two are to blame.
2. Out of memory
When the server doesn't have enough memory, the system starts using the disk instead of memory (swap). The disk is many times slower than memory, so everything becomes slow at once.
How to find it: the free -h command shows how much swap is being used. If it is used constantly, you need either more memory or to reduce how much the application and the database load into memory at the same time.
3. A misconfigured web server or PHP
The default settings are meant for an average situation. If PHP-FPM allows too few concurrent processes, requests wait in a queue. Too many, and memory runs out. The same applies to Nginx and Apache workers.
How to find it: the web server's error log usually says directly when the process limit was reached.
4. No caching
If every page view makes the application calculate the same thing again, the server is loaded for nothing. Caching can work on several levels: page cache, database query cache (for example Redis) and PHP code cache (OPcache).
5. Bots and attacks
Often the reason for slowness is that a large share of the traffic isn't real visitors. Aggressive crawlers, password guessing or a denial-of-service attack can take up most of the server's resources.
How to find it: look at the web server's access log. If an IP address or user agent makes thousands of requests a minute, you've found the cause.
6. The disk is slow or full
On old servers or cheap virtual servers the disk can be the bottleneck. A nearly full disk makes some file systems noticeably slower.
7. Outdated software
Old PHP, an old database version or an old operating system. Newer versions are often noticeably faster, especially with PHP. If the server runs an old Ubuntu, there is a post on upgrading it: upgrading an Ubuntu server to a new LTS.
Why a bigger server isn't always the answer
If the cause is a missing index, a server twice as fast makes the slow query slightly less slow. A month later, when there is more data, the problem is back and the bill is higher. If the cause is bots, they will eat a bigger server's resources too.
A bigger server is the right answer when the configuration is in order and the load has genuinely grown. The only way to know is to measure.
How I investigate slowness
- I measure where the time goes: web server, application or database.
- I go through the logs: slow queries, errors, suspicious traffic.
- I check the resources: memory, disk, CPU, and how they change over time.
- I fix the biggest cause and measure again. Often one change is enough.
- I set up monitoring, so the next slowdown is visible before customers complain. What to watch is described in the post on Linux server monitoring.
I manage Ubuntu, Debian and RHEL-family servers, Nginx and Apache, PostgreSQL, MySQL and MariaDB, and Docker. If your server is slow and the cause hasn't been found, see Linux server management or describe the problem.
