Back Home
2025-02-14 Web Development

Brotli vs Gzip: The Compression Showdown

A deep dive into how Brotli and Gzip work, when Brotli's higher compression actually matters, and why the answer isn't always 'use Brotli everywhere'.

The Short Version

Brotli (quality 4) compresses 15-25% smaller than Gzip (level 6) for text assets, with comparable compression speed. For most static hosting, Brotli is the right default. But the details matter.

How They Work

Gzip

Based on DEFLATE — a combination of LZ77 (finding repeated sequences) and Huffman coding (using shorter codes for more common symbols). It’s been the web compression standard since 1998.

Strengths: Ubiquitous. Every browser, every server, every CDN supports it. Fast compression and decompression.

Weaknesses: Smaller dictionary (32KB window). Less effective on larger files where patterns repeat beyond the window.

Brotli

Developed by Google in 2013. Uses a larger window (up to 16MB) and a pre-defined static dictionary of common web strings.

Strengths: Significantly better compression ratios on text (HTML, CSS, JS, JSON, SVG). The static dictionary captures common patterns that don’t even need to appear in the input.

Weaknesses: Higher compression levels are CPU-intensive. Not supported on all CDNs for dynamic compression. Only works with HTTPS (a feature, not a bug, but worth noting).

The Benchmarks

On typical web assets (minified JS, CSS, HTML):

AlgorithmSize vs OriginalCompression TimeDecompression Time
Gzip -6~33%FastVery Fast
Brotli -4~28%Comparable to Gzip-6Fast
Brotli -11~22%Very SlowFast

Key insight: Brotli-4 gives you the best compression-to-speed ratio. Brotli-11 is only worth it for static assets you can pre-compress at build time.

When to Use What

Static Assets (pre-compressed at build time)

Use Brotli-11. The compression is a one-time cost. The savings are delivered millions of times.

Dynamic Content (compressed on-the-fly by the server)

Use Brotli-4 for HTTPS clients, Gzip-6 as fallback. The compression ratio difference at these speeds is still meaningful (~15%), but the latency penalty is negligible.

API Responses (small, frequent JSON)

It barely matters. Both algorithms compress small JSON payloads effectively. Brotli’s advantage shrinks with smaller inputs.

Implementation Checklist

  1. Pre-compress your static assets with both Brotli and Gzip at build time.
  2. Configure your server to prefer Brotli and fall back to Gzip.
  3. Set Vary: Accept-Encoding on responses to avoid serving Brotli to Gzip-only clients through caches.
  4. Use compression quality 4-6 for dynamic content. Save quality 11 for static files.
  5. Monitor your TTFB — if dynamic Brotli compression is slowing responses, dial back the quality level.

The Verdict

Brotli is the better algorithm. Use it for everything you can pre-compress, and at moderate quality levels for dynamic content. But always provide a Gzip fallback — supporting 100% of users matters more than saving 3% on 2% of requests.