Skip to main content
Sonamu uses the @fastify/compress plugin to automatically compress HTTP responses. It supports various compression algorithms including gzip, brotli (br), and deflate.

Why is Compression Needed?

Compression reduces the size of data transmitted over the network.

Without Compression

With Compression

Benefits:
  • Transfer size: 100KB to 10KB (90% reduction)
  • Download time: 1 second to 0.1 second (10x faster)
  • Bandwidth savings

Basic Configuration

sonamu.config.ts

Default behavior:
  • threshold: 1024 (only compress 1KB or larger)
  • encodings: ["br", "gzip", "deflate"] (in priority order)
  • Automatically applied to all APIs

Configuration Options

Simple enable/disable

Compression Algorithms

Brotli (br)

The algorithm with the highest compression ratio
Characteristics:
  • Compression ratio: Best (15-20% better than gzip)
  • Speed: Slow (high CPU usage)
  • Support: Modern browsers (not supported in IE)
Suitable for:
  • Static content (can be pre-compressed)
  • When bandwidth is critical
  • When server CPU has capacity

Gzip

The most universal algorithm
Characteristics:
  • Compression ratio: Medium
  • Speed: Fast
  • Support: All browsers
Suitable for:
  • Dynamic API responses
  • Real-time compression
  • When compatibility is important

Deflate

Legacy algorithm
Characteristics:
  • Compression ratio: Similar to gzip
  • Speed: Fast
  • Support: Most browsers
Recommendation: Use gzip (more stable)

Compression Priority

Set priority when the browser supports multiple encodings.
How it works:

Threshold (Minimum Size)

Small responses are not compressed.
Reasons:
  • Small data has low compression efficiency
  • Compression/decompression overhead may be larger
  • HTTP header size increases
Recommended values:
  • Default: 1024 (1KB)
  • Aggressive: 256 (256B)
  • Conservative: 4096 (4KB)

Example

Custom Types

Compress only specific Content-Types.
Default behavior: Compresses most text-based responses
  • text/* (text/html, text/css, text/plain)
  • application/json
  • application/javascript
  • application/xml
Images/videos are already compressed:
  • image/png, image/jpeg (already compressed formats)
  • video/mp4 (already compressed format)
  • Additional compression has no effect

Control with Function

Practical Configuration Examples

Settings used:
  • threshold: 1024 (1KB)
  • encodings: ["br", "gzip", "deflate"]

2. High-Performance API Server

Characteristics:
  • Maximum compression ratio (brotli priority)
  • Compress everything 256 bytes and above
  • Bandwidth optimization

3. Legacy Browser Support

Characteristics:
  • Support for old browsers like IE
  • Use gzip only (stable)

4. Selective Compression

When you want to control compression per API:
Use case:
  • Most APIs don’t need compression
  • Selectively compress specific APIs

5. CPU Saving (Conservative)

Characteristics:
  • Minimize CPU usage
  • Only compress large responses
  • Fast processing

Environment-Based Configuration

Development environment: Disable compression
  • Easier debugging
  • Fast response (no compression overhead)
Production: Aggressive compression
  • Bandwidth savings
  • Improved user experience

Verifying Compression

Browser Developer Tools

curl Command

Precautions

Precautions when using compression:
  1. Exclude already compressed files: Images and videos have no compression benefit
  2. Don’t compress small responses: Use threshold setting
  3. Consider CPU load: brotli has high CPU usage
  4. brotli only supported over HTTPS: Use gzip over HTTP
  5. Watch for decompression errors: Verify that the client decompresses properly

Performance Impact

Advantages

  • Bandwidth savings: 70-90% size reduction
  • Download speed: Especially effective on slow networks
  • Cost reduction: Reduced CDN transfer volume

Disadvantages

  • Increased CPU usage: Compression/decompression overhead
  • Response latency: Additional compression time (usually a few ms)
  • Memory usage: Compression buffers

Recommendations

Next Steps

Compression Presets

Predefined compression settings

Per-API Control

Compression settings in @api decorator

Performance Optimization

Compression optimization strategies