← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Brotli vs gzip Compression

Performance PHP 5.0+ Intermediate
debt(d7/e2/b3/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7), detection_hints list curl/lighthouse/chrome-devtools and automated:no — you have to inspect response headers manually or run a perf audit; no linter flags ob_gzhandler.

e2 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1, scored e2), quick_fix is to remove ob_gzhandler/zlib.output_compression and configure nginx — mostly a config swap, slightly more than one line because nginx config also needs adjusting.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3), applies to web context only and affects the response pipeline of one component; doesn't reshape the codebase but does impose ongoing CPU cost per request until fixed.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (documented gotcha)' (t5), misconception that ob_gzhandler equals server-level compression is a well-known but easy-to-miss issue — developers reasonably assume PHP-level compression is fine, missing that nginx caches compressed output.

About DEBT scoring →

Also Known As

gzip brotli content-encoding response compression

TL;DR

Brotli (br) compresses 15-25% better than gzip for text content — use Brotli for supported browsers (all modern ones), gzip as fallback, both configured at the server level not PHP.

Explanation

HTTP response compression reduces transfer size by 60-80% for text content (HTML, CSS, JS, JSON). gzip: universal support, good compression. Brotli: designed for web, 15-25% better compression than gzip, slightly slower to compress (pre-compress static files), supported by all modern browsers via Accept-Encoding: br. Configure at nginx/Apache level — never use PHP's ob_gzhandler() in production as it adds CPU overhead on every request without the benefits of server-level caching. Pre-compress static assets at build time.

Common Misconception

PHP's ob_gzhandler is equivalent to server-level compression — PHP-level compression runs on every request without caching; nginx gzip compresses once and caches the result for static files.

Why It Matters

A 200KB JavaScript bundle served without compression takes 4x longer to download than compressed — enabling Brotli on static assets costs nothing and reduces page load time for every user.

Common Mistakes

  • ob_gzhandler() in production PHP — use nginx/Apache compression instead.
  • Not pre-compressing static assets — serve pre-built .br and .gz files directly.
  • Compressing already-compressed content (images, videos) — wastes CPU with no benefit.
  • No Vary: Accept-Encoding header — CDNs may serve compressed content to clients that don't support it.

Code Examples

✗ Vulnerable
// PHP-level compression — runs on every request:
// php.ini:
zlib.output_compression = On
// or:
ob_start('ob_gzhandler');
// CPU overhead per request, no caching, misses static file benefits
✓ Fixed
# nginx — server-level Brotli + gzip:
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json;

gzip on;
gzip_comp_level 5;
gzip_types text/plain text/css application/javascript application/json;
gzip_vary on;  # Add Vary: Accept-Encoding

# Pre-compressed static assets:
location ~* \.(js|css|html)$ {
    gzip_static on;    # Serve .gz if exists
    brotli_static on;  # Serve .br if exists
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 76
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 1 ping T 1 ping F 1 ping S 2 pings S 0 pings M 1 ping T 2 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 16 Google 8 Scrapy 8 Perplexity 5 SEMrush 5 Ahrefs 3 Unknown AI 2 Claude 2 ChatGPT 2 Bing 1 Meta AI 1
crawler 47 crawler_json 6
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Let Nginx handle gzip/brotli compression, not PHP — Nginx compresses much faster with less CPU and can cache compressed output; PHP's ob_gzhandler duplicates work Nginx already does
📦 Applies To
PHP 5.0+ web
🔗 Prerequisites
🔍 Detection Hints
ob_gzhandler used when Nginx handles compression; zlib.output_compression enabled alongside Nginx gzip; no compression headers on text responses
Auto-detectable: ✗ No curl lighthouse chrome-devtools
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: File


✓ schema.org compliant