← Back to Apache Course | Chapter 5: Performance | Lesson 4 of 4

Compression / mod_deflate

mod_deflate compresses responses (HTML, CSS, JavaScript, JSON) before sending them over the network, so the browser downloads a smaller file and decompresses it locally -- usually a large win for text-based content.

How Compression Saves Bandwidth

Text-based formats -- HTML, CSS, JavaScript, JSON, plain text -- compress very well, often shrinking to a fraction of their original size, because they're full of repeated characters and patterns. The browser sends Accept-Encoding: gzip to say it can handle compressed responses, and Apache replies with a Content-Encoding: gzip header alongside the compressed body.

Enabling mod_deflate

On Ubuntu/Debian: sudo a2enmod deflate then reload. Once enabled, AddOutputFilterByType DEFLATE text/html text/css application/javascript tells Apache which content types to compress -- it's applied by MIME type, not by file extension.

Don't Compress Already-Compressed Files

JPEG, PNG, MP4, and ZIP files are already compressed internally, so running them through mod_deflate again wastes CPU for little or no size reduction -- sometimes the "compressed" output is even slightly larger than the original. Compression should be scoped to text-based MIME types only.

Warning: Compressing already-compressed formats (images, video, archives) burns CPU on every request for essentially no benefit -- restrict DEFLATE to text-based content types.

mod_deflate vs. Brotli

Gzip (via mod_deflate) is universally supported and simple to configure. Brotli, supported via the separate mod_brotli module, generally compresses a little better than gzip at the same speed, and is supported by all modern browsers -- worth considering for a further, incremental improvement once basic gzip compression is already in place.

Example: Enabling gzip compression for text content

apacheconf
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css
    AddOutputFilterByType DEFLATE application/javascript application/json
</IfModule>
{# Flagged by hand after confirming a runner can't handle this example (a shell command / go.mod file stored as a TopicExample, a language feature the configured runner version doesn't support, or output that blows a runner's sandbox limit) -- see TopicExample.norun. Never render the run button for these, regardless of language, since it would just fail at execute_code (or worse, hang the Judge0 queue on a submission that can never finish cleanly). #}

⚠️ This example can't run in the browser editor. Try it in your own local environment instead.

{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Enabling DEFLATE for every content type including images and archives, wasting CPU without meaningfully reducing size.
  2. Forgetting to enable mod_deflate itself before adding AddOutputFilterByType rules that depend on it.
  3. Not testing that compression is actually active (curl -H "Accept-Encoding: gzip" -I and checking for Content-Encoding: gzip), assuming the config works just because it was added.
Chapter Summary
  • KeepAlive and the choice of MPM (prefork/worker/event) both control how efficiently Apache handles many simultaneous connections; event is the modern default unless a non-thread-safe module forces prefork.
  • Caching (Cache-Control/Expires, ETag validators) and compression (mod_deflate for text content) both reduce how much data has to move over the network, and both need to be scoped carefully -- caching only what's safe to reuse, compressing only text-based formats.
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.