What Is TTFB?
Time to First Byte measures the duration from a client request to receiving the first byte of the response. It includes DNS lookup, TCP connection, TLS handshake, and server processing time.
Measure TTFB
# Using curl
curl -o /dev/null -s -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTLS: %{time_appconnect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" https://example.comGood TTFB Targets
| Rating | TTFB |
|---|---|
| Excellent | < 200ms |
| Good | 200-500ms |
| Needs improvement | 500ms-1s |
| Poor | > 1s |
Optimization Strategies
1. Server-Side Caching
# Enable OPcache for PHP
opcache.enable=1
opcache.validate_timestamps=0
# Enable FastCGI cache in Nginx
# Or use Varnish as a full-page cache2. Database Optimization
- Add indexes on frequently queried columns
- Use query caching (Redis/Memcached)
- Optimize slow queries (check slow query log)
3. TLS Optimization
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets on;
ssl_stapling on;
ssl_stapling_verify on;4. Keep-Alive Connections
keepalive_timeout 30;
keepalive_requests 1000;5. Use a CDN
A CDN caches responses at edge locations worldwide, reducing TTFB for geographically distant visitors.
Monitoring TTFB
- Google PageSpeed Insights
- WebPageTest.org
- Uptime monitoring tools (Uptime Kuma)