Lesson 22 of 30
Compression isn't a free performance win you flip on and forget — it's a real CPU-vs-bandwidth tradeoff that happens to be a great deal for text, and close to worthless for anything already compressed.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
Why do gzip and Brotli shrink HTML/CSS/JS by 70-90% but do almost nothing for an already-compressed JPEG?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Text-based formats — HTML, CSS, JavaScript, JSON — are highly repetitive: the same tags, keywords, whitespace patterns, and punctuation appear over and over. Compression algorithms exploit exactly that repetition. gzip is the long-established, near-universally-supported option; Brotli, newer and generally more efficient at the same or better compression ratio, is now supported by every major browser and most CDNs. Applied to text assets, either one routinely shrinks transfer size by somewhere in the range of 70-90%, which is a real, direct win for every metric that depends on bytes-over-the-wire.
That win doesn't extend to everything. Formats that are already compressed — JPEG, most video codecs, WOFF2 fonts, anything already gzipped — have already squeezed out their redundancy. Running gzip or Brotli over an already-compressed file yields little to no further size reduction, and in some cases makes the file slightly larger (compression algorithms add their own small overhead, which has nothing left to offset if there's no redundancy left to remove). Re-compressing these formats is wasted CPU work for no benefit.
Compression isn't free — it costs CPU time on both ends of the connection. The server has to spend cycles compressing the response (or serve an already-compressed version), and the client has to spend cycles decompressing it before it can use the content. For a busy server compressing every response from scratch, on every request, that's real, recurring CPU cost that competes with everything else the server is doing.
Per-request compression: request in -> compress on the fly -> send (repeated CPU cost, every request)
Build-time compression: build once -> compress once -> cache -> send (CPU cost paid exactly once)
The production pattern that avoids paying that cost repeatedly: compress once, at build or deploy time, and serve the pre-compressed file from cache on every subsequent request — the server does zero compression work per request, just returns bytes that were already prepared. This is specifically why Brotli's static pre-compression at maximum quality is the recommended production practice for any asset whose contents are known at build time (your JS bundles, CSS, most HTML): compressing at Brotli's highest quality setting is genuinely slow, too slow to do live on every request, but doing it once at build time is free at request time — the cost is paid during the build, not during traffic.
"Your API responses are large — would you compress them?" is a real question testing whether a candidate reasons about the tradeoff (text vs. already-compressed formats, build-time vs. request-time cost) instead of reflexively answering "just gzip everything," which is wrong for the half of a typical page's payload that's already-compressed images and fonts.