INTERNET FUNDAMENTALSAI-REVIEWED

What is HTTP compression?

Useful first: HTTP · optional, not a requirement

Choose your depth

Stay with the essentials, or look closer.

Simple explanation
2 min read · estimate

YOUR GOALExplain why a compressed response can be smaller without removing part of the page.

Send fewer bytes, keep the content

Pack the text for the journey

A web server can encode a response using a lossless compression format, such as gzip or Brotli. The browser decodes it before using the content. Think of packing a blanket into a smaller bag: the package changes, but you still want the whole blanket when it comes out.

Imagine a text response that takes 100 KB before compression and 35 KB after compression. Those are invented sizes for this example, not a typical ratio or a measurement of this site. Decoding restores the original text; compression does not summarize the article.

[1] [3]

Smaller is useful, but measure the outcome

Saving transfer bytes can help, but compression and decompression require processing. A JPEG or ZIP file is already compressed, so another layer may save little or even make it larger. A server can choose an uncompressed response when that is acceptable to the client.

For example, a smaller text response and an unchanged photo may both be sensible results. Do not infer the whole page's loading time from one file's byte count.

[1] [2]
THE IDEA TO KEEP

Compression can reduce transfer size without shortening the content; the actual benefit depends on the response and the work involved.

ONE THING TO WATCH OUT FOR

HTTP compression makes a text page smaller by throwing away sentences.

Lossless compression changes how the data is represented. Decoding recovers the original content, including those sentences.

A QUICK CHECK, NOT A TEST
An example text response shrinks from 100 KB to 35 KB. What does that establish by itself?
Show the explanation

The encoded response uses fewer bytes, but total page-loading time still needs measurement. The size comparison concerns this response. Lossless decoding restores its text, and that one byte count does not measure the rest of the page or its total loading time.

Progress is saved only in this browser.

KEEP EXPLORINGHTTP
Was this explanation clear?
Report a mistake by email
Basic explanation
3 min read · estimate

YOUR GOALSeparate acceptable encodings, the chosen encoding and the underlying media type.

Read the request and response headers

The client offers choices

A request can include Accept-Encoding: gzip, br. Here br names Brotli. The header advertises acceptable response encodings; it is not an instruction to gzip every response. The server considers what it supports and whether applying compression is appropriate.

identity means no content encoding. An unencoded representation is normally acceptable unless the request explicitly excludes it. A listed encoding with q=0 is unacceptable.

[2] [3]

Type and encoding answer different questions

For a fictional HTML response, these headers describe different properties:

Content-Type: text/html; charset=utf-8
Content-Encoding: gzip

Content-Type identifies the media type before content encoding: the decoded content is HTML. Content-Encoding tells the recipient which decoding is needed. If Content-Length is present for this encoded response, it describes the encoded bytes, not the decoded HTML length.

Read the HTTP request and response guide for the wider exchange. These are selected headers, not a complete response with its body.

[3] [4]

A compressed file is a separate case

A ZIP download already has compression built into its file format. That alone does not mean its HTTP response should declare Content-Encoding: gzip. That header would describe an additional applied content coding, not merely the fact that the file is an archive.

When debugging, inspect the actual response header rather than guessing the encoding from a filename. An HTML page can be gzip-encoded while keeping its ordinary .html address.

[3] [4]
THE IDEA TO KEEP

Accept-Encoding offers choices, Content-Encoding describes applied coding, and Content-Type identifies the decoded media.

ONE THING TO WATCH OUT FOR

Accept-Encoding: gzip guarantees a gzip response.

It declares that gzip is acceptable. An acceptable unencoded response can still be returned; Content-Encoding describes what was actually applied.

A QUICK CHECK, NOT A TEST
A request includes Accept-Encoding: gzip and does not exclude identity. Must the server send a gzip-encoded response?
Show the explanation

No; an acceptable unencoded response is still possible. Accept-Encoding expresses acceptable choices. With identity still acceptable, the server can send an unencoded response; the filename does not negotiate content encoding.

Progress is saved only in this browser.

KEEP EXPLORINGHTTP
Was this explanation clear?
Report a mistake by email
Advanced explanation
3 min read · estimate

YOUR GOALDiagnose cache selection and validator mistakes when one URL can return different encodings.

Keep compressed cache variants consistent

Make the selecting request field visible

Suppose /guide.html can return gzip or an unencoded representation depending on Accept-Encoding. Return Vary: Accept-Encoding consistently, including on the default response and relevant 304 responses. This tells caches which request field affected selection.

A stored gzip response from a different accepting request must not be reused without revalidation when the selecting fields do not match. For example, a later request with Accept-Encoding: identity, gzip;q=0 must not simply receive the old gzip body. Vary does not set a freshness lifetime or independently grant permission to cache; those are separate HTTP caching decisions.

[5] [8]

Strong ETags identify the encoded variant

The same decoded HTML can have different representation bytes when encoded. Its gzip and unencoded representations therefore need distinct strong ETags, such as "guide-r4-gzip" and "guide-r4-plain". These example labels are opaque identifiers, not a prescribed naming scheme.

Using one strong ETag for both merely because their decoded text matches is incorrect. A weak tag, marked W/, has different comparison semantics and is not a substitute for byte-level identity. ETags also do not replace Vary.

[6] [7]

Validate the representation that can be used

When a stored response needs validation, a client can send its ETag in If-None-Match. A matching selected representation can be confirmed with 304 Not Modified instead of resending its body. Keep the negotiation and variant metadata consistent through this exchange.

For a practical check, compare an accepting request and an identity-only request: inspect Content-Encoding, Content-Type, Vary and the validator, then verify the decoded content. If a CDN also serves the response, inspect the final response a reader receives. A correct origin header alone is not a measurement of the final delivery path.

[5] [6] [8]
THE IDEA TO KEEP

Treat encoding selection, Vary matching, freshness and validators as separate parts of correct cache behavior.

ONE THING TO WATCH OUT FOR

If two variants decode to the same HTML, they can share the same strong ETag.

Strong validators distinguish representation bytes. Content-encoded and unencoded variants need different strong ETags even when their decoded content matches.

A QUICK CHECK, NOT A TEST
A URL offers gzip and unencoded HTML with identical decoded text. Which validator setup is correct when using strong ETags?
Show the explanation

Use distinct strong ETags for the two representations and preserve the relevant Vary policy. The encoded representation bytes differ. Distinct strong ETags avoid confusing those variants, while Vary still tells caches which request fields affect response selection.

Progress is saved only in this browser.

KEEP EXPLORINGHTTP
Was this explanation clear?
Report a mistake by email
GO STRAIGHT TO THE SOURCE

Keep asking. Check the details.

Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.

No human review is claimed. AI review can miss errors; check the original sources below.

  1. MDN — Compression in HTTP
  2. MDN — Accept-Encoding header
  3. MDN — Content-Encoding header
  4. MDN — Content-Type header
  5. MDN — Vary header
  6. MDN — ETag header
  7. RFC 9110 — Content-negotiated entity tags
  8. RFC 9111 — Cache keys and Vary
How explanations are prepared