Boost Your .NET Core Web API Performance with Response Compression (Gzip & Brotli)

Response compression reduces the size of data sent from your API to clients, resulting in faster load times and lower bandwidth usage. By compressing responses, you improve overall application performance and create a better experience for users, especially over slower networks.

Why Use Compression in .NET Core Web API?
•    Your API might return large JSON responses or files.
•    Compressing responses reduces payload size.
•    Less data transfer means faster response times and less network load.
•    Clients (like browsers or mobile apps) support gzip decompression automatically.

There are several algorithms available for compressing API responses, each with its own strengths. Gzip and Brotli are two of the best.

What is Gzip Compression?
•    Gzip is a widely used method of compressing files (and data) to reduce their size.
•    When applied to HTTP responses, it reduces the size of data sent over the network.
•    This improves the performance by reducing bandwidth and load times for clients.

How to Enable Gzip Compression in .NET Core Web API
.NET Core has built-in middleware to support response compression, including gzip.
1.    Add the response compression middleware
In Startup.cs (or wherever you configure services and middleware):

builder.Services.AddResponseCompression(options =>
{
    options.EnableForHttps = true; // Enable compression for HTTPS requests as well
    options.Providers.Add<GzipCompressionProvider>();
});
// Optional: configure Gzip compression level
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
    options.Level = System.IO.Compression.CompressionLevel.Fastest;
});


var app = builder.Build();
app.UseResponseCompression(); 


What is Brotli Compression?
•    Brotli is a newer compression algorithm developed by Google.
•    It generally offers better compression ratios than gzip — meaning smaller output size.
•    Brotli is widely supported by modern browsers and HTTP clients.
•    It works especially well for text-based content like HTML, CSS, JavaScript, and JSON.
•    Brotli has different compression quality levels (from 0 to 11) — higher means better compression but more CPU usage.
•    Brotli is now often the preferred compression format over gzip when supported by the client.

Feature     Gzip     Brotli
Compression ratio   Good     Better (higher ratio)
CPU cost  Lower     Higher (more CPU intensive)
Browser support   Universal     Modern browsers
Use case Legacy support, fast compression Modern web apps, best compression

Enabling Compression with Custom MIME Types (Including Brotli & Gzip)
In your Startup.cs or wherever you configure services:

builder.Services.AddResponseCompression(options =>
{

    options.EnableForHttps = true; // Enable compression for HTTPS requests as well

    // Add Brotli and Gzip compression providers
    options.Providers.Add<BrotliCompressionProvider>();
    options.Providers.Add<GzipCompressionProvider>();

    options.MimeTypes = new[] //controls which content types will be compressed.
    {
            "application/json",
            "text/plain",
            "text/html",
            "application/javascript",
            "text/css",
            "application/xml",
            // Add more types if needed
        };
});

// Optional: configure Brotli compression level
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
    options.Level = System.IO.Compression.CompressionLevel.Optimal;
});

// Optional: configure Gzip compression level
builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
    options.Level = System.IO.Compression.CompressionLevel.Fastest;
});

//------------------------
var app = builder.Build();
app.UseResponseCompression();

 


👉The client must send the Accept-Encoding header indicating which compression algorithms it supports. For example:
•    Accept-Encoding: br — client supports Brotli
•    Accept-Encoding: gzip — client supports gzip
•    Accept-Encoding: br, gzip — client supports both, and server will usually pick the best supported (usually Brotli if it’s available)
If the client does not send the Accept-Encoding header or if it doesn't include supported values (br or gzip), the server will not compress the response and will send it uncompressed.