# HTTP Caching, CDNs and Client Caches

> Use Cache-Control, ETags and CDNs so browsers and edge servers do the work instead of your origin. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/http-caching-cdn
Last updated: 2026-11-28

Use Cache-Control, ETags and CDNs so browsers and edge servers do the work instead of your origin.

Intermediate · about 4 hours · difficulty 3/5

Learn first: [HTTP in Depth](https://www.internhack.in/roadmaps/tracks/backend/http-protocol), [Server-Side Caching with Redis](https://www.internhack.in/roadmaps/tracks/backend/server-side-caching)

The fastest request is one that never reaches your server. HTTP has a built-in caching model that browsers, proxies and **CDNs** understand; your job is to send the right headers.

### Cache-Control

- `Cache-Control: public, max-age=31536000, immutable` for fingerprinted static assets (`app.3f9a1c.js`).
- `Cache-Control: private, max-age=60` for per-user responses that only the browser may cache.
- `Cache-Control: no-store` for sensitive data that must never be cached.
- `s-maxage` sets a different lifetime for shared caches like CDNs; `stale-while-revalidate` serves slightly old content while fetching fresh content in the background.

### Validation

With an **ETag** (a version fingerprint) or `Last-Modified`, clients send `If-None-Match` or `If-Modified-Since` and your server can reply **304 Not Modified** with no body, saving bandwidth.

### CDNs

A **Content Delivery Network** (Cloudflare, Fastly, CloudFront, Akamai) caches responses at edge locations close to users, absorbing traffic spikes and reducing latency. Beyond static files, CDNs can cache API responses, terminate TLS, block attacks and run edge functions. Plan for **purging** when content changes.

### Client-side caching

Browsers also cache in memory and on disk, and apps use Service Workers, localStorage or client libraries (React Query, SWR) to cache API data. The backend's headers and ETags make those clients efficient.

### Pitfalls

- Caching authenticated responses on a shared CDN without varying on the right headers, leaking one user's data to another.
- Forgetting the `Vary` header when responses differ by `Accept-Encoding` or language.

You are ready to move on when your static assets are served from a CDN with long-lived immutable caching and an API endpoint returns ETags that produce 304 responses.

## Key concepts

- **Cache-Control directives**
- **ETag and 304 Not Modified**
- **Vary header**
- **CDNs and edge caching**
- **Cache purging**
- **stale-while-revalidate**
- **Client-side caching**

## Mini project

Put a free CDN in front of your app, configure immutable caching for static assets and short shared caching for a public API endpoint, and verify cache hits using response headers.

## Check yourself

Why is putting 'Cache-Control: public' on a response that includes the logged-in user's name dangerous behind a CDN?

## Free resources

- [MDN: HTTP caching](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching) (MDN)
- [What is a CDN?](https://www.cloudflare.com/learning/cdn/what-is-a-cdn/) (Cloudflare)
- [Prevent unnecessary network requests with the HTTP Cache](https://web.dev/articles/http-cache) (web.dev)

Previous: [Server-Side Caching with Redis](https://www.internhack.in/roadmaps/tracks/backend/server-side-caching) · Next: [File Storage and Uploads with Object Storage](https://www.internhack.in/roadmaps/tracks/backend/file-storage-uploads)

## More in Caching for Speed

- [Server-Side Caching with Redis](https://www.internhack.in/roadmaps/tracks/backend/server-side-caching)
- [HTTP Caching, CDNs and Client Caches](https://www.internhack.in/roadmaps/tracks/backend/http-caching-cdn)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
