Brotli

On this page 7

Brotli is a lossless data compression format, standardized in RFC 7932, that web servers use to shrink text-based HTTP responses such as HTML, CSS, JavaScript and JSON before sending them to browsers. In server performance work it is the modern successor to Gzip compression, typically producing files 15 to 25 percent smaller for the same content at comparable decompression speed.

The format combines a classic LZ77-style matching engine with Huffman coding, context modeling and a built-in static dictionary of common web strings. Browsers advertise support with the br token in the Accept-Encoding header, and the server answers with Content-Encoding: br when it delivers a Brotli-compressed body.

How Brotli compresses web content

Brotli reduces size by replacing repeated byte sequences with back-references and then encoding the result with entropy coding. The encoder searches earlier data in a sliding window of up to 16 MB for matches, which is far larger than the 32 KB window of DEFLATE used by Gzip, so it can find repetitions spread across long files.

Two features give Brotli its advantage on web assets. The static dictionary of about 120 KB contains frequent words and fragments from HTML, CSS, JavaScript and natural language, so even short responses can reference common strings without having seen them before. Second-order context modeling lets the encoder choose different Huffman codes depending on the preceding bytes, which captures structure that a single code table misses.

The table below summarizes the main building blocks and what each contributes to the final size.

ComponentFunctionEffect on output
LZ77 matchingReplaces repeats with distance and length pairsRemoves redundancy
Large windowAllows references up to 16 MB backBetter ratio on large files
Static dictionaryProvides predefined web stringsHelps small responses most
Context modelingSelects code tables by preceding bytesTighter entropy coding
Huffman codingAssigns short codes to frequent symbolsCompact final bitstream
Components of the Brotli format

Compression levels and their trade-offs

Brotli offers quality levels from 0 to 11, and the choice between them is the central tuning decision. Higher levels search harder for matches and model context more thoroughly, which improves the ratio but raises CPU time steeply, while decompression speed stays roughly constant across levels.

Because of that asymmetry, the right level depends on whether content is compressed once or on every request. Ranges in the table are typical for text assets and vary with content, hardware and implementation.

Quality rangeRelative CPU costTypical use
0 to 3Very low, faster than default GzipRarely used for web responses
4 to 6Moderate, similar to Gzip level 6On-the-fly compression of dynamic pages
7 to 9HighBusy CDNs with spare CPU
10 to 11Very high, often tens of times slowerPrecompressed static assets at build time
Typical Brotli quality ranges

A common mistake is to enable level 11 for dynamic responses. On a loaded server this can add noticeable response time and consume CPU that PHP workers or application threads need, which reduces requests per second rather than improving it.

Brotli compared with Gzip and other encodings

Brotli and Gzip compression solve the same problem, and most servers offer both so that every client receives the best encoding it supports. The comparison below reflects typical behavior on text assets rather than fixed values.

EncodingTypical size versus GzipClient supportBest fit
GzipBaselineUniversalFallback and legacy clients
BrotliAbout 15 to 25 percent smallerAll modern browsers over HTTPSDefault for text assets
ZstandardClose to Brotli, faster to compressNewer browsers onlyDynamic content with high volume
No compressionLargestUniversalAlready compressed images and video
Brotli versus other HTTP content encodings

Browsers generally advertise Brotli only over HTTPS, because intermediary proxies on plain HTTP historically mishandled unknown encodings. Already compressed formats such as JPEG, PNG, WebP, MP4 and ZIP should not be compressed again, because the gain is negligible and the CPU cost is wasted.

Deploying Brotli on a web server

Brotli can be applied dynamically per request or precompressed ahead of time, and mature setups use both. Nginx supports it through the ngx_brotli module with directives such as brotli on and brotli_static on, Apache through mod_brotli, and most CDNs can compress at the edge without any origin change.

Correct header handling matters as much as the compression itself. The server must send `Vary: Accept-Encoding` so that caches, including a full-page cache or Varnish, store separate variants for Brotli, Gzip and uncompressed clients. Without that header a cache may serve a Brotli body to a client that cannot decode it.

Content typeRecommended setupReason
Versioned CSS and JavaScriptPrecompress at level 11, serve .br filesMaximum ratio, zero request-time CPU
Dynamic HTMLOn-the-fly at level 4 to 6Balanced latency and size
JSON API responsesOn-the-fly at level 4 to 5Short responses, low CPU budget
Responses under about 1 KBSkip compressionOverhead outweighs savings
Images and videoDo not compressAlready compressed formats
Choosing a Brotli deployment strategy

Limitations and common pitfalls

Brotli is a clear gain for most text-heavy sites, but its benefits depend on careful configuration. The most frequent problems come from excessive quality settings, cache misconfiguration and assumptions about where compression actually happens.

  • CPU saturation: high levels on dynamic content can cause resource saturation under traffic peaks.
  • Missing Vary header: caches may return the wrong encoding to some clients.
  • Double compression: an origin and a CDN both compressing wastes CPU and complicates debugging.
  • Security exposure: like other compressors, Brotli over TLS is subject to BREACH-style attacks when secrets and attacker-controlled input share one response.
  • Measuring the wrong thing: savings should be confirmed with real transfer sizes in browser developer tools, not assumed from benchmarks.

Stripped to the essentials

Brotli is a way of packing web pages more tightly before a server sends them. It works like vacuum-sealing clothes for a trip: the content is the same, but it takes up less room on the way and is unpacked by the browser when it arrives.

Smaller packages travel faster, so pages load sooner, especially on slow mobile connections. The main rule is to pack static files carefully in advance and to use a lighter, quicker setting for pages that are built fresh for each visitor.

Where it is applied

An online store serves a 900 KB JavaScript bundle and dynamic product pages from an Nginx server. With Gzip at level 6, the bundle travels as about 260 KB. The team adds a build step that precompresses every versioned asset with Brotli at quality 11, producing a file of about 205 KB, and enables brotli_static on so Nginx serves the .br file without spending CPU per request.

For HTML, the server compresses on the fly at quality 5, with Vary: Accept-Encoding set so the Varnish layer keeps separate variants. After rollout, measured transfer size for a typical first visit falls by roughly 20 percent, server CPU stays flat, and mobile users on slower networks see the main content render noticeably sooner.