Setting Up ASP.NET Core: Response Compression

In one of my conference talks, I ask the attendees this question:

What is the #1 performance killer for modern day apps?

I get lots of answers, but the actual answer is the internet! I’m not just saying this, I know this to be true. Over 10 years ago, I wrote the first public API for Mitchell International here in San Diego, California. Since I architected and coded this API, I was always concerned about performance.

Every morning, I would receive an email from a company we used to hit and record the performance of the API from different locations in the United States. One thing I found out, which was puzzling, was that API requests from Florida (2,500 miles away) were faster than requests from Los Angeles (110 miles away). I’m not a network expert, so I didn’t know how I could fix that. What I can fix is the performance on the server and how I send data payloads.

There are many things you should look at to improve performance, but if you don’t fix the performance of the data once it leaves your API, it won’t make much of a difference. One of those ways is to compress the data payload.

Response Compression

One way to improve performance is to use response compression. In ASP.NET Core, this does not seem to be automatic so there are a few things you need to implement. I believe compression is very important to any web app and this should be part of the default implementation (in Visual Studio) of Startup.cs.

1. Install NuGet Package

First, reference this package in your project: Microsoft.AspNetCore.ResponseCompression

2. Modify Configure() in Startup.cs

Next, add app.UseResponseCompression() as shown below.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseResponseCompression();
}

3. Modify ConfigureServices() in Startup.cs

Then, add AddResponseCompression() as sown below.

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCompression(options =>
    {
        options.Providers.Add();
        options.EnableForHttps = true;
    });
}

There are other providers than Gzip that you can use too. The Gzip provider does have options that can be set like this:

services.Configure(options => 
{
    options.Level = CompressionLevel.Fastest;
});

The Client

The last piece of this is that the client calling your API will need to add the following header to their calls.

Content-Encoding: gzip

The Stats

black and white business chart computer
With the current API that I am working on, just adding those few lines of code compressed our biggest payload by 84%! Wow! That means our payload is just 46,575 KB instead of the original 423,411 KB. The time it takes to compress and decompress the payload will add time to the performance. How much? Well, that really depends on a lot of factors, so make sure you perform benchmark tests, from different locations! As soon as I can do that for the API I am currently working on, I will update this post.

Summary

I plan to write more of these types of posts for ASP.NET Core so check back here often. How do you improve the performance of your web API’s and apps? Please make a comment below.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.