In the midst of one of my conference talks, I presented a pivotal question to the attendees: What stands as the primary performance bottleneck for modern-day apps? While diverse answers emerge, the resounding reply points to the internet itself. This assertion isn’t merely theoretical; it’s a reality I’ve personally witnessed and can attest to.
In 2010, I spearheaded the development of the inaugural public API for Mitchell International in San Diego, California. As the architect and coder responsible for this API, ensuring optimal performance was an ongoing priority. Every morning, I would receive performance reports via email from a collaborating company, assessing API performance from various locations across the United States throughout the day.
One intriguing observation caught my attention: API requests from Florida (2,500 miles away) consistently outpaced those from Los Angeles (a mere 110 miles away). Lacking expertise in the intricacies of networking, I couldn’t comprehend the cause. However, I was determined to address what was within my control – server performance and the transmission of data payloads.
While numerous strategies abound to bolster performance, overlooking the optimization of data once it exits your API undermines the overall impact. One highly effective approach involves compressing the data payload, significantly enhancing the efficiency of data transmission.
Boosting Performance through Response Compression
An instrumental technique for enhancing performance revolves around the utilization of response compression. In ASP.NET, this feature isn’t inherently automatic, requiring specific implementation steps. I strongly endorse compression as a pivotal element for any web application, and it should ideally be seamlessly integrated into the default implementation of Startup.cs within Visual Studio.
1. Install NuGet Package
To begin, reference the following package in your project: Microsoft.AspNetCore.ResponseCompression
2. Modify Configure() in Startup.cs
Subsequently, incorporate app.UseResponseCompression() as depicted below:
public void Configure(IApplicationBuilder app,
IWebHostEnvironment env)
{
app.UseResponseCompression();
}
3. Modify ConfigureServices() in Startup.cs
Continuing with the implementation, include AddResponseCompression() in the ConfigureServices method, as illustrated below:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
// Replace 'YourCompressionProvider' with the
desired compression provider.
options.Providers.Add<YourCompressionProvider>();
options.EnableForHttps = true;
});
}
It’s important to note that besides GZip, there are alternative providers available for you to utilize. The GZip provider, in particular, offers configurable options that can be set as follows:
services.AddResponseCompression(options =>
{
// GZip provider
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
// Configurable options for GZip provider
options.MimeTypes = ResponseCompressionDefaults
.MimeTypes.Concat(new[] { "image/svg+xml" });
options.ExcludedMimeTypes = ResponseCompressionDefaults
.MimeTypes.Concat(new[] { "image/jpeg" });
options.Providers.Add<GzipCompressionProvider,
CustomCompressionProvider>();
});
The Client
To complete the setup, the client calling your API should include the following header in their requests:
Content-Encoding: gzip
The Stats

For the API I initially worked on when I wrote this, incorporating these few lines of code resulted in an impressive 84% compression of our largest payload! Consequently, our payload is now only 46,575 KB, a significant reduction from the original 423,411 KB. It’s essential to acknowledge that the time taken for compression and decompression of the payload will impact performance. The magnitude of this impact varies based on several factors, making it advisable to conduct benchmark tests from different locations to thoroughly assess overall performance.
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.
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
