The amount of transferred data matters. On one hand, it often contributes to the cost of running a service and on the other, a lot of clients doesn't have as fast of connections as we would like to believe. This is why response compression is one of key performance mechanisms in the web world.

There is a number of compression schemas (more or less popular) out there, so clients advertise the supported ones with Accept-Encoding header.

The above screenshot shows the result of a request from Chrome to the simplest possible ASP.NET Core application.

As we can see the browser has advertised four different options of compressing the response but none has been used. This shouldn't be a surprise as ASP.NET Core is modular by its nature and leaves it up to us to pick the features we want. In order for compression to be supported, we need to add a proper middleware.

Enabling Response Compression

The support for response compression in ASP.NET Core is available through ResponseCompressionMiddleware from Microsoft.AspNetCore.ResponseCompression package. After referencing the package all that needs to be done is registering middleware and related services.

One thing to remember is to set the Content-Type, as compression is enabled only for specific MIME types (there is also a separated setting for enabling compression over HTTPS). Additionally, I'm adding a

Vary: Accept-Encoding

header to the response so any cache along the way knows the response needs to be cached per compression type ( a future version of middleware will handle this for us).

The below screenshot shows the result of the same request we did in our previous example, after modifications.

Now the response has been compressed using gzip. Gzip compression is the only one supported by the middleware, which is "ok" in most cases as it has the widest support among clients. But the web world is constantly evolving and compression algorithms are no different. The latest-and-greatest seems to be Brotli, which can shrink data by an additional 20%-25%. It would be nice to use it in ASP.NET Core.

Extending Response Compression With Brotli

The ResponseCompressionMiddleware can be extended with additional compression algorithms by implementing the ICompressionProvider interface. The interface is pretty simple, it has two properties (providing information about encoding token and whether flushing is supported) and one method (which should create a stream with compression capabilities). The true challenge is the actual Brotli implementation. I've decided to use a .NET Core build of Brotli.NET. This is, in fact, a wrapper around the original implementation, so some cross-platform issues might appear and force recompilation. The wrapper exposes the original implementation through BrotliStream which makes it very easy to use in the context of the ICompressionProvider.

So IE and Edge don't support Brotli at all and Firefox supports it only over HTTPS. Checking more detailed information at caniuse, we will learn that a couple more browsers don't support Brotli (but Edge already has it in preview, although it is rumored that the final support will be only over HTTPS). The overall support is about 57% which means that we want to keep gzip around as well. In order to do this, it needs to be added to the ResponseCompressionOptions.Providers collection too (the moment we start manually registering providers the default one is gone).

If we test this code against various browsers we will see that the chosen compression always ends up being gzip. The reason for that is the way in which middleware chooses the provider. It takes the advertised compressions, sorts them by quality, if present, and chooses the first one for which a provider exists. As browsers generally don't provide any quality values (in other words, they will be equally happy to accept any of the supported ones) the gzip always wins because it is always first on the advertised list. Unfortunately, the middleware doesn't provide an option for defining a server-side preference for such cases. In order to work around this, I've decided to go about it in a hacky way. If the only way to control the provider selection is through quality values, they need to be adjusted before the response compression middleware kicks in. I've put together another middleware to do exactly that. The additional middleware would inspect the request Accept-Encoding header and, if there are no quality values provided, would adjust them.

Now the tests in different browsers will give different results. For example, in the case of Edge, the response will be compressed with gzip, but in the case of Chrome, it will be compressed with Brotli, which is the desired effect.