Course contentsShow
← Web Development
Lesson 2140 of 2,98239. Network PerformanceFree lesson

Compression and CDN Configuration

Understand how CDNs handle compression, ensuring proper configuration for edge delivery and cache efficiency.

Compression and CDN Configuration

What you'll learn: How to ensure your CDN properly handles compression for optimal edge delivery and cache efficiency.

When you serve content through a Content Delivery Network (CDN), compression doesn't stop at your origin server—you need to configure how the CDN handles compressed files to maximize performance at the edge.

The CDN Compression Challenge

CDNs sit between your origin server and users. If misconfigured, they might:

  • Store uncompressed versions in cache (wasting space and bandwidth)
  • Re-compress already compressed files (wasting CPU cycles)
  • Serve the wrong compression format to browsers that support better algorithms
  • Miss compression opportunities entirely

Think of it like a relay race: your origin server hands off the baton (compressed file), but the CDN runner needs to know whether to carry it as-is or change their grip.

Key Configuration Points

Vary Header Respect: Your origin should send Vary: Accept-Encoding so the CDN caches separate versions for gzip, Brotli, and uncompressed responses based on client capabilities.

Edge Compression: Many CDNs offer "edge compression" that compresses at their servers. This is useful if your origin can't, but redundant if you've already pre-compressed files with Brotli. Configure whether the CDN compresses dynamically or serves pre-compressed versions.

Cache Key Normalization: CDNs should normalize the Accept-Encoding header in cache keys—otherwise you'd get separate cache entries for gzip, deflate vs gzip vs gzip, deflate, br, fragmenting your cache and reducing hit rates.

Compression Bypass Rules: Configure which file types bypass compression (already-compressed images, videos) to avoid wasting edge server CPU.

Best Practice

Pre-compress static assets with Brotli at build time, upload to your origin, and configure your CDN to serve these pre-compressed files directly—this gives maximum compression with zero edge CPU overhead.

Key Takeaway: Proper CDN compression configuration ensures you cache the right versions, serve optimal formats to each client, and avoid redundant compression work at the edge.