HTTP caching giảm bandwidth và latency bằng cách lưu responses tại browser hoặc CDN.
- Cache-Control directives: max-age=3600 (cache 1 giờ trước khi stale); no-cache (phải revalidate với server trước khi dùng — không phải 'không cache'); no-store (không cache bao giờ, sensitive data); public (CDN có thể cache); private (chỉ browser cache, không CDN); immutable (content không bao giờ thay đổi, skip revalidation kể cả khi max-age expired — dùng với content-hashed assets).
- Static assets strategy: Cache-Control: public, max-age=31536000, immutable cho files có content hash trong tên (bundle.abc123.js) — cache 1 năm, không bao giờ revalidate.
- HTML: Cache-Control: no-cache — revalidate mỗi lần nhưng serve từ cache nếu ETag match.
- Conditional requests: ETag (hash của content) và Last-Modified (timestamp); client gửi If-None-Match/If-Modified-Since trong request; nếu không thay đổi, server trả 304 Not Modified (no body) — tiết kiệm bandwidth.
- Stale-While-Revalidate: Cache-Control: max-age=60, stale-while-revalidate=300 — serve stale content trong 5 phút trong khi revalidate background; zero latency impact.
- CDN caching: Cloudflare, Vercel Edge Network cache responses — đặt s-maxage khác max-age để CDN cache lâu hơn browser.
- Cache busting: thay đổi URL khi content thay đổi (content hash, query string ?v=2).