Stefan Holm Olsen

Compressing Web API and Episerver API responses

In some of my recent Episerver projects, I created several Web API endpoints that returned lots of data. In order to send and receive the response faster, and to save a lot of network traffic, I ended up applying heavy compression on all the Web API responses.

This blog post is about how to do exactly that.

Sending big responses

Some of the endpoints I worked on would sometimes return more than a few hundred kilobytes of JSON data. Sometimes much more.

The issue with sending this much data, is that:

  1. All data is first serialized to a buffer.
  2. This buffer is then transferred over the network.
  3. The client or a CDN retrieves the data to its own buffer.

Each step consumes time and memory. But by compressing the response data, at least the network transfer part can be made much faster.

I once created particular Web API endpoints that returned lots of data in uncompressed JSON format. Just by applying some heavy compression, the total load time went down from 10 to 2 seconds. This is how significant response compression can be.

Applying response compression

In order to seamlessly compress data between server and client, we can select and use an HTTP transfer encoding, as supported by the client. This way, the client code does not need to know about the compression or how to handle it. It all happens in the browser (or the app runtime) at the HTTP transfer layer.

Modern browsers support these four compression schemes:

  • Deflate
  • Gzip
  • Brotli

By default, the browser will announce to a server which compression schemes they support, in every single HTTP request. So, the client side is already in place.

The only thing we need to do is extending the server-side Web API pipeline to make it support compression. To do this, I have created a set of classes to add and reference (the source code is available here).

The nice thing is that it automatically works with both your own and Episerver’s Web APIs (like the Content API and the Service API).

Compression with CDN

If you use a CDN between your users and your web servers, remember to enable Brotli compression on your CDN. This will ensure that the data is transferred with the best compression method, all the way to the server and back.

In Cloudflare this is controlled by a single button. In Azure CDN, however, this requires a little more configuration.