Black Friday: 30% off every plan through November 30. See the prices
SysDesignPrep.com
Study guide

Updated 24 October 2026

HTTP caching and cache headers

How browsers, CDNs and proxies decide what to cache: Cache-Control directives, max-age and s-maxage, ETags and conditional requests, immutable assets with fingerprinted URLs, stale-while-revalidate, Vary, private versus shared caches, and purging.

Reading is half of it. Now try it in an interview.An AI interviewer asks you to design a URL Shortener, out loud or typed, and scores the answer for your level. Or watch a sample interview first, free with no sign-up.
Start an AI mock

Try it yourself, in a playground:CDN caching and hit ratioHTTP caching

The cheapest request is the one that never reaches your servers. HTTP has a built-in caching system used by every browser, CDN and proxy, controlled by a few response headers. Getting them right can remove most of your traffic and make pages load instantly; getting them wrong serves one user's private data to another or leaves stale content stuck for days. Interviewers expect you to know the basics whenever a CDN appears in your design.

Cache-Control

The main header. Common directives:

DirectiveMeaning
max-age=Nfresh for N seconds in any cache
s-maxage=Nfresh for N seconds in shared caches (CDNs), overriding max-age there
publicmay be stored by shared caches
privateonly the user's browser may store it
no-cachemay store, but must revalidate with the server before each use
no-storenever store anywhere (sensitive data)
immutablewill never change; do not even revalidate
stale-while-revalidate=Nserve stale for up to N seconds while fetching a fresh copy in the background
stale-if-error=Nserve stale for up to N seconds if the origin fails

Note the common confusion: no-cache does not mean "do not cache"; no-store does.

Revalidation with ETags

When a cached copy is stale, the cache can ask the server whether it changed instead of downloading it again:

  • The server sends an ETag (a version identifier, often a hash) or Last-Modified.
  • The cache sends If-None-Match: <etag> (or If-Modified-Since).
  • If unchanged, the server replies 304 Not Modified with no body.

This saves bandwidth, not round trips. ETags also enable optimistic concurrency for writes (If-Match). See API design.

Patterns by content type

ContentHeadersWhy
Static assets with hashed names (app.3f9a1c.js)public, max-age=31536000, immutablethe URL changes when content changes, so cache forever
HTML pagespublic, max-age=0, s-maxage=60, stale-while-revalidate=300 or no-cachealways fresh-ish; CDN absorbs load
Public API responses (popular lists)public, s-maxage=30, stale-while-revalidate=60short CDN caching of shared data
Personalised responsesprivate, max-age=0 or no-storenever in shared caches
Images and video segmentslong max-age with versioned URLshuge bandwidth savings
Redirects for short links301 cached long, or 302 with a short max-agetrade analytics accuracy for load

The fingerprinted URL pattern (cache forever, change the name on deploy) is the most effective caching technique on the web. See Design Netflix and Design YouTube.

Vary

Vary tells caches which request headers change the response (for example Vary: Accept-Encoding for compressed versus plain). Varying on high-cardinality headers like Cookie or User-Agent effectively disables shared caching, since every user gets a separate entry. Normalise at the CDN instead (device class rather than full user agent).

Never leak private data

The worst caching bug is a CDN storing a response with one user's data and serving it to others. Prevent it:

  • Mark anything personalised private or no-store.
  • Configure the CDN to bypass caching when an Authorization header or session cookie is present, unless explicitly allowed.
  • Separate public and personalised endpoints; fetch personal data client-side after a cached public shell.

Purging and invalidation

  • Versioned URLs avoid invalidation entirely.
  • Purge APIs remove a URL or a tag (surrogate keys) from the CDN in seconds, for content that must change at a fixed URL.
  • Short TTLs with stale-while-revalidate give near-fresh content with high hit rates and no purge logic.

See CDN and edge and caching.

Browser and service worker caches

Browsers also have memory and disk caches, plus service workers that can implement custom strategies (cache first, network first) for offline apps. See offline-first apps and sync.

In the interview

When you add a CDN, say what it caches and for how long: "static assets are fingerprinted and cached for a year; the redirect responses are cached at the edge with a short s-maxage and stale-while-revalidate; personalised feed responses are private." For Design a URL Shortener, discuss 301 versus 302 and its effect on click analytics.

Checklist

  • Fingerprinted static assets with a one-year max-age and immutable.
  • Short s-maxage plus stale-while-revalidate for shared dynamic content.
  • ETags for cheap revalidation.
  • private or no-store for anything personal; CDN bypass on auth.
  • Careful Vary; no Vary on Cookie for shared content.
  • Versioned URLs or tag-based purges for invalidation.

Questions people ask

What is HTTP caching?

HTTP caching is the built-in system that lets browsers, CDNs and proxies store responses and reuse them, controlled by a few response headers, mainly Cache-Control. The cheapest request is the one that never reaches your servers, so getting the headers right can remove most of the traffic and make pages load instantly. Getting them wrong can serve one user's private data to another or leave stale content stuck for days.

What is the difference between no-cache and no-store?

no-cache lets a response be stored but requires revalidation with the server before each use, while no-store means it must never be stored anywhere. The names cause a common confusion: no-cache does not mean "do not cache", no-store does. Use no-store for sensitive data, and no-cache when content may be stored but must be checked before reuse, as HTML pages sometimes are.

What is the difference between max-age and s-maxage?

max-age=N makes a response fresh for N seconds in any cache, while s-maxage=N applies to shared caches such as CDNs and overrides max-age there. Combining them lets the browser check back often while the CDN absorbs the load. A typical setting for HTML pages is public, max-age=0, s-maxage=60, stale-while-revalidate=300.

What is an ETag and how does it work?

An ETag is a version identifier for a response, often a hash, that lets a cache ask whether a stale copy changed instead of downloading it again. The cache sends If-None-Match with the ETag, and if nothing changed the server replies 304 Not Modified with no body. This saves bandwidth, not round trips, and ETags also enable optimistic concurrency for writes through If-Match.

How long should static assets be cached?

Cache static assets with hashed names, such as app.3f9a1c.js, for a year with public, max-age=31536000, immutable. Because the URL changes whenever the content changes, the old copy never needs invalidating: a deploy simply references the new name. This fingerprinted URL pattern is the most effective caching technique on the web.

How do you stop a CDN from caching private data?

Mark anything personalised private or no-store, and configure the CDN to bypass caching when an Authorization header or session cookie is present unless explicitly allowed. The worst caching bug is a CDN storing a response with one user's data and serving it to others. Separating public and personalised endpoints also helps: serve a cached public shell and fetch personal data client-side.