Optimizing PHP Application: Efficient JSON Compression for Enhanced Performance

J. M. Rodrigues
2 min readJul 24, 2023

--

As web applications grow, handling large datasets efficiently becomes crucial to maintain optimal performance. In this article, I’ll discuss the challenge I faced with a table storing large JSON datasets for selects in my PHP application and how I devised a solution for better storage and data transfer by compressing the JSON data and storing it in the database as base64-encoded strings.

JSON Compression Optimization

Before the optimization, JSON datasets occupied significant storage space and caused slow data transfer, hampering the application’s performance. To tackle this, I decided to compress the JSON data. Leveraging PHP’s built-in gzcompress() function, I compressed the JSON string, significantly reducing its size.

$compressed_json = gzcompress($my_json), 9);

gzcompress() is a built-in PHP function that enables data compression using the Zlib library. It takes a string as input and compresses it using the DEFLATE compression algorithm. This algorithm effectively reduces the size of the input string, making it ideal for minimizing data storage requirements and optimizing data transfer over networks. The function allows developers to specify an optional compression level ranging from 0 (no compression) to 9 (maximum compression). Higher compression levels result in smaller output at the cost of increased processing time.

To effectively store the compressed data in the database, I couldn’t directly save it as-is due to database limitations. Instead, I encoded the compressed data into base64 format, as it only contains utf-8 characters. This clever approach allowed me to store the compressed data efficiently in the database without any data loss.

$encoded_compression = base64_encode($compressed_json),

When it comes to retrieving the data, I needed to reverse the process to get the original JSON format. To achieve this, It's pretty much decoding the base64 string with base64_decode()and then decompress it using the gzuncompress() function.

$decoded_compression = base64_decode($encoded_compression);
$uncompressed_json = gzuncompress($decoded_compression);

The results of this optimization were considerable. On average, the JSON data size reduced from 110 KBs to around 16 KBs. This reduction in data size not only saved substantial database storage space but also improved the time it took for the API to answer calls for this specific endpoint.

--

--

J. M. Rodrigues

A fullstack software developer who loves problem solving.