loading

How Gzip Compression Saves Web Traffic

For your browser to fully render a webpage, it has to download a bunch of files. This particular blog post required 23 files, totaling 5.8MB uncompressed. When the server sent it using gzip, the actual transfer size was 2.5MB, meaning you saved 3.2MB. At first glance, that’s a huge win—over half the size reduction! But when I broke down that 3.2MB saving by file type, I found that 3.0MB, or 93%, came from just three PNG images.

The text content, all combined, only added up to 227KB. Most of what gzip was "doing" on this site was actually compressing images that were never properly compressed to begin with. The reason the compression seemed effective wasn't because the algorithm was great, but because those three PNG files were essentially saved without any real compression. I tested this by downloading all 23 files twice—once with and once without enabling the Accept-Encoding header—and compared the sizes and headers. I even checked the server settings and the 370 PNG images in the upload folder.

Why One Blog Post Requires 23 Files

I measured this using the URL of a post that detailed SQL queries, which you can find here: SQL Query Post. I pulled the files using curl from a different server, comparing the size of each file downloaded once without compression versus once with the --compressed flag. Here’s how it breaks down by file type:

Type Count Uncompressed Transferred Savings
HTML 1 112,061B 24,997B 77.7%
CSS 8 124,155B 21,981B 82.3%
Self JS 4 58,152B 20,860B 64.1%
External JS (Ads) 1 206,369B 206,368B 0.0%
Images 9 5,307,843B 2,266,524B 57.3%
Total 23 5,808,580B 2,540,730B 56.3%

The file count is higher than when I counted the CSS and JS in another post (CSS/JS Count Post). That earlier count only included 13 styles and scripts, whereas this one includes the actual images visible on the screen. This article features two graphs in the main body, plus thumbnails for six related posts at the bottom of the page.

You'll notice two things right away in the table. First, nine images take up 5.3MB, which is 91% of the entire page. Second, those images saw a reduction of 57.3%. Since PNG files are already compressed formats, running gzip on them usually doesn't yield much savings.

Where Did the 3.2MB Saving Come From?

If you look at each of those nine images individually, you can see exactly where that 57.3% reduction came from.

Image Before Compression Transmitted Size Savings
Related Post Thumbnail A 1,275,326B 273,690B 78.5%
Related Post Thumbnail B 1,275,326B 267,561B 79.0%
Related Post Thumbnail C 1,275,326B 248,934B 80.5%
Featured Image 495,250B 494,642B 0.1%
Related Post Thumbnail D 406,862B 406,945B -0.02%
Related Post Thumbnail E 381,194B 381,229B -0.01%
Related Post Thumbnail F 135,797B 135,130B 0.5%
Body Graph 60,005B 55,636B 7.3%
Comment Avatar (External) 2,757B 2,757B 0.0%
Web file size comparison chart
The savings came mostly from just three PNG files, accounting for 92.9% of the total. Even adding up all 13 text files only accounts for 7.5%.

Those three files alone went from 3,825,978 bytes down to 790,185 bytes—a saving of 3,035,793 bytes. A whopping 92.9% of the total page savings of 3,267,850 bytes came from these three images. The other five images saw a smaller reduction, dropping from 1,479,108 bytes to 1,473,582 bytes, for a total saving of 5,526 bytes. Even if you added up the savings from all 13 text files (HTML, CSS, and custom JS), it would only be 226,530 bytes, which is just 7.5% of what those three images achieved.

Files That Got Bigger After Compression

If you look at the table, there are two lines where the savings are negative. One file went from 406,862 bytes to 406,945 bytes, and another went from 381,194 bytes to 381,229 bytes. Each one gained 83 bytes and 35 bytes, respectively. gzip just wraps the data if it can't compress it, adding only the necessary headers and checksum. It’s less of compression and more of packaging. In these two cases, the server actually used up CPU cycles and added extra bytes during the process.

One Line of Configuration Was Doing All the Work

The reason PNG files were getting compressed was in the Apache settings. The compression directive in the httpd.conf file looked like this:

SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml 
    text/css text/javascript application/javascript application/json
DeflateCompressionLevel 6

Even though there are two lines, they do different things. The second line tells the server to apply the compression filter only to the types listed. The first line, however, tells it to apply it to all output at that location, regardless of the type. Because the first line was present, the second line was essentially just re-specifying a filter that was already being applied. As a result, this server was gzip-compressing almost every response it sent out.

Even Types Not in the List Get Compressed

You can check exactly how this setting works by looking at the response headers. I checked a few URLs that weren't in the specified list.

URL Content-Type In List? Content-Encoding
Sitemap application/xml No gzip
Body Graph PNG image/png No gzip
robots.txt text/plain Yes gzip
REST Response application/json Yes gzip

The list included text/xml, but it didn't have application/xml, and there were no image types listed at all. Yet, both returned with content-encoding: gzip. This proves the first line, not the list, was making the decision. For reference, mod_brotli is installed as a module, but it isn't active because it lacks configuration. If you only send Accept-Encoding: br, the body comes back uncompressed at 112,273B.

When Compression Doesn't Help: The Time Cost

Compression isn't free. I compared the median results after serving seven files uncompressed and seven files with compression turned on.

File TTFB (No Compression) TTFB (gzip) Difference Bytes Saved
Main Image (495KB) 20.0ms 25.1ms +5.1ms 608B
Thumbnail (407KB) 21.9ms 25.2ms +3.3ms -83B
Thumbnail (1,275KB) 18.9ms 34.0ms +15.1ms 1,001,636B

The results are similar even when compressing directly on the server. Compressing the 495KB PNG with gzip level 6 took 16.3ms, the 407KB took 13.2ms, and the 1,275KB took 55.9ms. For the third row, you spend an extra 15.1ms but save 1MB, which is clearly a win. For the first two rows, you spend 3 to 5ms and gain nothing. Since the compressed result isn't saved, this calculation repeats every time a request comes in.

The Culprit Wasn't the Server, But How the Files Were Saved

At this point, it seems like an Apache configuration issue, but what's truly strange is that three PNG files were compressed by 78% to 80%. This is a number that shouldn't happen with a properly saved PNG. I checked the compression strength indicator inside 370 PNG files in the upload folder. PNG embeds its pixel data using zlib, and the first two bytes of that data specify the compression level used.

Compression Level File Count Original Total After gzip Savings
Lowest (0) 10 12,753,260B 2,652,506B 79.2%
Default (2) 61 24,204,466B 24,210,630B -0.03%
Maximum (3) 299 30,193,050B 30,070,680B 0.4%

Out of the 370 files, only 10 files were at the lowest compression level. These ten files all have the exact same dimensions of 652x488 and a file size of 1,275,326B. If you calculate the size by taking 652 x 488 pixels and multiplying by 4 bytes per pixel, plus 1 byte for each line, you get 1,273,192B, which is almost identical to the 1,273,388B of pixel data inside the file. This means the file was saved without any compression applied to the pixels. The tool that created the image saved it with a compression level of 0, and WordPress was just storing that original file.

The thumbnails generated by WordPress are a different story. A file that is 300x225 pixels from the same original was 72,007 bytes. This is because WordPress saves the thumbnails using its own settings, so they are properly compressed. The real issue lies with the original files.

  • If you re-save 10 images, 12.7MB can drop to around 2.7MB. This saves 10MB in server space, and it eliminates the repeated compression calculations the server had to run every time these files were displayed.
  • For the remaining 360 images, there's no need to touch them. They are already saved at maximum or default quality, and even with gzip, they only compress by less than 0.4%. In fact, 211 of them won't get smaller, or might even get bigger, even after compression.
  • It is correct to change the setting to point to the type. If you leave out the first line and only keep the list of types, only the text gets compressed. For this specific page, you should target 13 pieces of text, while the 9 images should be left as they are.

What Happens on a Second Visit?

What I've described so far is based on the first-time visitor. If the same person visits again, the browser checks the file's date and asks the server if anything has changed. If nothing has changed, the server only sends a 304 response without the main content. I tested this conditional request on all 23 files.

Out of the 23 files, 21 returned a 304 response. The remaining two are the main HTML and an external ad script. The HTML requires a fresh download because of the `cache-control: no-cache, must-revalidate` setting, and the ad script follows the policies of a different server. On a repeat visit, the actual data transferred was 231,366 bytes, which is only 9.1% of the initial 2,540,730 bytes from the first visit. Of that, 206,368 bytes came from a single ad script.

The static files had the setting `cache-control: public, max-age=31536000, immutable`. This means the browser is told not to ask again for a year, so in reality, the browser doesn't even send the conditional request. Therefore, the true cost of a repeat visit is even less than 231KB.

What to Fix First

When I changed how I measured the same page, the results looked different from my previous two tests. I noticed eight CSS files when counting files, and the related posts section when counting queries. When measuring in bytes, both were tiny amounts that rounded down to nothing, and images made up 91% of the page.

The order of operations is clear. First, you need to re-save the ten PNG files that were saved at the lowest quality setting. By touching just ten files, the size drops from 12.7MB to 2.7MB, and the server no longer has to handle that work for every request.

Next up is changing the settings to limit the compression targets by type. The page weight stays the same, but you save the 3–5ms and the corresponding CPU usage that was being used for every image.

The next thing to tackle is getting the thumbnails for the related posts down to their original size.

If you only looked at the 56.3% compression ratio, you might have thought this server's compression was set up well. But digging into the details showed that number wasn't proof of good compression; it was a sign that three uncompressed files were mixed in.