Gzip and Brotli compress HTTP response bodies so a website can send fewer bytes over the network. Both can work well for HTML, CSS, JavaScript, and other text-heavy resources. Brotli often produces smaller files, while gzip may need less processing time to compress a response. The difference depends on the content, encoder, and settings; a smaller download does not necessarily mean a faster page.

A website can offer both formats and choose an acceptable version for each client. Static assets can be compressed ahead of time, while changing responses may need compression during requests. Precompression moves the encoding work out of the request, making it practical to spend more time reducing file size. When compressing a response during a request, measure how long the server takes to start and finish sending it.

Start with Accept-Encoding Negotiation

A browser uses the Accept-Encoding request header to list acceptable content codings, such as br for Brotli and gzip for gzip. The server selects an acceptable coding according to its configuration and the request. It may also send an uncompressed representation when appropriate. Do not assume that a server will choose Brotli solely because it appears in the header; the server must have the relevant encoder or a precompressed copy available.

In the response, Content-Encoding: br means the body uses Brotli, while Content-Encoding: gzip means it uses gzip. The declared coding must match the transmitted bytes. Content-Type still describes the media type after decoding, such as HTML or CSS. Check the actual response headers and transferred size rather than inferring compression from the file extension or an optimization setting in an admin panel.

Keep Cache Variants Separate

Use Vary: Accept-Encoding when a cacheable response changes according to the request's Accept-Encoding header. This tells caches to take that header into account when choosing a stored response for the same URL. Without the appropriate variant handling, a cache could send Brotli data to a client that cannot decode it.

Check compression at the origin server, any reverse proxy, and the content delivery network (CDN). A CDN may receive one encoding from the origin and send another to the browser. Correct headers at the origin therefore do not prove that visitors receive the intended encoding.

If the server sends strong ETags, use a distinct value for each differently encoded representation. A strong ETag identifies specific representation bytes, so the compressed and uncompressed versions must not share the same strong tag. When a server returns 304 Not Modified, the client or cache reuses stored content rather than receiving a new body. Test these conditional requests with each supported encoding to confirm that the stored variant matches the request.

Compare Static and Dynamic Compression Costs

For a versioned CSS or JavaScript file, a build step can generate compressed gzip and Brotli copies once. The server or CDN can then select a matching copy for each request without repeating expensive compression work. This makes higher compression effort more practical for stable assets, although build time and storage are still costs. When the source asset changes, regenerate every served representation together.

A personalized page or frequently changing API response may be encoded during each request. Compression then uses CPU and can add latency. Higher effort settings may save bytes but cost more processing time; whether that tradeoff helps depends on payload size, traffic, and network conditions.

Consider whether the response should be compressed at all, especially if it is tiny or already compressed. Files in already compressed formats, such as JPEG, PNG, and ZIP, often gain little from another compression pass and can even grow. SVG images are a different case: they contain text and can benefit from gzip or Brotli.

Do not apply a blanket compression level to every response solely because one sample performed well. Compression libraries, server modules, hardware, and traffic mix differ. If compression runs at the CDN, measure the response received by the browser and check which compression work still happens at the origin. A reduction in origin CPU use does not by itself show that the page downloads faster.

Measure Representative Files and Requests

Choose a small set of real assets and dynamic responses: an HTML page, a CSS bundle, a JavaScript bundle, and a typical API response if relevant. Record the original size, gzip size, Brotli size, compression effort setting, and time or CPU needed to encode each. Use identical source content when comparing the two compressed versions, so a content change is not mistaken for a compression gain. Repeat across representative payload sizes because the extra bytes required by the compression format can outweigh savings on a tiny response.

In browser developer tools, distinguish the transferred size from the resource size after decompression. A response served from the browser's memory or disk cache does not show the cost of a fresh download. Disable the browser cache for that measurement, then test cached requests separately.

For static assets, compare the generated files and confirm that the chosen server configuration actually serves them. For dynamic responses, test concurrency and response latency under realistic load. Include an uncompressed baseline and a client that advertises only gzip. Record the environment and settings so a result can be repeated. Any percentage claimed for a website should come from those measurements, not from a generic benchmark or a different site's assets.

Apply the Results by Resource Type

If Brotli produces a smaller static bundle and the build time is acceptable, serve that precompressed version to clients that accept it. Keep a gzip version available for clients that accept gzip but not Brotli. For dynamic text, compare the download savings with the added encoding time under the expected load; the result may justify a lower compression level or a different algorithm.

Verify each variant's headers, transfer size, and cache behavior after deployment changes. Recheck when asset composition, CDN configuration, or traffic patterns change. If a new compression setting increases response latency under load, compare it with the previous setting before keeping the change.