Next Generation Server Compression With Brotli

Learn how Google's compression algorithm delivers 10-20% better compression than GZIP while maintaining reasonable CPU overhead--essential knowledge for Core Web Vitals optimization.

In the ongoing battle for faster website delivery, server-side compression remains one of the most impactful optimizations available. For nearly two decades, GZIP served as the undisputed standard for reducing file sizes during transit. But as web applications grew more complex and user expectations for instant loading intensified, the need for a next-generation solution became clear.

Enter Brotli--a compression algorithm developed by Google that promises significantly better compression ratios while maintaining reasonable computational overhead. This guide explores how Brotli works, why it matters for Core Web Vitals, and how to implement it across your server infrastructure.

Brotli Compression Impact

10-20%

Better compression than GZIP

95%+

Browser support coverage

20-30%

Transfer size reduction

3-4

Recommended compression level

Understanding Brotli Compression

What Makes Brotli Different from GZIP

Brotli is a lossless data compression algorithm developed by Google and released as open source in 2015. While both Brotli and GZIP share foundational technologies--the LZ77 algorithm and Huffman coding--their implementations differ significantly in ways that impact real-world performance.

The key differentiator is Brotli's static dictionary, containing over 13,000 common strings from HTML, CSS, JavaScript, and common web patterns. This pre-built dictionary allows the encoder to reference known patterns directly, rather than discovering them during compression. The result is faster compression times and better ratios for web content specifically.

Additionally, Brotli employs a larger sliding window (up to 16MB compared to GZIP's 32KB), enabling it to find more patterns across larger data sets. This expanded context window is particularly effective for larger files and pages with repetitive structures.

Compression Levels Explained

Brotli offers 11 compression levels (0-11), each representing a different balance between compression ratio and CPU time:

  • Levels 1-3: Fastest compression, suitable for high-traffic dynamic content where speed matters more than size
  • Levels 4-6: The recommended sweet spot for most web applications--strong compression without excessive CPU cost
  • Levels 7-9: Better compression for static assets where CPU cost is paid once during build/deploy
  • Levels 10-11: Maximum compression, rarely needed and CPU-intensive

For dynamic content generated on every request, levels 3-4 provide the best balance for most servers. For static assets pre-compressed at build time, levels 9-10 deliver maximum savings with one-time CPU cost.

How Brotli Impacts Core Web Vitals

The relationship between compression and Core Web Vitals is direct and measurable. When your server compresses HTML, CSS, and JavaScript more efficiently, three critical things happen:

Largest Contentful Paint (LCP) improves because compressed assets arrive faster, particularly on mobile networks with limited bandwidth. Studies show that properly configured compression can reduce transfer sizes by 20-30% compared to uncompressed content, and an additional 10-20% when switching from GZIP to Brotli.

Time to First Byte (TTFB) requires careful balance--compression itself doesn't reduce TTFB, but overly aggressive compression levels can increase it as the CPU spends more time compressing each response. The key is finding the sweet spot between compression ratio and CPU overhead.

Interaction to Next Paint (INP) benefits indirectly because smaller JavaScript bundles load and parse faster, reducing the time until the page becomes interactive. For comprehensive Core Web Vitals optimization, pair Brotli compression with our /services/core-web-vitals-optimization/ services.

To measure the impact of compression on your Core Web Vitals, use tools like Google Lighthouse to establish baseline scores before and after implementation. Additionally, our guide on Core Web Vitals tools covers the best monitoring solutions for tracking performance over time.

Server Infrastructure Considerations

Implementing Brotli effectively requires proper server infrastructure. Our /services/cloud-hosting-infrastructure/ team can help configure your servers for optimal compression performance while maintaining fast response times.

Frequently Asked Questions

Is Brotli always better than GZIP for website performance?

Brotli usually achieves better compression ratios than GZIP for text-based assets like HTML, CSS, and JavaScript. However, Brotli's higher levels can be CPU intensive, which may increase TTFB on weaker servers or under heavy load. The best strategy is to enable Brotli at a moderate level (3-4) for modern clients and keep GZIP as a fallback.

What compression level should I use for Brotli?

For dynamic content, start with Brotli compression level 3-4. These levels give strong reduction in response size without significantly increasing CPU time per request on typical servers. Use levels 9-11 only for precompressed static assets generated at build or deploy time.

Should I enable both Brotli and GZIP, or choose just one?

Enable both. Modern browsers over HTTPS will prefer Brotli when you advertise it, gaining smaller payloads and better performance. Older browsers and some tools still only support GZIP, so disabling GZIP entirely can break compatibility for part of your audience.

How can I check if Brotli compression is working?

Use curl with the Accept-Encoding header: `curl -I -H 'Accept-Encoding: br' https://example.com/`. In the response headers, look for `Content-Encoding: br`. You can also check in browser DevTools under the Network tab.

Implementing Brotli on Nginx

Installation and Verification

Most modern Nginx distributions include Brotli support. Verify your installation:

nginx -V 2>&1 | grep -i brotli

If Brotli isn't included, install the nginx-extras package:

sudo apt install nginx-extras

Production Configuration

A production-ready Nginx Brotli configuration balances compression efficiency with CPU considerations:

Nginx Brotli Configuration
1http {2 # Enable both compression types3 brotli on;4 brotli_comp_level 4;5 brotli_types text/plain text/css text/javascript6 application/javascript application/json7 application/xml application/rss+xml8 image/svg+xml font/ttf font/otf font/woff font/woff2;9 10 # GZIP as fallback11 gzip on;12 gzip_comp_level 4;13 gzip_types text/plain text/css text/javascript14 application/javascript application/json15 application/xml application/rss+xml16 image/svg+xml font/ttf font/otf font/woff font/woff2;17 18 # Precompressed static asset support19 brotli_static on;20 gzip_static on;21}

Key configuration notes:

  • Set brotli_comp_level 4 as a starting point--adjust based on CPU monitoring
  • Include brotli_static on to serve pre-compressed .br files when available
  • Keep GZIP enabled as a fallback for clients that don't support Brotli
  • The Vary: Accept-Encoding header is automatically added by Nginx

Precompressing Static Assets

For maximum performance with static assets, pre-compress files during your build process:

# Using Brotli CLI for precompression
find dist -type f \( -name '*.js' -o -name '*.css' -o -name '*.html' \) \
 -exec brotli -f -q 10 {} \;

This approach moves the CPU cost from every request to a single build operation. Combined with our /services/performance-optimization/ services, you can achieve optimal compression across all your web assets.

For CI/CD integration, consider adding precompression to your deployment pipeline to ensure all static assets are optimized before deployment. To verify your compression implementation is working correctly and measure the performance gains, follow our guide on running page speed tests to establish baselines and track improvements over time.

Implementing Brotli on Apache

Module Installation

Apache requires two separate modules--mod_deflate for GZIP and mod_brotli for Brotli:

# In httpd.conf or apache2.conf
LoadModule deflate_module modules/mod_deflate.so
LoadModule brotli_module modules/mod_brotli.so

# Restart Apache
systemctl restart httpd

Apache Configuration

Production Apache configuration for both compression methods:

Apache Brotli and GZIP Configuration
1<IfModule mod_deflate>2 AddOutputFilterByType DEFLATE \3 text/plain text/html text/xml text/css \4 text/javascript application/javascript \5 application/json application/xml \6 application/xhtml+xml application/rss+xml \7 image/svg+xml font/ttf font/otf font/woff font/woff28 9 # Exclude already-compressed formats10 SetEnvIfNoCase Request_URI \11 .(?:gif|jpe?g|png|webp|avif|mp4|mp3|ogg|pdf|zip|gz)$ \12 no-gzip dont-vary13 14 DeflateCompressionLevel 415 Header append Vary Accept-Encoding env=!dont-vary16</IfModule>17 18<IfModule mod_brotli>19 BrotliCompressionQuality 420 BrotliWindow 1621 22 AddOutputFilterByType BROTLI_COMPRESS \23 text/plain text/html text/xml text/css \24 text/javascript application/javascript \25 application/json application/xml \26 application/xhtml+xml application/rss+xml \27 image/svg+xml font/ttf font/otf font/woff font/woff228 29 Header append Vary Accept-Encoding30</IfModule>

Browser Support and Compatibility

Current Browser Coverage

Brotli enjoys excellent browser support, with over 95% of global users accessing browsers that support the compression format. However, there's a critical requirement: Brotli is typically only available over HTTPS.

This HTTPS requirement is intentional--early implementations showed that some transparent proxies would strip or corrupt the br content-encoding, breaking pages for users behind such proxies. HTTPS ensures end-to-end encoding integrity.

All major modern browsers support Brotli:

  • Chrome/Edge (desktop and mobile): Full support since 2015
  • Firefox: Full support since 2017
  • Safari: Full support since macOS High Sierra / iOS 11
  • Opera: Full support since 2015

Creating a Fallback Strategy

The recommended approach is enabling both Brotli and GZIP, letting the server negotiate based on the client's Accept-Encoding header:

  1. Server advertises both br and gzip via Accept-Encoding
  2. Brotli-capable browsers request br encoding
  3. Older browsers request gzip encoding
  4. Server responds with the appropriate encoding

The Vary: Accept-Encoding header is essential for this to work correctly with CDNs and proxies. When implementing server-side optimizations like Brotli, ensure your /services/cdn-optimization/ configuration is compatible with your compression settings.

Brotli and HTTP/2 Synergy

Why They Work Well Together

Brotli and HTTP/2 create a powerful performance combination. HTTP/2's multiplexing eliminates head-of-line blocking, while Brotli's better compression reduces the data that multiplexing carries. The result is faster page loads, especially on high-latency connections.

HTTP/2 header compression (HPACK) also benefits from smaller payloads--compressed HTML/CSS/JS means less data for HPACK to process, further reducing overhead.

Combined Configuration Example

server {
 listen 443 ssl http2;
 server_name yourdomain.com;

 ssl_certificate /path/to/certificate.crt;
 ssl_certificate_key /path/to/private.key;

 # Brotli with optimal settings
 brotli on;
 brotli_comp_level 4;
 brotli_types text/plain text/css text/javascript
 application/javascript application/json
 application/xml application/rss+xml
 image/svg+xml font/ttf font/otf font/woff font/woff2;
 brotli_static on;

 # GZIP fallback
 gzip on;
 gzip_comp_level 4;
 gzip_types text/plain text/css text/javascript
 application/javascript application/json
 application/xml application/rss+xml
 image/svg+xml font/ttf font/otf font/woff font/woff2;
 gzip_static on;
}

Implementing Brotli alongside HTTP/2 is a core component of our comprehensive /services/website-speed-optimization/ methodology, delivering measurable improvements in Core Web Vitals scores. For automated performance monitoring in your CI/CD pipeline, consider integrating Lighthouse CI to catch compression issues before deployment.

Common Implementation Mistakes

Pitfalls to Avoid

  1. Overly aggressive compression levels: Setting level 9-11 for dynamic content will spike CPU usage and increase TTFB. Reserve high levels for pre-compressed static assets.

  2. Compressing already-compressed formats: Never compress images (JPEG, PNG, WebP, AVIF), videos, or archives--they're already compressed, and the CPU cost yields no benefit.

  3. Missing Vary: Accept-Encoding: Without this header, CDNs and proxies may serve the wrong encoding to some users.

  4. Neglecting GZIP fallback: Disabling GZIP entirely breaks compatibility for older browsers and some tools.

  5. Not verifying implementation: Assuming compression is working without testing leads to surprises when you check metrics.

Testing Your Implementation

# Verify Brotli is active
curl -I -H 'Accept-Encoding: br' https://yourdomain.com/

# Verify GZIP fallback works
curl -I -H 'Accept-Encoding: gzip' https://yourdomain.com/

Look for Content-Encoding: br in Brotli-enabled responses and Content-Encoding: gzip in fallback responses. For a comprehensive performance audit, our team can help identify and resolve compression configuration issues as part of our /services/performance-audit/ services.

Ready to Optimize Your Website Performance?

Brotli compression is just one of many techniques we use to deliver exceptional web performance. Our team can help you implement server optimizations that improve Core Web Vitals and user experience.