skip to content

In ASP.NET Core 10's response compression, how are the Brotli and Gzip providers chosen and tuned, and how do you change the compressed content types?

level: middleimportance: should knowfreq 35%

answer

  1. highest q-value wins, then provider order
  2. Brotli listed before Gzip by default
  3. adding one provider drops the defaults
  4. CompressionLevel.Fastest by default
  5. MimeTypes replaces, so Concat the defaults

basics

~20 s

With no providers configured, Brotli then Gzip are offered; the encoding with the highest Accept-Encoding quality wins, ties going to the earlier provider. Levels come from BrotliCompressionProviderOptions and GzipCompressionProviderOptions (Fastest by default), and MimeTypes replaces the default type list.

solid answer

~40 s

In ASP.NET Core 10, an empty `Providers` collection means Brotli then Gzip. For each request the middleware builds candidates from `Accept-Encoding`, drops anything with `q=0`, and picks the highest quality; equal qualities go to the provider listed first, so `gzip, br` gets Brotli. Adding any provider with `options.Providers.Add<...>()` replaces the defaults, so add both if you want both. Levels are set with `Configure<BrotliCompressionProviderOptions>` and `Configure<GzipCompressionProviderOptions>`; both default to `CompressionLevel.Fastest`, because every response is compressed on the fly. `MimeTypes` replaces the defaults when you assign a non-empty list, so extend it with `ResponseCompressionDefaults.MimeTypes.Concat(...)`, and use `ExcludedMimeTypes` to carve types out. Version 10 ships only Brotli and Gzip providers; the Zstandard provider in the current docs is an ASP.NET Core 11 preview feature.

code

csharp · 15 lines
csharp
using System.IO.Compression;
using Microsoft.AspNetCore.ResponseCompression;

builder.Services.AddResponseCompression(options =>
{
    options.Providers.Add<BrotliCompressionProvider>();
    options.Providers.Add<GzipCompressionProvider>();
    options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
        ["application/problem+json", "text/csv"]);
});

builder.Services.Configure<BrotliCompressionProviderOptions>(o =>
    o.Level = CompressionLevel.Fastest);
builder.Services.Configure<GzipCompressionProviderOptions>(o =>
    o.Level = CompressionLevel.Optimal);

go deeper

for a junior

Recall that Brotli and Gzip are the built-in encodings and that the client's Accept-Encoding decides which one is used.

for a middle

Explain quality-then-order selection, the replace-the-defaults rule for providers and MIME types, and where the compression level is set.

for a senior

Tune level against CPU for large dynamic responses, keep both providers when customising, and extend the MIME list without dropping defaults.

for a principal

Decide whether dynamic compression in the app is worth its CPU compared with caching compressed output or compressing at the edge.

## The providers you get `ResponseCompressionOptions.Providers` is a `CompressionProviderCollection`. When it is **empty**, the middleware creates two providers itself, in this order: 1. `BrotliCompressionProvider` with encoding name `br`; 2. `GzipCompressionProvider` with encoding name `gzip`. As soon as you add **any** provider, the defaults are no longer created: the collection is used exactly as you filled it. Registering only a custom provider, or only Gzip, silently removes Brotli. ```csharp builder.Services.AddResponseCompression(options => { options.Providers.Add<BrotliCompressionProvider>(); options.Providers.Add<GzipCompressionProvider>(); }); ``` A custom provider implements `ICompressionProvider`: an `EncodingName`, a `SupportsFlush` flag, and `CreateStream(Stream outputStream)`. ## How one provider is picked per request The middleware parses `Accept-Encoding` into encodings with quality values and then: - skips any entry whose quality is zero (`gzip;q=0` means "never gzip"); - adds a candidate for every provider whose `EncodingName` matches an entry; - treats `*` as a match for every provider; - treats `identity` as a candidate with no provider, ranked last among equal qualities; - orders candidates by **quality, highest first**, then by **position in the provider list**. So a browser sending `gzip, deflate, br` (all quality 1) gets Brotli, because Brotli sits first in the default list, while a client sending `br;q=0.5, gzip` gets Gzip. If the winner is `identity` or nothing matches, the response goes out uncompressed. ## Tuning the level Both providers read their level from options classes: | Options class | Property | Default | |---|---|---| | `BrotliCompressionProviderOptions` | `Level` | `CompressionLevel.Fastest` | | `GzipCompressionProviderOptions` | `Level` | `CompressionLevel.Fastest` | ```csharp builder.Services.Configure<BrotliCompressionProviderOptions>(o => o.Level = CompressionLevel.Optimal); ``` The trade-off is CPU per response. This middleware compresses **dynamically**: the same catalogue page is compressed again for every request, nothing is cached. `Fastest` keeps latency and CPU low with a slightly larger output; `Optimal` or `SmallestSize` shrink the body further and cost more CPU on every call. For a wholesaler whose catalogue JSON is large but changes rarely, measure before raising the level: output caching or a CDN in front often saves more than a higher level. ## Choosing which content types are compressed - **`MimeTypes`** lists the eligible types. When it is empty the defaults apply: `text/plain`, `text/css`, `application/javascript`, `text/javascript`, `text/html`, `application/xml`, `text/xml`, `application/json`, `text/json` and `application/wasm`. Assigning a non-empty list **replaces** the defaults. - **`ExcludedMimeTypes`** is checked first, so it wins over `MimeTypes`. - Already-compressed formats (PNG, JPEG, ZIP) gain almost nothing from another pass and only cost CPU; leave them out. ```csharp options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat( ["application/problem+json", "image/svg+xml"]); ``` ## Seeing which provider won With the `Microsoft.AspNetCore.ResponseCompression` log category at `Debug`, each compressed response logs "The response will be compressed with 'br'." (or `gzip`), and a request whose encodings matched no provider logs "No matching response compression provider found." On the wire, the `Content-Encoding` response header names the provider that was used, and `Vary: Accept-Encoding` tells caches to keep one copy per encoding. ## Writing a custom provider A provider is a small class: `EncodingName` is the token matched against `Accept-Encoding`, `SupportsFlush` says whether the stream can flush partial output (both built-in providers return `true`, which matters for streamed responses), and `CreateStream` wraps the output stream. Register it with `options.Providers.Add<T>()` or `Providers.Add(typeof(T))`, and remember that doing so turns off the defaults, so add Brotli and Gzip back if clients still need them. A provider registered by type is created from DI, so it can take options through its constructor the way the built-in ones take `IOptions<BrotliCompressionProviderOptions>`. ## Version notes ASP.NET Core 10 ships the Brotli and Gzip providers only. The current response compression documentation also describes a Zstandard provider and a change to always emit `Vary: Accept-Encoding`; both are marked for ASP.NET Core 11, which is a release candidate, not a shipped release. ## What interviewers listen for - Quality first, provider order second, and why Brotli usually wins. - That adding one provider removes the defaults. - `Fastest` as the default and why: compression runs on every response. - `MimeTypes` replacing the defaults, and `ExcludedMimeTypes` taking precedence.

  • A team adds only a custom provider to Providers and Brotli traffic disappears. Why?
    The middleware creates the default Brotli and Gzip providers only when `Providers` is empty. Once anything is added, the collection is used as is, so Brotli and Gzip must be added explicitly with `Providers.Add<BrotliCompressionProvider>()` and `Providers.Add<GzipCompressionProvider>()` if they should stay.
  • Why does the middleware default to CompressionLevel.Fastest rather than Optimal?
    It compresses each response as it is written, with no caching of the result, so the level is paid in CPU on every request. `Fastest` keeps that cost and the added latency low at the price of a somewhat larger body. Raise it only after measuring CPU headroom, or compress once upstream instead.

  • Adding a Gzip provider keeps Brotli as well, because the defaults are always registered.
  • The middleware always prefers gzip, whatever the client's Accept-Encoding qualities say.
  • Setting MimeTypes to one extra type adds it on top of the defaults.
  • CompressionLevel.SmallestSize is free because the result is cached after the first request.
  • ASP.NET Core 10 ships a Zstandard response compression provider.

Assumes ASP.NET Core 10: the built-in providers are Brotli and Gzip; the Zstandard provider in the current docs belongs to ASP.NET Core 11, still a release candidate.