Improve ASP.NET Core API Performance with Response Compression
Introduction
Have you ever wondered how you can boost your API performance with just a few tweaks?
One way to enhance your API is by compressing the response to reduce its size. Making the response smaller usually makes an app respond faster, often by a lot.
Network bandwidth is often limited by hosting providers. Compressing the response reduces its size, saving bandwidth.
You are sending a large file to your friend in internet by compressing the file with any compression software.
When you need to use Response Compression?
You should first use web server-based response compression. Web servers such as IIS, Apache, or Nginx already having response compression support and you should prefer them at first. Their performance is better than ASP.NET response compression middleware.
Use response compression middleware only when you can't use server-based compression or when you're hosting directly on HTTP.sys or Kestrel server. HTTP.sys and Kestrel servers do not currently support built-in compression.
Setting Up Response Compression
Add the below code to enable response compression.
var builder = WebApplication.CreateBuilder(args);
//Add response compression
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
var app = builder.Build();
//enable response compression
app.UseResponseCompression();
app.MapGet("/", () => "Compressed response");
app.Run();
Https compression is disabled by default due to security issue, enable it using AddResponseCompression. Later call the UseResponseCompression() method to enable the response compression.
What will happen when you call AddResponseCompression?
When you call the AddResponseCompression, below compression providers are added by default.
BrotliCompressionProvider
GzipCompressionProvider
You can also create custom compression implementations with ICompressionProvider and use like below along with default compression providers.
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.Providers.Add<CustomCompressionProvider>();
});
Which compression providers will be selected by client?
Based on the above configuration, the client will select any one of the supported compressions with following order. Brotli will be used by default if clients supported.
Brotli
Gzip
Custom compression provider
How compression providers work in between client and server?
Clients will send supported compressions providers Accept-Encoding request headers. Server will send the response with supported compression providers in the Content-Encoding response headers.
Conclusion
Implementing response compression can significantly enhance the performance of your web applications by reducing the size of data transferred between the server and clients. By using providers like Brotli and Gzip, or even creating custom providers, you ensure that your application can serve content efficiently while maintaining flexibility and adaptability to client capabilities. Remember, the key is to configure your compression settings to balance performance gains with the needs of your users.
Reference:
Response Compression in .NET Core