{"id":"sarqfezhaw4jjit","title":"Brotli vs Gzip for Static Sites (What I Enable in Production)","slug":"brotli-vs-gzip-static-sites","summary":"Gzip is the reliable baseline, but Brotli’s static dictionary consistently shaves another 15-20% off text files like CSS and JS bundles.  To avoid the CPU…","imageUrl":"https://briancrabtree.me/images/journal-brotli-vs-gzip-static-sites.webp","category":"Performance","date":"2026-03-26T18:00:00.000Z","featured":false,"likes":32,"author":"Brian Crabtree","content":"<h2>The Web Compression Decision</h2>\n\n<p>Every millisecond counts for page load speed. When I'm optimizing a static site, one of the first questions always pops up: what's the deal with brotli vs gzip for static sites? It's a common fork in the road for web performance, and the answer has evolved.</p>\n\n<p>For years, Gzip was the undisputed king of web compression. It did its job well, reducing file sizes and speeding up delivery. But like any technology, newer, more efficient options have emerged to challenge its dominance.</p>\n\n<p>My goal is always to deliver content as fast as possible to the end-user without overcomplicating the server setup. This means looking at the real-world impact of compression algorithms, not just theoretical benchmarks or marketing claims. I need something reliable that ships.</p>\n\n<pre><code># nginx — precompressed assets at build time\nlocation ~* \\.(js|css|svg)$ {\n  gzip_static on;\n  brotli_static on;\n}</code></pre>\n\n<p><figure>\n  <img src=\"/images/journal-inline-brotli-vs-gzip.webp\" alt=\"File size comparison of Brotli versus Gzip compression for static assets\" width=\"1200\" height=\"675\" loading=\"lazy\" />\n  <figcaption>Brotli wins on text assets — if nginx serves the .br file.</figcaption>\n</figure></p>\n\n<h2>Gzip A Tried and True Standard</h2>\n\n<p>Gzip, based on the DEFLATE algorithm, has been the workhorse of web compression since the HTTP/1.1 era. It’s universally supported by browsers and web servers, making it a safe default for reducing the bandwidth needed to serve assets. Most CDNs enable it by default, which simplifies a lot of initial setups.</p>\n\n<p>It works by finding repeated patterns in data and replacing them with shorter references. This is highly effective for text-based content like HTML, CSS, and JavaScript, which often contain many repetitive strings. The compression process is relatively fast, so it doesn't add significant latency during runtime.</p>\n\n<p>While effective, Gzip’s compression ratio, especially at its highest levels, can be modest compared to newer algorithms. The algorithms have matured, and there isn't much more room for improvement within its established architecture. It's solid, but not groundbreaking anymore.</p>\n\n<h2>Brotli The Modern Alternative</h2>\n\n<p>Brotli, developed by Google, entered the scene with a clear mandate: achieve better compression ratios than Gzip, particularly for web content. It uses a combination of LZ77, Huffman coding, and a 120KB static dictionary common to web resources, like common keywords in HTML and JavaScript. This dictionary is a key differentiator.</p>\n\n<p>The result is often 15-20% smaller files for comparable quality settings compared to Gzip, sometimes even more for certain file types. This means less data travels over the network, leading to faster downloads for users. For critical CSS or JavaScript bundles, these savings add up quickly.</p>\n\n<p>Brotli also offers different compression levels, similar to Gzip. Higher levels achieve greater compression but take longer to process. For static sites, where compression happens once at build time or on demand for cached assets, this increased processing time is usually a non-issue. The client-side decompression is fast.</p>\n\n<h2>Measuring Real World Gains</h2>\n\n<p>I've run countless tests on briancrabtree.me and client sites comparing the two. The gains with Brotli are consistent and measurable, especially on larger JavaScript bundles and CSS files. A 200KB JavaScript file Gzipped might be 60KB, but Brotli can often shrink it down to 50KB or even less. That's a significant win for initial page loads.</p>\n\n<p>The benefit is most pronounced on textual content. Images and videos are typically already compressed using their own specialized algorithms like JPEG, PNG, or WebP, so applying Brotli or Gzip to them offers minimal to no additional benefit. In fact, it can sometimes make them larger if applied incorrectly.</p>\n\n<p>For users on slower connections or mobile networks, these smaller file sizes translate directly to a snappier experience. It’s not just about raw speed; it’s about reducing the overall data burden. That's a practical advantage for every visitor to your static site, particularly important for global audiences.</p>\n\n<h2>Nginx Configuration Specifics</h2>\n\n<p>When it comes to my Nginx setup for static sites, I configure Brotli first. Most modern browsers support Brotli, so serving it as the primary option makes sense. I instruct Nginx to check if the browser accepts \"br\" encoding via the Accept-Encoding header before falling back to \"gzip\".</p>\n\n<p>The configuration is straightforward but requires the ngx_brotli module. Once compiled or installed, I use brotli on; and set brotli_comp_level to a higher value like 6 or 8 for pre-compressed static assets. For dynamic content, a lower level might be necessary to avoid CPU spikes, but for static, we can afford the time.</p>\n\n<p>It's important to ensure your build process pre-compresses your static assets with Brotli. Relying on Nginx to compress on-the-fly for every request can be CPU-intensive and negate some performance benefits. I usually generate both .br and .gz versions of my assets at build time, then let Nginx serve the appropriate one. This ensures we don't accidentally serve redundant content or break accessibility landmarks like I discussed in <a href=\"/journal/semantic-html-landmarks-beyond-divs/\">Semantic HTML Landmarks Search Engines Actually Read</a> for HTML structure.</p>\n\n<h2>The Tradeoffs and Server Load</h2>\n\n<p>While Brotli offers superior compression, it generally comes at the cost of higher CPU usage during the compression phase. For static sites, this impact is typically mitigated by pre-compressing assets at build time. The server then just serves the already compressed file, with minimal CPU overhead for the request itself.</p>\n\n<p>If you're compressing on-the-fly, particularly on a server with limited resources or high traffic, Gzip might still be the more pragmatic choice. The balance between compression ratio and CPU cycles is a real engineering decision. Always profile your server's performance under load before making a blanket change.</p>\n\n<p>Another consideration is client support. While Brotli is widely supported by modern browsers, legacy browsers might still rely solely on Gzip. This is why a robust server configuration includes a fallback mechanism, ensuring all users receive a compressed version of your assets, even if it's the older standard.</p>\n\n<h2>My Production Playbook Today</h2>\n\n<p>For every new static site project, or when I'm optimizing an existing one, my first move is to implement Brotli compression. I pre-compress all textual assets – HTML, CSS, JavaScript, SVGs – at the highest practical compression level during the build step. These pre-compressed .br files live alongside their uncompressed and Gzipped counterparts.</p>\n\n<p>My Nginx configuration prioritizes serving the Brotli-compressed version if the browser supports it. If not, it falls back to the Gzip version, ensuring broad compatibility and optimal performance for the vast majority of users. This layered approach guarantees we're leveraging the best available compression without leaving anyone behind.</p>\n\n<p>The gains in page load speed and reduced bandwidth are real and worth the minor setup effort. It's a low-hanging fruit for performance optimization that directly impacts user experience and crawl efficiency. If you're looking for other ways to boost your site's speed, don't forget to check out my notes on <a href=\"/journal/webp-avif-modern-image-delivery/\">WebP and AVIF: How I Ship Images Without Regret</a> for modern image delivery.</p>","tags":["brotli","gzip","compression"],"views":77}