Isaac.

Using Response Compression in ASP.NET Core

Learn how to implement response compression to improve application performance and reduce bandwidth usage.

By EMEPublished: February 20, 2025
aspnetcompressionperformancebandwidthgzipbrotli

A Simple Analogy

Imagine shipping a package. If you send it as-is, it takes up lots of space and costs more. But if you vacuum-seal it first, it's smaller and cheaper to ship. Response compression is like that vacuum seal—it squeezes data down before sending it to users, making everything faster and using less bandwidth.


What Is Response Compression?

Response compression is a technique where the server compresses responses (HTML, CSS, JSON, etc.) before sending them to the client. The client automatically decompresses them. Common algorithms are Gzip and Brotli.


Why Use Response Compression?

  • Faster delivery: Smaller files download quicker
  • Less bandwidth: Reduce data transfer costs
  • Better UX: Pages load faster, users are happier
  • Easy to implement: Built-in ASP.NET Core middleware
  • Transparent: Works automatically with no client changes

How Compression Works

  1. Client sends request with Accept-Encoding header: gzip, deflate, br
  2. Server checks which encodings client supports
  3. Server compresses response using best available algorithm
  4. Server sends compressed response with Content-Encoding header
  5. Browser automatically decompresses and displays

Enable Compression in ASP.NET Core

Quick Setup

// Program.cs
builder.Services.AddResponseCompression();

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

app.MapGet("/", () => "Hello World");
app.Run();

Configure Compression Options

builder.Services.AddResponseCompression(options =>
{
    // Add custom MIME types to compress
    options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
        new[] { "application/json", "text/plain" });

    // Which providers to use (Gzip, Brotli)
    options.Providers.Add<BrotliCompressionProvider>();
    options.Providers.Add<GzipCompressionProvider>();
});

// Set compression levels
builder.Services.Configure<BrotliCompressionProviderOptions>(options =>
{
    options.Level = CompressionLevel.Optimal;
});

builder.Services.Configure<GzipCompressionProviderOptions>(options =>
{
    options.Level = CompressionLevel.Optimal;
});

Compression Algorithms

Gzip

  • Support: Nearly universal (all browsers)
  • Compression ratio: Good (~60-80% reduction)
  • Speed: Fast
  • Use: Default choice for compatibility

Brotli (br)

  • Support: Modern browsers (IE 11 not supported)
  • Compression ratio: Better (~80-90% reduction)
  • Speed: Slower (better for static content)
  • Use: Best compression, modern applications

Deflate

  • Support: Legacy
  • Compression ratio: Fair
  • Use: Rarely used now

What Gets Compressed?

By default, only certain MIME types are compressed:

// Default MIME types
"text/plain"
"text/css"
"application/javascript"
"text/html"
"application/xml"
"text/xml"
"application/json"

Practical Examples

E-commerce Site

Before compression:

  • Product list JSON: 500 KB
  • Compressed with Brotli: ~50 KB
  • Saves 450 KB per request

API Response

[HttpGet("/api/products")]
public IActionResult GetProducts()
{
    var products = _db.Products.ToList();
    return Ok(products); // Automatically compressed
}

Large HTML Page

  • Before: 2 MB HTML page
  • After: 250 KB with Brotli
  • Load time: 2s → 0.2s (on 10 Mbps connection)

Real-World Use Cases

  • Web APIs: Compress JSON responses for mobile apps
  • SPAs: Gzip JavaScript bundles for faster load
  • Static sites: Reduce bandwidth costs significantly
  • Global apps: Critical for users on slow connections
  • SaaS dashboards: Speed up data-heavy analytics pages

Best Practices

  • Always enable compression in production
  • Use Brotli for static content (pre-compress files)
  • Use Gzip as fallback for older browsers
  • Don't compress already-compressed formats (images, videos, PDFs)
  • Monitor compression overhead (minimal for modern servers)
  • Test with real browsers and networks
  • Measure performance improvements with tools like WebPageTest

Performance Impact

Typical compression metrics:

| Content Type | Size Before | Size After (Gzip) | Savings | |--------------|-------------|-------------------|---------| | HTML | 100 KB | 15 KB | 85% | | CSS | 50 KB | 8 KB | 84% | | JavaScript | 200 KB | 50 KB | 75% | | JSON | 100 KB | 10 KB | 90% |


Related Concepts to Explore

  • HTTP caching headers (Cache-Control, ETag)
  • Static file compression (pre-compressed assets)
  • Content Delivery Networks (CDNs)
  • Minification (separate from compression)
  • Image optimization
  • HTTP/2 multiplexing
  • HTTPS and its interaction with compression
  • Brotli compression levels and tuning
  • Response streaming for large payloads
  • Performance profiling and metrics

Summary

Response compression is one of the easiest performance wins available. By enabling Gzip or Brotli in your ASP.NET Core app, you can reduce bandwidth by 75-90% with minimal effort, making your application faster for users worldwide—especially those on slower connections.