# Brotli

> The Brotli is a lossless compression algorithm developed for the web that compresses better than Gzip.

## What is the difference between Brotli and Gzip?

The lossless compression algorithm with a built-in dictionary of common web words. Gives smaller files than Gzip for text resources and is served with the 'br' value of the `Content-Encoding` header.

- Topic: Compression
- Type: technique

## Example

Wrong:

```bash
curl -sI https://example.com/app.js
```

Right:

```bash
curl -sI -H 'Accept-Encoding: br' https://example.com/app.js
```

`curl` sends no `Accept-Encoding` by default, so the server answers uncompressed; asking for 'br' shows if Brotli works.

## Common mistakes

- Compressing images and videos again, which wastes CPU and saves nothing.

## Key points

- The server picks the encoding from `Accept-Encoding` and sends a matching `Content-Encoding`.
- Responses include `Vary: Accept-Encoding` so caches keep separate copies.
- Static assets are precompressed at high levels at build time, dynamic responses use lower levels.

## Interview questions

### Does Brotli work over plain HTTP?

Browsers advertise 'br' only over HTTPS, so sites served over HTTP get Gzip or no compression.

### Which Brotli level should I use?

Use the maximum level 11 for files compressed ahead of time at build. For on-the-fly compression, middle levels like 4 to 6 balance speed and size.

### Should I precompress files with Brotli?

Yes, for static assets. Compressing at build time allows the slowest, smallest setting without costing CPU on each request.

## Related cards

- [Gzip](https://wonderium.dev/web/gzip.md)
- [Zstandard](https://wonderium.dev/web/zstd.md)
- [CDN](https://wonderium.dev/web/cdn.md)
- [HTTP Header](https://wonderium.dev/web/http-header.md)
- [Minification](https://wonderium.dev/engineering/minification.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/brotli
- Last updated: 2026-11-25
