Reduce Latency in HTTP calls using Compression with Node.js
HTTP payload compression algorithms - Edition #22
Hey, I'm Marco and welcome to my newsletter!
As a software engineer, I created this newsletter to share my first-hand knowledge of the development world. Each topic we will explore will provide valuable insights, with the goal of inspiring and helping all of you on your journey.
In this episode, I'll explain what HTTP response payload compression is, including the algorithms involved and how to implement support for them in a Node.js server.
You can download all the code shown directly from my Github repository: https://github.com/marcomoauro/magic-link-auth
👋 Introduction
Consider that you have an API needs to transmit a large JSON file over the Internet. Sending such a large file could take a long time to transfer and could consume a significant amount of bandwidth.
Compression techniques allow the API to reduce the size of the JSON file before sending it over the Internet. By compressing the file, the data is encoded more efficiently, resulting in a smaller packet. This compression process not only speeds up transmission times, but also reduces the bandwidth required for the transfer.
Therefore, when the API compresses the JSON file before sending it, it benefits in several ways. Firstly, it ensures that the data reaches its destination more quickly, improving overall efficiency. Secondly, it reduces the pressure on Internet resources, making the transmission lighter.
How to request compression from the server?
It is the client that decides to request compression and uses the Accept-Encoding header where it makes explicit all the compression formats it can handle.
the server receives the API call, processes it and before returning the response to the client, compresses it by choosing one of the algorithms made explicit by the client, if supported.
Finally it returns the compressed response to the client by adding the Content Encoding header, so the client knows how to decompress the response to process it correctly.
Pros vs cons
In addition to the benefits of lower bandwidth occupancy and consequent reduction in overall latency, compression introduces inefficiencies.
Pros include:
✅ Reduced file size: compression significantly reduces file size. This enables faster transfer of data over the network, especially in conditions of limited bandwidth such as mobile devices.
✅ Improved website performance: file compression helps to reduce the loading time of web pages. As file sizes are reduced, browsers can download content faster, improving the user experience and helping to reduce site abandonment rates.
✅ Reduced latency due to smaller payloads: Compression leads to smaller file sizes, resulting in reduced latency. Compressed files take less time to be transferred over the network because there is less data to transmit. As a result, HTTP requests can be processed faster, reducing the overall response time of the server.
Keep reading with a 7-day free trial
Subscribe to Implementing to keep reading this post and get 7 days of free access to the full post archives.