← Lessons

quiz vs the machine

Gold1340

Networking

Gzip and Brotli Compression

How servers shrink text responses and how clients ask for it.

4 min read · core · beat Gold to climb

Shrinking the payload

Text such as html, css, and json compresses well because it is repetitive. The client advertises support with Accept Encoding, the server compresses the body, and it answers with Content Encoding naming the algorithm used. The two most common are gzip and brotli.

Gzip versus brotli

  • Gzip is universally supported and fast to compress, a safe default.
  • Brotli usually produces smaller files at the same speed level and ships a built in dictionary tuned for web text.
  • Brotli at its highest level is slow to compress, so it suits static assets compressed ahead of time more than dynamic responses.

What not to compress

Already compressed formats like images and video gain nothing and waste CPU. Compressing tiny bodies can even grow them due to overhead. Mixing compression with secrets in one stream also created the old BREACH style risk, so sensitive tokens deserve care.

A server should also send Vary Accept Encoding so caches keep compressed and plain variants apart.

Key idea

Clients request compression with Accept Encoding and servers reply with Content Encoding, where gzip is the safe default and brotli often wins on size for web text.

Check yourself

Answer to earn rating on the learn ladder.

1. Which header does a client use to advertise compression support?

2. Why is brotli at maximum level often best for static assets?