Skip to content
  • Caching · API Design

Caching

LearnViaNotes @learnvianotes· 1 min read

The fastest API call is the one you don't make. HTTP has built-in caching primitives — use them.

Caching layers

Client cache → CDN / Edge → Reverse proxy → App cache (Redis) → Database

HTTP caching headers

Cache-Control
// Public — CDN + browser can cache
Cache-Control: public, max-age=3600

// Private — only browser caches
Cache-Control: private, max-age=300

// Never cache (auth endpoints, mutations)
Cache-Control: no-store

// stale-while-revalidate: serve stale, refresh in background
Cache-Control: public, max-age=60, stale-while-revalidate=600
ETag
// Server response:
ETag: "v2-abc123"

// Client's next request:
If-None-Match: "v2-abc123"

// Unchanged → 304 Not Modified (no body = saves bandwidth)
// Changed → 200 OK + new ETag + new body
Vary
// Different languages = different cache entries
Vary: Accept-Language

// NEVER do this — disables CDN caching effectively
Vary: Authorization

What to cache vs not

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report