TTFB Optimization: Diagnosing Slow Server Responses from DNS to Backend Processing
Isolate which phase of the response is slow before spending money. A slow TTFB is often a redirect chain or a missing index, not your hosting.
In short
Time to First Byte is how long the visitor waits before your server starts answering. Until it does, the browser has nothing at all — no HTML, no knowledge of your images, nothing to render.
That makes TTFB a ceiling. If it is 1.5 seconds, your Largest Contentful Paint cannot be under 1.5 seconds no matter what you do to your images.
And here is the finding that saves the most money: a slow TTFB usually is not your hosting. In practice it is most often a missing cache, a redirect chain, or a database query without an index. All three are fixable without spending anything.
So the job is to isolate which phase is slow before changing anything. TTFB is six things in sequence, and they have nothing in common.
The six phases
| # | Phase | What is happening |
|---|---|---|
| 1 | DNS lookup | Turning your domain into an IP address |
| 2 | TCP connection | Opening a connection to the server |
| 3 | TLS negotiation | Agreeing encryption |
| 4 | Redirects | Any hops before the real page. Each repeats phases 1–3. |
| 5 | Server processing | Your application building the page |
| 6 | First byte sent | The response starts arriving |
Phases 1–3 are mostly a function of distance and configuration. Phase 4 is self-inflicted and cheap to fix. Phase 5 is your application, and is where most slow TTFB actually lives.
Measuring each phase
The one command to run first
curl -w "dns: %{time_namelookup}s\nconnect: %{time_connect}s\ntls: %{time_appconnect}s\nredirect: %{time_redirect}s\nttfb: %{time_starttransfer}s\ntotal: %{time_total}s\n" \
-o /dev/null -sL https://example.com/
These are cumulative, so the time spent in each phase is the difference between consecutive numbers.
What you should see, roughly: DNS under 100 ms, connect within about 100 ms of DNS, TLS within another 100 ms, redirect at zero, and TTFB close to TLS plus your server's thinking time.
How to read it:
redirectabove zero → you have hops before the real page. Fix that first; it is the cheapest win available.- A large gap between
tlsandttfb→ server processing. This is phase 5, and it is where you should look next. dnsslow → your DNS provider, or no DNS caching.connectandtlsslow → distance to the origin, or TLS configuration.
Run it several times. The first run includes a cold DNS lookup and a cold connection.
In DevTools
Network panel → click the document request → Timing tab. You get the same breakdown visually, including Waiting for server response, which is phase 5.
Instrument the server itself
Once you know phase 5 is the problem, Server-Timing tells you where inside your application the time goes, and it appears in DevTools alongside the network phases.
Server-Timing: db;dur=420, render;dur=85, cache;desc="miss"
This single header turns "the server is slow" into "the database took 420 ms and the cache missed", which is a fixable statement. Adding it is usually an afternoon's work and pays for itself immediately.
Fixing each phase
Redirects (fix these first)
Every redirect is a full round trip — DNS, connection, TLS, request, response — before the real page starts. A chain of http:// → https:// → www. → final page costs three.
curl -sIL https://example.com | grep -iE "^HTTP|^location"
What you should see: one 200, or at most one redirect before it.
Fix by redirecting straight to the final address in a single hop, and by updating internal links, sitemaps and canonical tags so visitors and crawlers arrive at the final URL directly.
Server processing: caching
The most common cause by a wide margin. Without caching, your server runs your application code and queries your database for every visitor, to produce a page that is identical each time.
- Page caching stores the rendered output. The biggest single win on most sites.
- Object caching stores the results of expensive queries, which helps pages that cannot be fully cached.
- A cache that is not working is as bad as none. Confirm the hit rate rather than assuming the plugin is doing its job.
Exclude anything personalised — cart, account, checkout. See browser caching for how the three caching layers differ.
Server processing: database
Usually appears as the catalogue or content set grows.
- Missing indexes on whatever you filter and sort by.
- Too many queries per page — dozens of small queries where one join would do.
- Queries fetching far more than needed.
- Table or row locking under write load.
Enable slow query logging and look at the actual queries. On WordPress a query monitoring plugin shows the count and duration per page. A telling sign: a product listing page is slow while a static page is fast.
Server processing: external calls
An API call inside the request — a stock lookup, a currency rate, a personalisation service — puts someone else's latency inside your TTFB. And if their service is slow today, your site is slow today.
Move these out of the request path: fetch on a schedule and cache the result, or load the data in the browser after the page has rendered.
Distance to the origin
Data travels at a finite speed. An origin in London serving Sydney costs a few hundred milliseconds per round trip before any work happens.
A CDN solves this for static files, and for cacheable HTML. It does not solve it for a slow, uncacheable, personalised page — see CDN performance.
DNS, TCP and TLS
Usually small, occasionally not.
- A slow or distant DNS provider is worth changing.
- HTTP/2 and HTTP/3 reduce connection overhead; HTTP/3 in particular reduces handshake cost.
- TLS session resumption avoids repeating the full negotiation.
- Keep certificate chains short.
These are refinements. Do not start here unless your measurements point here.
Platform differences
Self-hosted (WordPress, WooCommerce, custom). Everything above is available to you. Start with caching, then redirects, then queries, then hosting.
Shopify and other hosted platforms. You cannot change the server, its resources or its location. A slow server response is almost always expensive template rendering — your theme asking the platform to do too much work. That is yours to fix. See Shopify theme performance.
Hosted site builders. TTFB is entirely the platform's. There is nothing here you can act on; spend your effort on what you load in the browser.
Common mistakes
Assuming slow TTFB means bad hosting, and upgrading before diagnosing.
Measuring once. The first request includes cold DNS and a cold connection.
Measuring from your own machine only, when your audience is elsewhere.
Testing a warm cache and reporting it as normal. Test both, and say which.
Optimising images to fix a server problem.
Installing a caching plugin without verifying it caches. Confirm the hit rate.
Leaving an external API call in the request path.
Forgetting redirects, which are often the largest and cheapest single component.
Verification
- Re-run the
curltiming command, several times, and compare the phase you targeted. This is the direct proof. - Check cold and warm separately. A cache fix shows up as a large gap between them, which is expected and good.
- Confirm cache hit rate if caching was your fix, rather than inferring it from the timing.
- Check
Server-Timingif you instrumented it — the specific sub-phase should have shrunk. - Test from more than one location, especially if distance was your diagnosis.
- Check LCP improved too. TTFB is one of its four subparts, so a real TTFB win should show up there.
- Then watch field data for four weeks.
When it still does not improve
- TTFB is unchanged after adding caching → it probably is not caching. Verify hits; a misconfigured cache serves nothing.
- Fast warm, slow cold → that is a cache working. Decide whether the cold case matters enough to pre-warm it.
- Server processing is still large and queries look fine → look for an external API call in the request, or template rendering cost.
- Fast for you, slow in the field → distance or device variety. Test from your audience's regions.
- You improved TTFB and LCP did not move → TTFB was not your LCP bottleneck. The image may be discovered late or the render blocked.
- Nothing is slow in your measurements but visitors complain → measure under load. A server that is fine for one request can queue badly under concurrency; see hosting performance.
Keeping it fast
TTFB regresses through ordinary work: a plugin that adds queries, a feature that calls an API during rendering, a catalogue that outgrows its indexes, a cache that silently stops working after a migration.
Three habits: monitor TTFB continuously rather than checking occasionally; keep Server-Timing in production so a regression tells you where it is; and check the cache hit rate as part of your release routine, because a cache that has quietly stopped working is one of the most expensive silent failures there is.
Frequently asked questions
What is a good TTFB?
Under about 0.8 seconds is a reasonable target and under 0.5 is good. More important than the absolute number: TTFB is a ceiling on your Largest Contentful Paint, so if it is 1.5 seconds your LCP cannot be faster than that whatever you do to your images.
Does a slow TTFB mean I need better hosting?
Usually not. In practice it is most often a missing page cache, a redirect chain, or a database query without an index — all free to fix. A site slow because of its code will be exactly as slow on a bigger server.
How do I measure TTFB properly?
One curl command gives you every phase, using time_namelookup, time_connect, time_appconnect, time_redirect and time_starttransfer. The values are cumulative, so the time spent in each phase is the difference between consecutive numbers. Run it several times, because the first includes a cold DNS lookup.
How much do redirects cost?
Each one is a full round trip — DNS, connection, TLS — before the real page starts. A chain of http to https to www to the final page costs three, and on a mobile connection that is easily half a second before anything useful happens.
What is the Server-Timing header?
A response header your application can send to report where time went inside the request, such as database and render durations. It appears in DevTools alongside the network phases and turns “the server is slow” into a specific, fixable statement.
Can I improve TTFB on Shopify?
Yes, but not by changing hosting — you cannot. A slow server response on Shopify is almost always expensive Liquid rendering, which is yours to fix by reducing the work your templates ask for.
References
- Time to First Byte (TTFB) — web.dev
- Optimize TTFB — web.dev
- Server-Timing — MDN