Skip to main content
Compression can significantly improve network performance, but improper use can actually degrade performance. This document explains optimization strategies for using compression effectively.

The Trade-offs of Compression

Compression trades CPU time for network time.

Break-even Point

Conclusion: The slower the network and larger the data, the greater the compression benefit

Finding the Optimal Threshold

Threshold is the minimum size to compress.

Effects by Threshold

Suitable for:
  • Mobile networks (slow speeds)
  • Expensive bandwidth costs
  • Most responses are small (< 10KB)
Disadvantage: Increased CPU usage

Finding Optimal Value Through Experimentation

Algorithm Selection Strategy

Brotli vs Gzip

Usage Strategies

Pre-compression -> Use Brotli
Advantages:
  • No compression time concerns (pre-compressed)
  • Best compression ratio (quality 11)
  • No runtime CPU load

Environment-Based Strategy

Combining Caching and Compression

Using compression with caching achieves the best performance.

Effect

Per-layer optimization:
  1. Browser cache: 0ms (fastest)
  2. CDN cache: 10ms (network only)
  3. Server cache: 50ms (compression only, no DB)
  4. DB query: 500ms (compression + DB)

CDN and Compression

CloudFront Optimization

CloudFront configuration:
  • Receive compressed response from origin
  • CloudFront caches as-is
  • Serve quickly from edge

Vary Header Consideration

When using compression, responses differ based on Accept-Encoding:

Pre-compression

Pre-compressing static files at build time eliminates runtime CPU load.

Build Script

Server Configuration

Advantages:
  • No runtime CPU load
  • Best compression ratio (quality 11)
  • Instant response

Performance Measurement

1. Browser Developer Tools

2. Server Logging

3. Load Testing

Practical Optimization Checklist

Enable global compression
Set threshold
  • General: 1024 (1KB)
  • Mobile: 256 (256B)
  • High-performance: 4096 (4KB)
Select algorithm
  • Static: brotli (pre-compressed)
  • Dynamic: gzip (real-time)

Common Mistakes

Mistakes to avoid:
  1. Compressing all responses: Inefficient for small responses
  2. Re-compressing already compressed files: No benefit
  3. Compressing streams: Causes delays
  4. Compression in development environment: Difficult debugging
  5. Brotli for real-time compression: High CPU load

Performance Benchmark

Based on actual 100KB JSON response: Conclusion:
  • Gzip: 85% time savings
  • Brotli: 87% time savings
  • Pre-compressed: 88% time savings (best)

Next Steps

Compression Configuration

Global compression plugin settings

Compression Presets

Predefined compression settings

Per-API Control

Compression settings in @api decorator