Skip to content

Support zstd Content-Encoding #50643

@manandre

Description

@manandre

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

No response

Describe the solution you'd like

Zstandard, or zstd, is a data compression mechanism described in RFC8878. It is a fast lossless compression algorithm, targeting real-time compression scenarios at zlib-level and better compression ratios. The "zstd" token was added as an IANA-registered Content-Encoding token as per https://datatracker.ietf.org/doc/html/rfc8878#name-content-encoding.

The Facebook/Zstd team has published some of their research on compression level vs. CPU vs. compression ratio.

Current support

Features request

Request compression

  • Support zstd as the token from the Content-Encoding header in the request.
  • Decompress with Zstandard the request content.

Response compression

  • Support zstd as a token from the Accept-Encoding header in the request.
  • Compress with Zstandard when the zstd token is elected as response compression, and so stored in the Content-Encoding header in the response.

Zstandard implementation

API Proposal: Add Zstandard (zstd) Response Compression Support

Background and Motivation

ASP.NET Core currently supports Gzip and Brotli compression for HTTP request and responses through the Request Decompression and Response Compression middlewares.
Zstandard (zstd) is a modern, high-performance compression algorithm that offers:

  • Better compression ratios than gzip at similar speeds
  • Faster decompression than both gzip and brotli
  • Wide industry adoption (supported by major browsers and CDNs)
  • RFC 8878 standardization

With the recent addition of System.IO.Compression.ZstandardStream to the .NET runtime (available in .NET 11 Preview 1), ASP.NET Core can now natively support Zstd compression without external dependencies.

Adding Zstd support would provide developers with a modern compression option that balances compression ratio and performance.

Response compression

Proposed API

namespace Microsoft.AspNetCore.ResponseCompression;

public class ZstdCompressionProvider : ICompressionProvider
{
    public ZstdCompressionProvider(IOptions<ZstdCompressionProviderOptions> options);
    public string EncodingName { get; }
    public bool SupportsFlush { get; }
    public Stream CreateStream(Stream outputStream);
}

public class ZstdCompressionProviderOptions : IOptions<ZstdCompressionProviderOptions>
{
    public CompressionLevel Level { get; set; }
    ZstdCompressionProviderOptions IOptions<ZstdCompressionProviderOptions>.Value { get; }
}

Usage Examples

Basic Usage

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddResponseCompression(options =>
{
    options.Providers.Add<ZstdCompressionProvider>();
});

var app = builder.Build();

app.UseResponseCompression();

app.Run();

Custom Compression Level

builder.Services.AddResponseCompression(options =>
{
    options.Providers.Add<ZstdCompressionProvider>();
});

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

Default Configuration

Zstd becomes the default first compression provider in the priority order: zstd → brotli → gzip

This means clients that support zstd (via Accept-Encoding: zstd) will automatically receive zstd-compressed responses without any additional configuration.
This also means clients that support multiple encodings (via Accept-Encoding: gzip, br, zstd) will automatically receive zstd-compressed responses by default.

Request decompression

zstd is added as a supported Content-Encoding value.

Usage examples

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRequestDecompression();

var app = builder.Build();

app.UseRequestDecompression();

app.MapPost("/", (HttpRequest request) => Results.Stream(request.Body));

app.Run();

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions