How to Improve Time to First Byte (TTFB)
What is this?
Time to First Byte (TTFB) measures how long it takes from the browser's request to receiving the first byte of the response. It includes DNS lookup, TCP connection, TLS handshake, and server processing time. A good TTFB is under 200ms; under 800ms is acceptable.
Why it matters
- Performance: TTFB is the starting point for all other metrics. A slow TTFB delays everything.
- User experience: Users perceive sites with TTFB over 600ms as sluggish
- SEO: Google uses TTFB as part of its Core Web Vitals assessment
How to fix it
Enable server-side caching
Nginx (FastCGI cache for PHP/WordPress)
fastcgi_cache_path /tmp/nginx-cache levels=1:2 keys_zone=SITE:10m inactive=60m;
server {
location ~ \.php$ {
fastcgi_cache SITE;
fastcgi_cache_valid 200 60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
}
}
Redis caching (Node.js)
const redis = require('redis');
const client = redis.createClient();
app.get('/api/data', async (req, res) => {
const cached = await client.get(req.url);
if (cached) return res.json(JSON.parse(cached));
const data = await fetchFromDB();
await client.setEx(req.url, 300, JSON.stringify(data));
res.json(data);
});
Use a CDN
A CDN caches your content at edge locations close to users, reducing network latency:
- Cloudflare: Enable caching rules under Caching > Cache Rules
- AWS CloudFront: Create a distribution with your origin server
- Fastly / Bunny CDN: Configure origin shield and cache TTLs
Optimize database queries
Slow database queries are the most common cause of high TTFB:
-- Add indexes to frequently queried columns
CREATE INDEX idx_users_email ON users(email);
-- Use EXPLAIN to identify slow queries
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 123;
Upgrade hosting
Shared hosting is a common TTFB bottleneck. Consider upgrading to a VPS or managed hosting provider with better I/O performance.
Common mistakes
- Measuring TTFB from a distant location and blaming the server. TTFB includes network latency; test from a location near your server first.
- Caching dynamic content that should not be cached (e.g. user-specific pages). Use
Cache-Control: privatefor authenticated content. - Over-optimizing application code while ignoring that the database is the bottleneck.
Test your fix
After making improvements, audit your site on BeaverCheck to see your updated TTFB in the Performance tab.