Advanced Backend

Caching Strategies

Redis, CDN, HTTP caching, and cache invalidation.

Speed Through Caching

Caching reduces latency and database load by storing frequently accessed data closer to where it's needed.

Cache Layers

  • Browser Cache: HTTP headers (Cache-Control, ETag)
  • CDN: Edge servers worldwide (Cloudflare, Vercel Edge)
  • Application Cache: In-memory (Redis, Memcached)
  • Database Cache: Query cache, materialized views

Caching Patterns

  • Cache-Aside: App checks cache, then DB
  • Write-Through: Write to cache and DB together
  • Write-Behind: Write to cache, async DB write

HTTP Cache Headers

Cache-Control: public, max-age=31536000
Cache-Control: private, no-cache
Cache-Control: no-store
ETag: "abc123"
Last-Modified: Wed, 21 Oct 2023 07:28:00 GMT

Cache Invalidation

"There are only two hard things in CS: cache invalidation and naming things."

  • Time-based expiration (TTL)
  • Event-based invalidation
  • Versioned keys