ゆるテックノート

Compression and delivery (gzip/br/zstd)

A quick field guide for deciding when and how to compress HTTP responses. Aligned with RFC 9110 (8.4) for encoding negotiation.

Basics

Clients declare acceptable encodings via Accept-Encoding; servers respond with the chosen Content-Encoding. Vary: Accept-Encoding keeps caches honest.

Key headers

Header Role
Accept-Encoding What the client can accept (gzip, br, zstd, identity). q-values express preference.
Content-Encoding What the server actually used. Multiple encodings are listed in order applied.
Vary: Accept-Encoding Identifies Accept-Encoding as a selection input for caches. Content-Encoding alone does not make Vary mandatory in every response.

Algorithm traits

Ratios and processing costs depend on content, implementation and compression level. Measure transfer size, compression time and decompression time using representative data.

Selection criteria

Coding What to check
gzip Receiver support and the size/time trade-off at different levels.
brotli (br) Precompression is an option for static content. Measure size and generation time.
zstd Check both client and server support and benchmark the implementation and level.

Choosing

  • Select a coding accepted by the actual client. Brotli does not require HTTP/2: protocol version and content coding are separate choices. Check browser and delivery-system support.
  • For dynamic APIs, measure both bandwidth savings and added response latency. Level numbers are not a common performance scale across algorithms.
  • Static files: precompress br/gzip at build time and serve by extension mapping (e.g., via Nginx).

Operational notes

Get the headers and targets right to avoid user-facing breakage.

Checklist

  • Already compressed formats such as JPEG, PNG and ZIP often gain little from additional compression. Measure size and cost rather than deciding solely by whether a file is binary.
  • Normally send Vary: Accept-Encoding on cacheable responses selected using that request header, including the uncompressed variant. Use consistent selection rules for caches.
  • Range requests operate on the encoded bytes; large downloads may be better uncompressed.
  • Check CDN/TLS terminators for auto-recompression to avoid double encoding.
  • If you serve multiple encodings of the same resource, ensure the cache key includes the encoding.

Takeaway

Choose an accepted coding using measurements and label the actual content correctly. When selecting between encoded and unencoded variants, verify Vary and cache behavior.

Specifications and related guides

RFC 9110 Accept-Encoding / Vary / Brotli / Zstandard / HTTP caching