Intermediate Performance

Caching Strategies

Browser cache, CDNs, service workers, and cache invalidation.

Caching for Speed

Effective caching dramatically reduces load times for returning visitors.

Browser Caching

# Immutable assets (hash in filename)
Cache-Control: public, max-age=31536000, immutable

# Dynamic content
Cache-Control: private, no-cache

# Never cache
Cache-Control: no-store

Caching Layers

  1. Browser Cache: Local storage on device
  2. CDN Cache: Edge servers worldwide
  3. Application Cache: Redis, in-memory
  4. Database Cache: Query cache, indexes

Service Workers

// Cache-first strategy
self.addEventListener('fetch', (event) => {
  event.respondWith(
    caches.match(event.request)
      .then(cached => cached || fetch(event.request))
  );
});

CDN Best Practices

  • Cache static assets at the edge
  • Use content-based hash in filenames
  • Set appropriate cache headers
  • Purge cache on deployments