How to Fix Slow TTFB on VPS: Server, DB and Cache Checks

How to Fix Slow TTFB on VPS: Server, DB and Cache Checks

Time to first byte, or TTFB, is the time between your browser sending a request and the first byte of the response coming back. When TTFB is slow, every page feels slow, even if the rest of the page loads fast after that. The good news is that you can fix slow ttfb on vps setups in most cases by checking three places in order: the server, the database, and the cache. This guide walks through each one with commands you can run yourself. You need SSH access and a terminal.

What TTFB Is and What Counts as Slow

TTFB covers everything that happens before the server starts answering: the DNS lookup, the TLS handshake, and the time the server spends building the page. For a dynamic page, that last part is the biggest share of server response time, and it is the part you can control.

As a rough target, aim for under 200 ms for a dynamic page served to a visitor near your server. Between 200 and 500 ms is workable but worth improving, and anything consistently over 600 ms deserves a proper look. Static files should answer far faster, often under 50 ms. Two things can confuse the numbers: distance and cold caches, so measure from a location close to your visitors, and measure more than once before you decide something is broken.

Fix Slow TTFB on VPS: Measure It Before You Change Anything

Measure first, change one thing, then measure again. Curl can print timing details for a single request:

curl -o /dev/null -s -w "TTFB: %{time_starttransfer}snTotal: %{time_total}sn" https://example.com

The time_starttransfer value is your TTFB. Test three URLs: your homepage, a static file such as an image, and a dynamic page like wp-admin. If the static file answers in 30 ms but the homepage takes 900 ms, the network is fine and the problem is PHP or the database. If even the static file is slow, the server is loaded or the network path is bad.

Run the same test against 127.0.0.1 from the server itself. If localhost is fast but the public URL is slow, the network path is the problem. If localhost is slow too, the problem is inside the server. Write the numbers down before you change anything, so you can prove the fix worked.

Check the Server: CPU, Memory, and Disk

A server short of CPU, memory, or disk speed will answer slowly no matter how good the code is. Open htop and check the load average against the number of CPU cores: a higher load means processes are waiting for CPU time. Then check memory and disk:

free -h
df -h

If the server is using swap, it is out of RAM, and slow disk is doing memory’s job. A full disk is just as bad, so leave at least 10 percent free.

Next, check whether PHP-FPM is the bottleneck. If pm.max_children is too low, requests queue up waiting for a free worker, and TTFB grows with every visitor. Look for “server reached pm.max_children” in the PHP-FPM error log. Raising the limit helps only if you have the RAM for it, since each child holds its own memory. A runaway cron job or backup during peak traffic is another common cause, so check what else is running with ps aux --sort=-%cpu | head. Most slow servers are simply busy or short of RAM, which is the unglamorous core of vps performance tuning. For a deeper walkthrough of these same checks, see our guide to diagnosing a slow VPS with htop, iotop, and vmstat.

Database Query Optimization: Find the Slow Queries

On a WordPress site or any database-backed app, the database is the most common reason TTFB is slow: one bad query can hold a page for seconds while the server sits idle. Turn on the slow query log in MariaDB or MySQL and record anything slower than one second:

SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;

Let it run during normal traffic, then read it with mysqldumpslow. The worst queries usually share a pattern: a missing index on a column used in WHERE or JOIN, or a query that scans a whole table to return a few rows. Run EXPLAIN on the worst one and look for “ALL” in the type column, which means a full table scan. Adding an index on the filtered column is often the whole fix.

WordPress has its own usual suspects. The wp_options table collects autoloaded rows from old plugins, and every page load reads all of them. Bloated postmeta tables and transients that never expire do the same, so clean out rows from plugins you removed. Also check that the InnoDB buffer pool fits in your RAM: if the pool is too small, MySQL reads from disk for data it should hold in memory. On a small server this setting matters a lot, so follow our guide to tune MariaDB 11.x for a low-memory VPS before you change anything else.

Turn On Caching: Nginx FastCGI Cache and Redis

If every visit runs PHP and hits the database, TTFB will always be limited by how fast PHP and the database are. A cache breaks that link. Nginx FastCGI cache stores the finished HTML page on disk and serves it on the next request without starting PHP at all. That is often the fastest single way to fix slow ttfb on vps sites running WordPress. Our guide to configure Nginx FastCGI cache for WordPress without a plugin walks through the setup.

After setting it up, check that it works. Ask for the page headers:

curl -I https://example.com

You want to see X-FastCGI-Cache: HIT on the second request. If it says MISS every time, the page is being skipped, usually because of a cookie, a query string, or a cache rule that excludes the URL. Fix the rule, do not just add more RAM. Never cache pages for logged-in users or the admin area, and make sure the cache clears when you publish or update a post, or visitors will keep seeing the old page.

Page caching handles anonymous visitors. For logged-in traffic and the admin area, add an object cache. Redis stores the results of database queries in memory so repeated queries skip the database entirely. Install it with our guide to install Redis 7.x on Ubuntu 24.04 and use it as a cache, then connect WordPress to it with an object cache plugin. A CDN does not fix TTFB for a first-time visitor when the origin server itself is slow, so fix the server first.

Tune PHP-FPM and OPcache

Two PHP settings quietly decide how fast dynamic pages are. The first is OPcache, which stores compiled PHP code in memory so PHP does not recompile every file on every request. Check that it is on:

php -i | grep -i opcache

Look for opcache.enable => On. In production, set opcache.validate_timestamps=0 so PHP stops checking file timestamps on every request, and restart PHP-FPM after each deploy so it picks up your code changes. Give it 128 MB to start on a small server, and watch the hit rate. Under 99 percent usually means the cache is too small or something keeps flushing it.

The second setting is the PHP-FPM process manager. The ondemand mode starts workers only when requests arrive, which saves RAM on a quiet server but adds startup time to each request. The dynamic mode keeps a few workers warm, which costs a little RAM and answers faster under steady traffic. Set pm.max_children from real RAM numbers: divide the memory you can spare by the memory one PHP child uses, which you can read from ps or htop. Too few children queue requests, too many push the server into swap.

Verify the Fix and Keep TTFB Low

Fix one thing at a time and re-run the curl test after each change. Change the database, the cache, and PHP-FPM all at once and you will never know which one helped, or what to undo if something breaks. Keep the numbers from your first measurement and compare against them after each step.

Once TTFB is where you want it, keep it there: a cron job that runs curl every few minutes and logs the timing will show you when the number creeps back up, long before visitors complain. Re-check after plugin updates, traffic spikes, and content imports, since all three can quietly add slow queries or fill the disk. If you work through the checks in this guide and still see slow numbers, go back to the measurement step and test the static file, the homepage, and localhost again. The split between those three numbers will point at the part you missed, and that is how you keep TTFB low for good instead of fixing it for a week.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.