Response compression is one of the simplest ways to reduce the amount of data an ASP.NET Core application sends over the network. Smaller responses mean less bandwidth, faster transfers, and lower network overhead.
Most ASP.NET Core applications already use compression through Brotli or Gzip. Zstandard, commonly called Zstd, provides another compression option that is designed to deliver a strong balance between compression speed and compression ratio.
The interesting part is not simply enabling Zstd. The real question is where it fits in an ASP.NET Core application, which clients can use it, and whether the additional compression benefit is worth introducing another encoding into the response pipeline.
This article explains how response compression works, where Zstandard fits, how to configure it when supported by your ASP.NET Core hosting stack, and what to consider before using it in production.
What Is Zstandard?
Zstandard is a lossless compression algorithm originally developed by Facebook and now maintained as an open-source project.
It is designed for high-speed compression and decompression while still providing useful compression ratios.
For HTTP responses, the basic idea is:
Without compression
ASP.NET Core
|
v
JSON response
|
v
Network
|
v
Client
With compression
ASP.NET Core
|
v
JSON response
|
v
Zstandard compression
|
v
Smaller response
|
v
Network
|
v
Client
|
v
Zstandard decompression
The client must understand Zstandard before the server can safely send a response using that encoding.
The HTTP Accept-Encoding header is used to negotiate this capability.
For example:
Accept-Encoding: zstd, br, gzip
The client is telling the server that it can handle Zstandard, Brotli, and Gzip.
Why Response Compression Matters
Consider an API returning a large JSON document:
{
"products": [
{
"id": 1001,
"name": "Laptop",
"description": "Long product description...",
"category": "Computers"
}
]
}
JSON contains repeated property names, structural characters, and text patterns. Compression algorithms can represent these repeated patterns much more efficiently.
For a large response, the difference can be significant.
| Response | Network payload |
|---|
| Uncompressed JSON | Large |
| Gzip-compressed JSON | Smaller |
| Brotli-compressed JSON | Often smaller |
| Zstandard-compressed JSON | Depends on content and settings |
The exact result depends heavily on the response data and compression level.
You should benchmark your actual payloads instead of assuming one algorithm will always produce the smallest response.
Compression Negotiation in HTTP
Response compression is not normally forced on every client.
The client advertises supported encodings:
Accept-Encoding: zstd, br, gzip
The server then chooses an encoding that it supports.
The response contains:
Content-Encoding: zstd
This tells the client how the response body was compressed.
A simplified request/response flow looks like this:
Client
|
| Accept-Encoding: zstd, br, gzip
v
ASP.NET Core
|
| Select supported encoding
v
Response compression
|
| Content-Encoding: zstd
v
Client
This negotiation is important because an application should never send compressed data using an encoding that the client cannot decode.
Where Zstandard Fits in ASP.NET Core
ASP.NET Core provides response compression middleware, but support for a particular compression algorithm depends on the framework and hosting stack being used.
The built-in response compression middleware commonly handles Brotli and Gzip.
For Zstandard, applications generally need a compatible compression provider or an infrastructure component that performs Zstd compression.
That distinction is important.
You should not assume that adding a package named for Zstandard automatically makes every ASP.NET Core response use it. The provider must integrate correctly with the ASP.NET Core response compression pipeline.
A typical architecture looks like this:
Client
|
| HTTP request
v
Reverse Proxy / Edge
|
| Optional Zstd compression
v
ASP.NET Core
|
| Application response
v
Proxy / Compression Layer
|
| Compressed response
v
Client
For many production systems, performing compression at the edge or reverse proxy is simpler than adding custom compression logic to the application.
Using a Zstandard Provider
When a compatible ASP.NET Core Zstandard provider is available for the target framework, the configuration generally follows the same response-compression pattern.
For example, the application can be structured around the standard response compression middleware:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
var app = builder.Build();
app.UseResponseCompression();
app.MapGet("/api/products", () =>
{
return Results.Ok(new
{
Items = Enumerable.Range(1, 1000)
.Select(id => new
{
Id = id,
Name = $"Product {id}",
Description = "Product description"
})
});
});
app.Run();
The important part is that the actual Zstandard provider must be registered according to the provider's API.
The application should not invent a custom Content-Encoding implementation simply by changing a response header.
If you are using a third-party Zstd provider, follow its supported registration and middleware integration model.
Why You Should Not Manually Set Content-Encoding
A common mistake is writing something like:
context.Response.Headers.ContentEncoding = "zstd";
and assuming the response has now been compressed.
It has not.
The header only tells the client that the body is compressed. It does not perform compression.
The correct flow is:
Response body
|
v
Compression provider
|
v
Compressed bytes
|
v
Content-Encoding: zstd
Both the body and header must agree.
Otherwise, clients attempting to decode the response can fail.
Which Responses Should Be Compressed?
Not every response needs compression.
Text-based formats are usually strong candidates:
- JSON
- XML
- HTML
- CSS
- JavaScript
- SVG
- Plain text
- Large API payloads
Already compressed formats are generally poor candidates:
- JPEG
- PNG
- WebP
- MP4
- ZIP
- GZIP archives
- PDF files that are already internally compressed
Compressing already compressed data often produces little benefit while consuming CPU.
A practical policy might look like this:
JSON -> Compress
HTML -> Compress
CSS -> Compress
JavaScript -> Compress
SVG -> Compress
JPEG -> Usually don't compress
PNG -> Usually don't compress
ZIP -> Don't compress
MP4 -> Don't compress
Compression Level Is a Trade-Off
Zstandard supports different compression levels.
Higher compression levels generally require more CPU but can reduce the response size further.
Lower levels generally prioritize speed.
That creates a basic trade-off:
Lower compression level
|
+-- Lower CPU usage
+-- Faster compression
+-- Larger response
Higher compression level
|
+-- Higher CPU usage
+-- More compression work
+-- Smaller response
For an API serving many requests per second, CPU cost matters.
A compression setting that saves another few percent of network traffic may not be worthwhile if it significantly increases CPU consumption.
The correct setting depends on payload size, traffic volume, CPU availability, and network conditions.
Compression and HTTPS
Compression should normally be evaluated together with HTTPS.
ASP.NET Core's response compression middleware can be configured to operate over HTTPS, but this should be an intentional decision.
For example:
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
Compression over HTTPS has security considerations when attackers can influence both request data and sensitive response data. The risk is particularly relevant to compression side-channel attacks.
Do not enable compression blindly for responses containing secrets that can be correlated with attacker-controlled input.
For ordinary public JSON responses, the risk profile is different, but security review is still appropriate for sensitive endpoints.
Zstandard vs Brotli vs Gzip
The choice should be based on real application requirements.
| Feature | Zstandard | Brotli | Gzip |
|---|
| Compression speed | Very good | Good | Very good |
| Decompression speed | Very good | Very good | Very good |
| Compression ratio | Very good | Very good | Good |
| Browser compatibility | Depends on client | Broad | Broad |
| API use cases | Strong candidate | Strong candidate | Strong candidate |
| Existing ASP.NET Core support | Depends on provider/stack | Built in | Built in |
For browser-facing applications, Brotli often remains the simpler choice because browser support is mature.
For controlled API environments where clients are known to support Zstandard, Zstd can be an attractive option.
Gzip remains useful as a compatibility fallback.
Supporting Multiple Encodings
A production API may support several compression algorithms.
For example:
Accept-Encoding: zstd, br, gzip
The server can select an encoding supported by both sides.
A client that only supports:
Accept-Encoding: gzip
can still receive a Gzip response.
This is one reason not to remove existing compression support simply because Zstandard is available.
A compatibility-oriented strategy is often better:
Modern client
|
+-- Zstd
Browser / modern HTTP client
|
+-- Brotli
Legacy or limited client
|
+-- Gzip
Unsupported client
|
+-- Uncompressed
Caching Compressed Responses
Compression changes the representation of a response.
If a response can be returned using different encodings, caches need to understand that the representation varies by Accept-Encoding.
The response should normally include:
Vary: Accept-Encoding
This prevents a cache from serving a Zstandard-compressed response to a client that only understands Gzip.
ASP.NET Core and the surrounding HTTP infrastructure should be configured correctly so that content negotiation and caching work together.
This becomes particularly important when a CDN or reverse proxy sits in front of the application.
Common Mistakes
Compressing Everything
Compressing every response wastes CPU on content that does not benefit from compression.
Ignoring Small Responses
Tiny responses may not benefit enough to justify compression overhead.
A small JSON response can sometimes be faster uncompressed, particularly on low-latency networks.
Supporting Zstd Without Checking Clients
The client must support Zstandard.
Do not send:
Content-Encoding: zstd
unless the client advertised support or another trusted protocol contract guarantees it.
Choosing Maximum Compression
Maximum compression is not automatically the best production setting.
Benchmark CPU usage and network savings using representative payloads.
Compressing at Multiple Layers
If both a reverse proxy and application middleware attempt to compress the same response, configuration errors can occur.
Decide where compression belongs and make that layer responsible for it.
Best Practices
Compress Large Text Responses
Focus first on large JSON, HTML, CSS, JavaScript, and similar text-based payloads.
Keep a Fallback Encoding
If your clients are not completely controlled, retain Brotli or Gzip support where appropriate.
Benchmark Real Payloads
Test actual API responses instead of synthetic strings.
Measure:
Original size
Compressed size
Compression CPU time
Decompression time
Requests per second
Network transfer time
Compress at the Edge When Appropriate
If a CDN, reverse proxy, or gateway already provides efficient response compression, using that layer may simplify the ASP.NET Core application.
Monitor CPU and Response Size Together
A smaller response is not automatically a faster application.
Monitor both network savings and CPU cost.
Advantages
- Reduces network payload size.
- Can improve transfer time for large responses.
- Provides a strong compression-speed balance.
- Useful for high-volume API traffic.
- Works well with content negotiation.
- Can complement existing Brotli and Gzip support.
Disadvantages
- Adds CPU work.
- Requires compatible client support.
- Adds another compression option to maintain.
- Can complicate caching when multiple representations exist.
- May provide little value for already compressed content.
- Requires careful security consideration for sensitive responses.
Troubleshooting Checklist
If Zstandard compression is not working as expected, check:
- Does the client send Accept-Encoding: zstd?
- Does the server actually have a Zstd compression provider configured?
- Is response compression middleware running at the correct point in the pipeline?
- Is the response content type configured for compression?
- Is another reverse proxy already compressing the response?
- Does the response contain Content-Encoding: zstd only when the body is actually compressed?
- Is caching configured to vary by Accept-Encoding?
- Are you measuring large responses where compression provides meaningful savings?
Network inspection tools are usually the fastest way to answer these questions.
Summary
Zstandard gives ASP.NET Core applications another option for reducing the size of HTTP responses. Its main value comes from combining efficient compression with fast decompression, making it interesting for high-volume APIs and controlled client environments.
The important architectural decision is not simply whether Zstd is faster than Gzip or Brotli. It is deciding where compression should happen, which clients need to support it, and whether the CPU cost is justified by the network savings.
For browser-facing applications, Brotli remains a practical default. For APIs with controlled clients and large text responses, Zstandard can be worth evaluating.
The safest approach is to keep compression based on content negotiation, avoid compressing already compressed data, configure caching correctly, and measure the result using real production-like payloads.
ASP.NET CoreZstandardresponse compressionHTTP compressionContent-EncodingAccept-EncodingBrotliGzip
Join the conversation! Your thoughts help the community grow.