Skip to content

brotli

currentMigrated from docs/data/brotli.mdx at 37c3317fd058; modified for Encode Wiki · CC BY-SA 4.0

Brotli is a compression format from Google (RFC 7932) designed for the web. It combines LZ77, Huffman coding and a built-in dictionary of common words and markup from HTML, CSS, JavaScript and natural language. That dictionary is why it beats gzip by a clear margin on small text assets. It is also the format inside WOFF2 fonts and the br HTTP content coding. As a general archive compressor it is unusual; use zstd or xz for that.

Terminal window
brotli -q 11 file # file.br, maximum quality (the CLI default)
brotli -q 5 file # fast, good for on-the-fly use
brotli -d file.br # decompress
brotli -t file.br # test
brotli -c file > file.br # to stdout

Unlike gzip and xz, brotli keeps the input (-j/--rm deletes it). Quality runs 0–11, and the default of 11 is very slow (many times slower than zstd or gzip at similar size gains), which is fine for static files compressed once but too slow for dynamic responses. For responses compressed per request, servers use qualities 4–5.

OptionEffect
-q NQuality 0–11
-w NWindow size 2^N − 16 bytes, N from 10 to 24; the CLI uses 24 (16 MiB), shrunk to fit smaller files
--large_window=NNon-standard window up to 2^30; only decodable by decoders that support it
-D fileCustom raw dictionary (both sides must have it)

Browsers send Accept-Encoding: br over HTTPS, so serve precompressed files:

Terminal window
brotli -q 11 app.js app.css index.html # then serve app.js.br with Content-Encoding: br

nginx (ngx_brotli), Apache (mod_brotli) and CDNs such as Cloudflare can compress responses on the fly at a low quality; Caddy serves precompressed .br files with file_server { precompressed br }. Precompress static assets at 11 and let the server handle dynamic responses at 4–5. Brotli does not help already-compressed data (images, video, WOFF2 itself). Serving Content-Encoding: br to browsers that did not ask for it breaks the page; keep a gzip fallback.

  • Not an archive format. There is no directory support, and tools such as GNU tar do not auto-detect it: use tar -I brotli -cf dir.tar.br dir/ and tar -I brotli -xf dir.tar.br.
  • Large windows (--large_window) are not RFC 7932 and are not valid for HTTP.
  • Decompression is fast (comparable to gzip), so the cost is entirely at compression time.