HTTP API Caching: Mastering Cache-Control, ETag, and Conditional Requests
Slash API server costs and reduce latency with HTTP caching. Learn Cache-Control max-age, s-maxage, stale-while-revalidate, and ETag 304 Not Modified.
The fastest API call is the one served directly from browser memory or a nearby CDN edge node. Proper HTTP caching headers control both freshness lifetime (Cache-Control) and conditional revalidation (ETag / If-None-Match).
1. Essential Cache-Control Directives
# Public catalog endpoint cached for 60s in browser, 10m on CDN edge: Cache-Control: public, max-age=60, s-maxage=600, stale-while-revalidate=30 # Private user profile endpoint (never cached on shared CDN proxies): Cache-Control: private, no-cache, must-revalidate ETag: "v2-98a1f2c"
2. How ETag Conditional Revalidation Saves Bandwidth
When a cached response expires, the client sends the stored token in an If-None-Match: "v2-98a1f2c" request header. If the underlying database record has not changed, the origin server immediately returns 304 Not Modified with an empty body, saving both serialization CPU time and egress bandwidth.
Frequently Asked Questions (FAQ)
What is the difference between no-cache and no-store?
no-cache allows the browser to store the response locally but forces it to revalidate the ETag with the origin server before reusing it. no-store forbids writing the response to disk or memory caches altogether.