Back to all questions

Is Brotli Compression Lossless?

Michael Hakimi
Compression Techniques
May 27, 2024

Is Brotli Compression Lossless?Yes, Brotli compression is a lossless compression algorithm. It reduces the size of files for web delivery without losing any data, ensuring the original content can be perfectly reconstructed.

Brotli offers better compression ratios than older web compression algorithms like gzip, making web pages load faster and saving bandwidth.

{{cool-component}}

How Brotli Compression Works

Brotli compression uses a combination of LZ77, Huffman coding, and second-order context modeling to achieve high compression ratios. Here’s a breakdown of its key components:

  1. LZ77 Algorithm: Brotli starts by identifying and eliminating repeated sequences of data. This step reduces redundancy and shrinks the file size.
  2. Huffman Coding: The algorithm then assigns shorter codes to frequently occurring patterns and longer codes to less common patterns. This step further compresses the data.
  3. Context Modeling: Brotli uses a second-order context model to make more accurate predictions about the data, enhancing compression efficiency.

Brotli Compression Levels

Brotli supports 11 compression levels, ranging from 0 (no compression) to 11 (maximum compression). Each level represents a trade-off between compression speed and compression ratio:

  • Lower Levels (0-4): These levels offer faster compression speeds but lower compression ratios. They are suitable for scenarios where speed is more critical than the compression ratio.
  • Medium Levels (5-8): These levels balance compression speed and ratio, making them suitable for general use cases.
  • Higher Levels (9-11): These levels provide the highest compression ratios but require more time and computational resources. They are ideal for scenarios where achieving the smallest file size is crucial, and speed is less of a concern.

Brotli Compression Test

To evaluate the effectiveness of Brotli compression algorithm, you can conduct tests using various tools and libraries. Here's a simple example using Node.js:

const fs = require('fs');
const zlib = require('zlib');

// Read the file to be compressed
const input = fs.readFileSync('example.txt', 'utf8');

// Compress using Brotli
zlib.brotliCompress(input, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 11 } }, (err, result) => {
  if (err) throw err;
  fs.writeFileSync('example.txt.br', result);
  console.log('File successfully compressed using Brotli.');
});

Web Compression with Brotli

Brotli is particularly effective for web compression. When enabled, it can significantly reduce the size of web assets, leading to faster page loads and improved user experience. 

Here's how to enable Brotli compression in popular web servers:

Enabling Brotli Compression in Apache

To enable Brotli compression in Apache, you need to ensure the mod_brotli module is installed and configured:

LoadModule brotli_module modules/mod_brotli.so

<IfModule mod_brotli.c>
    AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css application/javascript application/json
    # Configure compression level (optional, default is 5)
    BrotliCompressionQuality 6
</IfModule>

Enabling Brotli Compression in Nginx

For Nginx, Brotli support can be added through a third-party module. After installing the module, configure Nginx as follows:

http {
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/javascript application/json;

    server {
        # Your server configuration
    }
}