HAMZA KAREEM / CONCEPTS hamzawi.dev

Lesson 1372 min read

Compression Levels (Brotli vs gzip)

Why Brotli 11 Can Make Your Site Slower

Web developmentweb development / performance

The myth

"Brotli beats gzip, so set brotli_comp_level 11 everywhere and you've shipped a free win."

Actually

Brotli's win is real — for files you compress ONCE. For a response generated per request, level 11 is a CPU bill you pay on every single hit, and the milliseconds spent compressing are milliseconds the user spends staring at nothing. Compression level is not a quality dial. It's a trade of CPU-now for bytes-later.

🧮 The arithmetic (120 KB JSON response, one core, rough orders of magnitude)

gzip -6   → 18 KB,   ~4 ms
br -5     → 17 KB,   ~8 ms
br -11    → 14 KB,  ~400 ms

On a 10 Mbps link, br 11's extra 3 KB saves ≈ 2.4 ms of transfer. It cost ~400 ms of TTFB. And that CPU is serialized against every other request on the box — at 200 req/s you haven't built a fast site, you've built a compression queue.

The rule that falls out of it

Ask: how many times is this byte-stream compressed vs served?

  • Compressed once, served a million times (JS, CSS, fonts) → spend everything. br 11, at build time.
  • Compressed per request (JSON, SSR HTML) → spend almost nothing. br 4–5.

nginx, done right

# dynamic responses: cheap level
brotli on;
brotli_comp_level 5;
brotli_types application/json text/html;

# static: serve the .br you already built
brotli_static on;

...and in your build:

brotli -q 11 -k dist/app.js    # emits app.js.br

Three more things people get wrong

Already-compressed bodies. JPEG, PNG, WebP, MP4, .zip, .woff2 are entropy-dense — you burn CPU for ~0% gain. Audit your gzip_types / brotli_types list; the default lists are often too greedy.

Tiny responses. Below ~1 KB the format overhead can make the payload BIGGER. Set gzip_min_length 1024.

The security footnote. If a response body mixes a secret (a CSRF token) with attacker-controlled input, compressed length leaks the secret byte-by-byte — that's BREACH. The modern fix is per-request token masking, not switching compression off globally. Plenty of teams still cargo-cult the 2013 advice.

(zstd is landing in CDNs and proxies now. Same rule, same knob — don't let the new acronym reset your thinking.)

Verify it on your own box

Without --compressed, curl reports the bytes actually on the wire:

curl -s -o /dev/null -D- \
  -H 'Accept-Encoding: br, gzip' \
  -w 'wire=%{size_download}B ttfb=%{time_starttransfer}s\n' \
  https://your.app/api/thing

Run it twice — once with the header, once without — and read content-encoding in the dumped headers to confirm what you actually got.

Your exercise

Pick one real dynamic JSON endpoint. Measure size + TTFB at br 11, br 5, and gzip 6 under ~50 concurrent requests (wrk, hey, or ab). Find the level where the bytes-saved curve flattens out.

Then do the same for your static JS bundle — and tell me whether the answer was the same number. If it was, one of your two measurements is lying.