HTTP Caching: Complete Guide

HTTP caching strategies: Cache-Control directives, ETag/Last-Modified validation, CDN caching, stale-while-revalidate, and browser cache behavior.

  • JSON8,515 B
  • YAML6,067 B · −29%
  • TOON4,794 B · −44%

A guide to HTTP caching directives and validation: Cache-Control values such as max-age, no-cache, no-store, private, public and immutable, and the validators ETag and Last-Modified, each with an example and notes on when it applies.

Use it when

  • You are setting caching headers for static assets or API responses.
  • You need to explain the difference between no-cache and no-store.

Watch out for

  • no-cache does not mean 'do not cache'; it means revalidate before reuse. no-store is the directive that forbids storing.
  • Shared caches such as CDNs and browser caches have different rules (s-maxage, private).

The data

Directives

DirectiveTypeDescriptionExampleNotes
max-age=<seconds>FreshnessHow long the resource is considered fresh from the time of the requestCache-Control: max-age=31536000 (1 year, for immutable assets)Relative to request time; use long values for versioned static assets
s-maxage=<seconds>Freshness (shared caches)Like max-age but only for shared caches (CDNs), overrides max-age for shared cachesCache-Control: s-maxage=3600, max-age=0CDNs respect s-maxage; browsers ignore it and use max-age
no-cacheRevalidationResource can be cached but must revalidate with origin server before useCache-Control: no-cacheDoes NOT mean 'don't cache' — it means 'check with server first'
no-storeNo cachingDo not store in any cache (browser, CDN, proxy)Cache-Control: no-storeUse for sensitive data (banking, personal info) or highly dynamic content
privateCache scopeOnly cacheable by browser (private cache), not by CDNs/proxiesCache-Control: private, max-age=3600Default for authenticated responses; use public for CDN-cached content
publicCache scopeCacheable by any cache including shared CDNs and proxiesCache-Control: public, max-age=86400Required for CDN caching of content behind auth (with care)
must-revalidateRevalidationOnce stale, the cached response cannot be used without successful revalidationCache-Control: max-age=3600, must-revalidatePrevents serving stale content even when offline
stale-while-revalidateStale handlingServe stale content while fetching fresh version in backgroundCache-Control: max-age=3600, stale-while-revalidate=86400Improves perceived performance; user gets instant response, fresh content loads async
stale-if-errorError fallbackServe stale content if origin server returns 5xx errorCache-Control: stale-if-error=604800Graceful degradation during origin outages; use with monitoring
immutableFreshness optimizationResource will never change; browser skips revalidation checks even on user reloadCache-Control: max-age=31536000, immutableOnly for versioned assets (app.abc123.js); prevents unnecessary revalidation on reload

Validation

MechanismHeaderDescriptionExampleNotes
ETagETag: "abc123"Opaque identifier for a specific version of a resource, compared via If-None-MatchClient sends If-None-Match: "abc123"; server responds 304 Not Modified if unchangedStrong ETags guarantee byte-for-byte identity; weak ETags (W/) allow semantic equivalence
Last-ModifiedLast-Modified: Wed, 21 Oct 2024 07:28:00 GMTTimestamp of last modification, compared via If-Modified-SinceClient sends If-Modified-Since: <date>; server responds 304 if not modified1-second resolution; ETag is more precise and preferred for programmatic use

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
directivesdirectivestringmax-age=<seconds>
directivestypestringFreshness
directivesdescriptionstringHow long the resource is considered fresh from …
directivesexamplestringCache-Control: max-age=31536000 (1 year, for im…
directivesnotesstringRelative to request time; use long values for v…
validationmechanismstringETag
validationheaderstringETag: "abc123"
validationdescriptionstringOpaque identifier for a specific version of a r…
validationexamplestringClient sends If-None-Match: "abc123"; server re…
validationnotesstringStrong ETags guarantee byte-for-byte identity; …

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.yunusemrevurgun.com/api/dataset/http-caching-guide?format=toon"
const res = await fetch(
  "https://yjtoon.yunusemrevurgun.com/static-data/dataset/http-caching-guide.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • http
  • caching
  • cache-control
  • etag
  • cdn
  • performance
  • browser-cache

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets