API Response Compression in .Net Core

API Response Compression in .Net Core


If you are here, I am sure you must have used (consumed/ produced) APIs and indeed its been the case in modern web/ mobile application where APIs are used very frequently. 

Data transfer size in case of APIs is a very big concern as the volume of data which can be transferred via APIs are not really meant to have huge data as because of following reasons:
  • Data transfer time over network can be a big constraint if the size of data is huge
  • APIs response time are expected to be fast and quick and if the volume of the data is huge obviously the response time of the API will be impacted as well, and no client application consuming the APIs will expect the response time of an API to be beyond a few seconds (at max).
So how do we really solve this problem. Well we all understand that API is not really meant to perform huge data transfers but just in case we still have to do how to do it effectively. And there comes API compression frameworks at our rescue.

With API compression the data which is supposed to be transferred over the network is compressed/ zipped so as the data transfer size is smaller than the actual data size and hence which will as well reduce the transfer time over the network.

Indeed .Net Core 1.1 onwards where is an inbuilt middleware for the compression of our API responses.
The default framework used in this middleware is Gzip.

To enable the API compression, we will have to include a nuget package named "Microsoft.AspNetCore.ResponseCompression".

Now that we have installed and restored the nuget package, we can start the configuration in our API project. And the first step to do that is to create and extension of IServiceCollection as indicated in the screenprint below.


 

Post creation of the extension we have to invoke the service extension and to do that we have to add services.AddResponseCompressionProvier();  in the ConfigureServices method in Startup.cs like shown below.


Along with this we have to indicate MVC API to consider API response compression, which can be done by adding app.UseResponseCompression(); in Configure method of Startup.cs file. 


Now that we are all set to test our API response compression, we need an API with huge amount of data as response to test our compression logic. Now to do that I have just changed the regular boilerplate code provided by MVC API scaffolding as shown below to provide random 5000 weather forecasts.

 

So this how our API response was looking before implementing the API response compression. In this the size of the data was 501 KB.



Now lets see how our API response looks post implementation of the API response compression. IN this the size of the data had reduced to 47.3 KB.



Conclusion 

In this post we saw how API response compression technique can reduce the API response data size which can be very useful in case of the API response size is huge which helps the application performance in many ways. Hope you liked the post, do provide inputs in the comment, also if you think the article will be helpful for any of your friends and relatives then please do share the article with them.  


And don't forget to subscribe for the new post available so that you get notified on your mailbox.  

Happy Learning. 📚 

 

Comments

Popular posts from this blog

Disaster Recovery of Azure API Management (APIM)

What is Cloud Computing?

API Aggregation Using Azure API Management