Website Performance
Server Response Time and TTFB: How to Measure and Improve It
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيTime to First Byte (TTFB) is the time between the browser starting a request and receiving the first byte of the response. Everything the visitor sees waits for it: no HTML means no CSS, no images and no text on screen. A high TTFB is one of the clearest signs that a problem lies on the server or network side rather than in the front end. The good news is that it is easy to measure and usually has a short list of causes.
What TTFB includes
TTFB is not a single delay. It adds up several stages:
- Redirects, if the first URL redirects elsewhere.
- DNS lookup to find the server's IP address.
- TCP connection, one round trip.
- TLS handshake for HTTPS, one or two round trips.
- Server processing: the web server, application, database queries and any external API calls.
- The first byte travelling back to the browser.
Stages 2 to 4 and 6 depend mainly on distance and network quality. Stage 5 depends on your server and code. Knowing which part is large tells you where to work.
TTFB and SEO
TTFB is not one of the Core Web Vitals itself, but it directly delays Largest Contentful Paint (LCP), which is one. Google's web.dev guidance suggests aiming for a TTFB of about 0.8 seconds or less for most sites, measured on real users. Search engines also crawl sites more comfortably when the server responds quickly and reliably. Treat TTFB as the foundation that the rest of your performance work stands on, not as a ranking trick.
Measuring TTFB with curl
curl can break a request into its stages:
curl -o /dev/null -s -w \
'dns: %{time_namelookup}\nconnect: %{time_connect}\ntls: %{time_appconnect}\nttfb: %{time_starttransfer}\ntotal: %{time_total}\n' \
https://example.com/The values are cumulative seconds from the start. If connect is already high, the server is far away or the network path is poor. If the gap between tls and ttfb is large, the server is slow to generate the page. Run it several times, from more than one location, and test both the home page and heavy pages such as search or product listings.
Browser developer tools show the same breakdown in the Network tab under Timing ("Waiting for server response").
Finding slow requests on the server
Add timing to your Nginx access log so you can see how long the application took for each request:
log_format timed '$remote_addr "$request" $status '
'rt=$request_time urt=$upstream_response_time';
access_log /var/log/nginx/access.log timed;$upstream_response_time is the time spent waiting for PHP-FPM, Node.js or another backend. Find the slowest requests:
awk -F'urt=' '{print $2, $0}' /var/log/nginx/access.log | sort -rn | head -20For PHP, the FPM slow log shows a stack trace for requests above a threshold. In your pool file, for example /etc/php/8.3/fpm/pool.d/www.conf (the version depends on your system):
request_slowlog_timeout = 2s
slowlog = /var/log/php-fpm-slow.logFor databases, enable the slow query log in MySQL or MariaDB (slow_query_log, long_query_time) or log_min_duration_statement in PostgreSQL.
Common causes and fixes
| Cause | Fix |
|---|---|
| No page caching | cache full pages for anonymous visitors (Nginx fastcgi_cache, a CMS cache plugin, or a CDN) |
| PHP compiling scripts every request | enable OPcache |
| Repeated database queries | object cache with Redis, add indexes, fix N+1 queries |
| Slow external API calls during the request | move them to background jobs or cache the results |
| Too few PHP-FPM or app workers | tune pm.max_children to fit available RAM |
| Server swapping or CPU saturated | check free -h, vmstat 1, top, then optimize or resize |
| Server far from users | choose a closer region, use a CDN for cacheable content |
| Redirect chains | link directly to the final URL, including https and www choice |
| Slow DNS | use a reliable DNS provider with reasonable TTLs |
Caching usually brings the largest single improvement because it skips stage 5 almost entirely. Our guides on server-side caching and edge caching explain the options.
A quick troubleshooting routine
- Measure with curl from a location near your users.
- Compare
connectagainst the gap betweentlsandttfb. - If the network part is high, test from other locations and consider a different region or a CDN.
- If processing is high, check
$upstream_response_time, the slow logs and server load. - Fix the slowest endpoint first, then measure again.
- Keep monitoring TTFB over time so regressions after updates are caught early.
When the server is the limit
Sometimes the code and caching are fine, but the server simply lacks CPU or RAM for the traffic, and the load average stays high or the system swaps. In that case, moving to a larger plan or a location closer to your audience is the right fix. You can compare options on the pricing page.
FAQ
What is a good TTFB?
Google's web.dev guidance suggests about 0.8 seconds or less as a reasonable target for most sites, measured on real users. Cached pages served close to visitors can respond far faster. Pages that must be generated per user, such as dashboards, will usually be slower, so judge each type of page separately.
Does a CDN reduce TTFB?
For content the CDN can cache, yes, because the response comes from a nearby edge server without reaching your origin. For uncached pages it can still save time on the connection and TLS setup, but the server processing time remains. If dynamic pages are slow, the fix is on the server.
Why is my TTFB high only sometimes?
Intermittent spikes usually point to cache expiry, cron jobs or backups running at the same time, traffic peaks exhausting workers, slow external APIs, or a database lock. Correlate the slow times in your logs with server load and scheduled tasks to find the pattern.