TL;DR: TTFB measures the time between a request and the first byte of response. High TTFB hurts Core Web Vitals and user perception. This guide covers server-side optimization, edge computing benefits, database query tuning, and caching to reduce TTFB. For an overview of CDN strategies that directly improve TTFB, see our CDN Selection and Configuration Guide. Optimization patterns at tanstackship.com.
Introduction
TTFB is the first performance metric users experience. A slow TTFB makes everything else feel slow, even if your client-side rendering is optimized. Google recommends TTFB under 800ms, with a target of under 200ms. Getting started with Cloudflare Workers Introduction is a great way to begin reducing latency at the edge.
TTFB Optimization Techniques
| Technique | Impact | Effort | Best For |
|---|---|---|---|
| Edge computing (Workers) | High | Medium | Dynamic content |
| CDN caching | Very high | Low | Static + semi-dynamic |
| Database query optimization | High | Medium | API responses |
| Connection pooling | Medium | Low | Database-heavy routes |
| Streamed SSR | Medium | Medium | Server-rendered pages |
| Brotli compression | Low | Low | All text responses |
Edge Computing with Workers
export default {
async fetch(request: Request, env: Env): Promise<Response> {
// Workers handle requests at the edge, near the user
const url = new URL(request.url)
// Check cache first
const cached = await env.KV.get(url.pathname, 'json')
if (cached) {
return new Response(JSON.stringify(cached), {
headers: { 'CF-Cache-Status': 'HIT' }
})
}
// Fetch from origin
const response = await fetch(`${ORIGIN_URL}${url.pathname}`)
return response
},
}
Conclusion
Reducing TTFB requires attention at every layer: choose edge computing over centralized servers, cache aggressively, optimize database queries, and use compression. Each millisecond saved improves user experience and SEO.
Deepen your knowledge with our Edge Caching Strategies Guide for advanced cache configuration, and the Core Web Vitals Optimization Guide for a broader performance framework that includes TTFB as a key metric.