-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
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
- Chrome: Planned in version 118
- Safari: Not planned but with positive position
- Mozilla: Not planned with defer position
- Curl: Released since version 7.72.0
Features request
Request compression
- Support
zstdas the token from theContent-Encodingheader in the request. - Decompress with Zstandard the request content.
Response compression
- Support
zstdas a token from theAccept-Encodingheader in the request. - Compress with Zstandard when the
zstdtoken is elected as response compression, and so stored in theContent-Encodingheader in the response.
Zstandard implementation
- Native: https://github.com/facebook/zstd
- .NET Runtime: [API Proposal]: Add support for Zstandard to System.IO.Compression runtime#59591
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();