CDN Compression: Brotli vs Gzip Analysis

· James Thornton · 4 min read

Why Compression Matters

Text-based assets (HTML, CSS, JavaScript, JSON, SVG) compress dramatically — typically 60-80% size reduction. Compression directly reduces transfer time and bandwidth costs. Every CDN supports compression; the question is which algorithm and how to configure it. For foundational context, read CDN Performance Metrics: What to Measure — FlyFish NWA.

Gzip vs Brotli

FeatureGzipBrotli
Compression ratioBaseline15-25% better than gzip
Compression speedFast (all levels)Fast (levels 1-4), slow (levels 5-11)
Decompression speedFastFaster than gzip
Browser supportUniversal97%+ (all modern browsers)
HTTPS requiredNoYes (browsers require HTTPS for Brotli)

Compression Level Trade-offs

Both algorithms offer configurable compression levels:

Performance Comparison: Edge vs Origin Processing
Metric Edge Processing Origin Processing Improvement
Latency (p50)8–15 ms120–300 ms15–20x faster
Cache Hit Ratio85–95%N/AReduces origin load
TLS Handshake1–5 ms (nearby PoP)50–150 ms10–30x faster
Bandwidth Cost$0.01–0.04/GB$0.05–0.12/GB3–5x cheaper
Availability99.99% (multi-PoP)99.9% (single DC)10x fewer outages
Content Delivery Flow — Optimization pipeline from request to responseContent Delivery Flow — Optimization pipeline from request to response CDN Request Flow — From Browser to Edge CDN Request Lifecycle Step 1 DNS Lookup Step 2 TCP Connect Step 3 TLS Handshake Step 4 Edge Server Step 5 Cache Check Step 6 Response Cache HIT → 5ms response Cache MISS → Fetch origin 0ms ~50ms (cache hit) ~200ms (cache miss)
Figure 1: Content Delivery Flow — Optimization pipeline from request to response
Gzip levels 1-9:
  Level 1: Fast compression, larger files
  Level 6: Default balance (most CDNs use this)
  Level 9: Maximum compression, slow

Brotli levels 0-11:
  Levels 1-4: Fast, for on-the-fly compression at the edge
  Level 5-6: Good balance for dynamic content
  Level 11: Maximum compression, very slow (pre-compress only)

CDN Compression Strategy

The optimal strategy depends on whether content is static or dynamic:

Performance Impact — Measurable improvements from optimizationPerformance Impact — Measurable improvements from optimization CDN Performance Dashboard — Core Web Vitals Core Web Vitals with CDN LCP 1.8s Target: < 2.5s Good FID 45ms Target: < 100ms Good CLS 0.05 Target: < 0.1 Good TTFB 120ms Target: < 800ms Good
Figure 2: Performance Impact — Measurable improvements from optimization

Static Assets (Pre-Compressed)

Upload both gzip and Brotli-compressed versions at maximum compression level:

# Build step:
brotli -q 11 -o style.css.br style.css    # Maximum Brotli
gzip -9 -k style.css                       # Maximum gzip

# CDN serves the pre-compressed version matching Accept-Encoding

This gives the best compression ratio with zero CDN CPU overhead.

Dynamic Content (On-the-fly)

CDN compresses responses in real-time using lower compression levels:

Accept-Encoding: gzip, br

# CDN selects:
# Brotli level 4 if supported (fast compression, good ratio)
# Gzip level 6 as fallback

Size Comparison

File TypeOriginalGzip-6Brotli-4Brotli-11
React bundle (JS)500 KB145 KB130 KB118 KB
CSS framework200 KB32 KB27 KB22 KB
HTML page50 KB14 KB12 KB10 KB
JSON API response100 KB12 KB10 KB8 KB

Brotli-4 (fast, suitable for on-the-fly) beats Gzip-6 by 10-15%. Brotli-11 (pre-compressed) beats Gzip-9 by 15-25%.

CDN Configuration

Compression and Caching

The CDN must cache separate copies for each encoding. The Vary: Accept-Encoding header ensures this. Without it, a Brotli-encoded response might be served to a client that only supports gzip. See HTTP caching fundamentals.

External References

References & Standards: Cloudflare documentation · RFC 7234