MGMLaboratory
Back

Menu

01Home
02About Us
03Projects
04Publications
05Member
06Articles
07Contact

Let's Talk

Open in Mail app

Socials

Malang (ID) --:--Privacy PolicyTerms of Service

December 13, 20232 min read

When compressing your JSON API actually pays offWhen compressing your JSON API actually pays off

A study measured how much bandwidth application-level Zlib and Base64 save, and the payload size where the savings stop.

  • Journal
  • Publications

Written by

  • SRShirasaka RenIT & Infrastructure · Student Member

In this article

  1. 01Where the compression lives
  2. 02What the measurements showed

REST APIs usually ship JSON over HTTP with no compression at all. For a desktop client on a wired connection that is fine. For a mobile app running on a metered data plan, every extra byte carries a price.

A 2023 study in the Journal of Information Technology and Computer Science put numbers on that tradeoff. The team built a PHP server and a browser client, then measured 1500 different JSON payload sizes to find out what a Zlib plus Base64 pipeline really buys.

JSON has become the default payload format for REST APIs. Diagram: Diego Mariano, CC BY-SA 4.0, via Wikimedia Commons.
Fig. 01JSON has become the default payload format for REST APIs. Diagram: Diego Mariano, CC BY-SA 4.0, via Wikimedia Commons.

01/02

Where the compression lives

The design choice that matters is placement. Instead of leaning on the web server Accept-Encoding and gzip layer, the team compressed at the application level. The server runs Zlib through the PHP gzencode function, then Base64 encodes the binary result so the payload stays string safe. The client reverses both steps, decompressing with the Pako library in JavaScript.

That approach keeps working even when the web server cannot be reconfigured for compression. It also lets the API decide, payload by payload, whether compressing is worth the effort.

A short JSON response. Payloads this small are where compression loses rather than wins. Image: Dcbma, CC BY 4.0.
Fig. 02A short JSON response. Payloads this small are where compression loses rather than wins. Image: Dcbma, CC BY 4.0.

02/02

What the measurements showed

Large payloads compress well. For JSON between roughly 12.5 and 101 kilobytes, the compressed and encoded output settled at about 33 percent of the original size. That is a bandwidth saving of up to 66 percent, which is the headline claim the authors set out to test.

  • Server CPU cost rose from 0.064 milliseconds on a 68 byte payload to 3.269 milliseconds on a 101 kilobyte payload.
  • Memory overhead stayed under 2.62 percent, which the authors describe as negligible.

Small payloads are where intuition breaks. A 68 byte response grew to 112 bytes after compression and encoding. A 135 byte response grew to 144 bytes. Base64 expands the binary that Zlib produces, and below roughly 150 bytes that expansion costs more than the compression saves.

A Mann-Whitney U test on memory usage returned a p value of 0.0000022, so the overhead is statistically real but minor. The practical rule fits in a sentence: compress the big responses, leave the tiny ones alone.

Source

  1. Pinandito, A., Kharisma, A.P., Jonemaro, E.M.A. (2023). Architectural Design of Representational State Transfer Application Programming Interface with Application-Level Base64-Encoding and Zlib Data Compression. Journal of Information Technology and Computer Science.

    DOI10.25126/jitecs.202383619 (opens in a new tab)

Next articleThe 150 byte rule for compressing API responses(Keep scrolling)(Read it next)(Loading)