Illustration comparing Brotli vs gzip compression: two funnels shrinking document blocks into smaller packages

Every text file a CDN delivers (HTML, CSS, JavaScript, SVG) travels compressed, and for years that meant gzip. Brotli is the second option, and many setups now offer both. So in the Brotli vs gzip question, which one should the edge serve, and what breaks if the choice is wrong?

Why does the choice matter if gzip already works?

Gzip is good enough, so nobody revisits it. The cost shows on slow mobile connections, where every kilobyte of render-blocking CSS and JavaScript delays the first paint.

Say a shop in Leeds ships a 200 KB JavaScript bundle. Gzip shrinks it to 60 KB and Brotli at its highest level to 52 KB (illustrative sizes; your files will differ). That's 8 KB less, or 13% (8 / 60). Add 3 KB saved on the stylesheet and 2 KB on the HTML and you get 13 KB per page view. On a 3 Mbps phone connection, 13 KB is 104 kilobits, and 104 / 3,000 kilobits per second is about 35 ms. Not a revolution. But 35 ms is real when you're chasing the last tenth of a second in your Largest Contentful Paint, and once files are precompressed it costs almost nothing.

NetDNA, the CDN whose pull zones, support articles and blog this site archives, documents compression as one zone setting: tick the gzip box, and the edge compresses a fixed list of text types such as text/html, text/css and application/javascript (see How to enable gzip compression). That documentation dates from 2013, and Wikipedia's Brotli article puts Brotli's first release at 15 October 2013, so the article describes gzip only. The practical question today is where Brotli should come from: your origin or your edge.

How much better is Brotli, really?

Vendor charts vary wildly, so it helps to know why Brotli wins. Wikipedia's Brotli entry explains that it combines LZ77, Huffman coding and second-order context modelling, and that it ships with a predefined dictionary of about 120 KiB holding over 13,000 common words and phrases drawn from text and HTML documents. A built-in dictionary pays off most on small files that use ordinary web vocabulary, which describes most HTML, CSS and JavaScript.

Two limits matter in practice. First, Wikipedia notes that, apart from Pale Moon and Basilisk, browsers accept Brotli only over HTTPS, so plain-HTTP visitors will always get gzip. Second, support is broad: the same article reports that 96% of worldwide users had a Brotli-capable browser as of July 2022, and the format became an IETF standard as RFC 7932 in July 2016.

A sensible default follows from that: Brotli for text assets over HTTPS, gzip as the fallback, and nothing at all for files that are already compressed (JPEG, WebP, MP4, WOFF2). Recompressing those wastes CPU and sometimes makes them larger.

Should you compress on the fly or ahead of time?

The highest Brotli levels are slow. Compress a large bundle at maximum level on every request and the origin spends tens or hundreds of milliseconds on it before sending the first byte, which pushes up time to first byte.

Say your build produces app.91c3.css. The better pattern is to compress it once, at build time, at the highest level, and store the results beside the original:

app.91c3.css
app.91c3.css.gz
app.91c3.css.br

Nginx can serve those files directly when it has the Brotli module installed:

gzip_static   on;
gzip_vary     on;
brotli_static on;

The reasoning is simple: the expensive work happens once per deploy instead of once per visitor. Dynamic HTML is the exception, since it can't be precompressed. A mid-range level suits it better, because speed matters more there than the last few percent.

What happens to the cache when two encodings exist?

A cache that stores one compressed copy can hand a Brotli body to a client that only understands gzip, and the page arrives as unreadable bytes. This is the failure people remember.

The fix is the Vary: Accept-Encoding response header. It tells shared caches that the body depends on the request's Accept-Encoding, so they keep one variant per encoding. We covered the same trap in Accept-Encoding, It's Vary important. The trade-off is more cache entries per URL (the uncompressed, gzip and Brotli versions), so the cold-start misses go up a little. Most edges normalise Accept-Encoding into a few buckets to keep that in check.

A two-line test shows what your server sends:

curl -sI -H 'Accept-Encoding: br' https://www.example.com/app.css | grep -iE 'content-encoding|vary'
curl -sI -H 'Accept-Encoding: gzip' https://www.example.com/app.css | grep -iE 'content-encoding|vary'

Each should show the matching content-encoding and a vary line that includes Accept-Encoding.

When gzip alone is the right answer

Brotli isn't worth the setup for every site. If a response already fits in one network packet (roughly 1.4 KB), compression saves nothing visible. If your traffic is still largely plain HTTP, most of the gain never reaches visitors. And if your CDN plan can't store separate variants, forcing Brotli risks the broken-body failure above.

The comparison that settles it takes two commands. Run gzip -9 -k and brotli -q 11 -k on your largest bundle, then read the byte counts. If the difference is under a few percent, gzip stays. If it's the 13% from the example, precompressing for Brotli pays for itself in a day.

Related: Cache-Control headers, enable gzip on a pull zone, CDN glossary.