Lossless Compression: Definition, Algorithms, Web Use
Lossless compression shrinks files without losing data — original is reconstructed exactly. Algorithms: gzip, Brotli, Zstd, LZ4, xz.
What is lossless compression?
Lossless compression is the practice of reducing file size while preserving every bit of the original data — decompression reconstructs the file exactly. The opposite is lossy compression (used for images, audio, video), which permanently discards data to achieve smaller sizes.
Lossless is the standard for any data where bit-perfect accuracy matters: text, source code, executables, databases, archive files, and HTTP responses (HTML, CSS, JavaScript, JSON, XML).
How lossless compression works
Lossless algorithms exploit redundancy in data — patterns that repeat or could be encoded more efficiently. Two main techniques:
- Dictionary coding (LZ77, LZ78 family): replace repeated strings with references to a dictionary or earlier occurrence. Used by gzip, zlib, Zstd, LZ4.
- Entropy coding (Huffman, arithmetic): assign shorter bit codes to more frequent symbols. Used as a final pass in most compressors.
Modern compressors (Brotli, Zstd) combine multiple techniques + pre-trained dictionaries.
Common lossless compression algorithms
| Algorithm | Speed | Compression ratio | Best for |
|---|---|---|---|
| gzip / DEFLATE | Fast | Moderate | HTTP, tar.gz, zip — universal |
| Brotli | Medium | Better than gzip (~20%) | HTTP responses (modern web) |
| Zstandard (Zstd) | Very fast | Better than gzip | Real-time, log archives |
| LZ4 | Fastest | Lower ratio | Real-time streams, in-memory |
| xz / LZMA | Slow | Best ratio | Source archives, Linux distros |
| bzip2 | Slow | Better than gzip | Legacy archives |
| Snappy | Very fast | Lower ratio | BigTable, RPC payloads |
Lossless vs lossy compression
| Aspect | Lossless | Lossy |
|---|---|---|
| Reconstruction | Bit-perfect | Approximate |
| Use cases | Text, code, data | Images, audio, video |
| Examples | gzip, Brotli, PNG, FLAC | JPEG, MP3, H.264 |
| Compression ratio | 2-10× | 10-100× |
| Reversible? | Yes | No |
Lossless compression on the web
HTTP supports lossless compression via the Accept-Encoding request header and Content-Encoding response header:
GET / HTTP/1.1
Accept-Encoding: br, gzip, deflate
HTTP/1.1 200 OK
Content-Encoding: br
Content-Type: text/html| Encoding | Browser support | Compression ratio (HTML/JS) |
|---|---|---|
| gzip | Universal | ~70% reduction |
| Brotli | ~96% browsers | ~75% reduction |
| Zstd | Chrome 122+, growing | Similar to Brotli, faster |
Use Brotli when available, fall back to gzip. Most CDNs (Cloudflare, Fastly, AWS CloudFront) negotiate this automatically.
When to use which compressor
HTTP responses (HTML, CSS, JS)
Brotli for static (compressed once at build, served from CDN). Brotli or gzip for dynamic. Compress at level 11 (max) for static; level 4-6 for dynamic.
Real-time streaming / low-latency
LZ4 or Snappy. Sacrifice ratio for speed. Used by Kafka, BigTable, Redis (with compression).
Long-term archives
xz/LZMA or zstd at high level. Best ratio matters more than speed.
Logs in motion
Zstd with shared dictionary trained on log format. Beats gzip at both speed + ratio.
Lossless compression best practices
- Compress at build time when possible. Pre-compressed assets cost zero CPU at request time.
- Use Brotli over gzip. ~20% smaller payloads; modern browser support.
- Don't double-compress. Already-compressed files (JPEG, MP4, .zip) won't get smaller — wasted CPU.
- Pick level for use case. Level 11 for build-time; level 4-6 for runtime.
- Use shared dictionaries for similar payloads. Zstd shines here (e.g., compressing many similar JSON docs).
- Measure both ratio and CPU. Higher levels = diminishing ratio gains but big CPU cost.
- Compress before encryption. Encrypted data is essentially random — compresses poorly.
- Cache compressed responses. Don't recompress identical responses on each request.
Compression in popular formats
| Format | Compression | Lossless? |
|---|---|---|
| PNG | DEFLATE | Yes |
| WebP | VP8/VP9 | Both modes (lossless or lossy) |
| FLAC | Custom (LPC + Rice) | Yes (audio) |
| ZIP | DEFLATE (typically) | Yes |
| tar.gz | gzip | Yes |
| Parquet | Snappy/gzip/Zstd | Yes (data warehouse) |
FAQ: lossless compression
What's the best lossless compressor?
Depends on tradeoff. Best ratio: xz/LZMA. Best balance: Zstd. Best speed: LZ4. Best web compatibility: gzip; modern web: Brotli.
Why is my file not getting smaller after compression?
Already-compressed data (JPEG, MP4, encrypted) has no exploitable redundancy. Random data is incompressible by definition.
Should I compress JSON?
Yes — JSON has high redundancy (repeated keys), often shrinks 80%+. Always Content-Encoding: gzip/br on JSON APIs.
What about images: PNG vs JPEG?
PNG is lossless (good for screenshots, logos, anything with sharp edges). JPEG is lossy (good for photos). WebP and AVIF support both.
Is Brotli better than gzip?
Yes — for HTTP responses, Brotli typically yields 15-25% smaller payloads at the same speed. Use Brotli; fall back to gzip for ancient clients.
Can compression help my website's performance?
Yes — compressed HTML/CSS/JS reduces transfer time, improving LCP and TTFB. Critical for Core Web Vitals.
What's the relationship between compression and encryption?
Compress first, then encrypt. Encryption produces high-entropy output that doesn't compress. Compressing encrypted data wastes CPU.
Test compressed payload performance with LoadFocus
LoadFocus runs JMeter and k6 scripts that measure response sizes and TTFB across 25+ regions, helping you verify Brotli/gzip configuration is actually saving bandwidth. Sign up free at loadfocus.com/signup.
Related LoadFocus Tools
Put this concept into practice with LoadFocus — the same platform that powers everything you just read about.