Brotli: The New Era of Data Compression

KinstaKinsta
10 min read

In the web’s pursuit of fast loading times, we have a number of different technologies to help us. One approach is to minimize the underlying code your site will use without affecting how it functions. GZIP compression is one way to do this, but Brotli compression is an alternative fledgling method that commands attention.

It’s a Google-developed solution that looks to provide a number of benefits over (as well as an alternative to) GZIP compression.

For this tutorial, we’re going to look into Brotli compression and show you how to check whether your site uses it and how to enable it if you need to.

Brotli Compression vs GZIP Compression

As mentioned, both Brotli and GZIP use the DEFLATE method to compress (and decompress) data. This can confuse many people, because this alone doesn’t warrant a switchover.

However, Google is building on top of DEFLATE to offer enhanced techniques and compress data to a greater, faster standard.

How Brotli Uses Dictionaries to Improve Data Compression

One technical aspect of data compression formats is the way Brotli compression uses existing known language and text within data dictionaries to employ its algorithm.

Developers will use a dictionary of key-value pairs to store data, as it’s efficient, flexible, and scalable. Here’s what a PHP dictionary (called an “array”) would look like:

$cats = get_categories(
  array(
    'taxonomy' => 'link_category',
    'hierarchical' => 0,
    'include' => $link_cat,
  )
);

While GZIP doesn’t use dictionaries, Brotli uses two.

Brotli’s Static Dictionary

The first is a static (i.e. pre-defined) dictionary of common code terms that act as a reference for the HTML, CSS, and JavaScript text.

There are over 13,000 words in six different languages, and Brotli will use these as references to points in the code. It’s not an exact analogy, but it’s similar to the way a WordPress hook references a larger suite of code.

As such, the encoder doesn’t have to sift through the code byte by byte. Instead, it can act on the references, pull the definition from the dictionary, and move on to the next one.

You’ll also find there are real-world phrases within the dictionary as well as code that wouldn’t often see compression. This helps some tags such as <HTML> and parameters such as type="text/javascript" take some compression and give you some greater gains.

There are also some “transforms” within the dictionary: partial, incomplete, and other types of phrases that with a new prefix, suffix, or case become a whole new word — for example, “Work” transforming to “Working” or “html” to “HTML.”

Brotli’s Dynamic Dictionary

The dynamic dictionary parses content and code at the source, which is good for smaller devices, but not so good for larger files. It’s also called a “sliding window” and can be up to 16 MB in size. This is where the compression algorithm ‘caches’ some of the most recent data in order to reference it. It’s ultra-dynamic in that it constantly changes.

If you compare this to GZIPs sliding window of around 32 KB, you’ll see that the scope for real-time parsing and compression is huge. In fact, most typical practices use a Brotli sliding window of around 4 MB, which is still mammoth compared to competing algorithms.

Benefits of Brotli Compression

There’s a lot of information to take on board about Brotli compression so far. However, we can summarize what you need to know about why you should choose Brotli over GZIP:

  1. It takes the same technology as GZIP uses and enhances it with modern methods.

  2. Brotli’s dictionary-based parsing means it can compress more of your files to a deeper level.

  3. While Brotli needs more computational power compared to GZIP, the results mean smaller files.

  4. At the compression levels, most web hosts use — something mid-range such as level four or five — Brotli performs better than GZIP without breaking a sweat.

  5. You’ll find that Brotli has near-universal support across browsers, if not some of the benchmark tools you’re used to.

  6. Brotli is free to use and open source. This is an advantage if you use a Brotli-compatible CDN, such as Cloudflare.

It’s worth noting that Cloudflare uses Brotli compression on all its servers. In fact, it uses a modified and optimized version of Brotli to give you further gains with regard to speed and file delivery.

Because Kinsta offers Cloudflare integration on all plans, every hosted site uses Brotli by default. This is just one reason why Kinsta is one of the market-leading and best hosting providers around.

1. Use an Online Tool

The simplest way to check whether your site uses Brotli compression is through an online tool. While there are a few to choose from, you’ll want something that’s quick and simple to use that will also give you a bunch of information about your setup.

Gift of Speed is our choice to check for Brotli compression.

The Gift Of Speed Brotli checker, showing that the Google website uses Brotli compression, and displaying metrics such as the page size, percentage of compression, and data on the HTTP status of the site.

The Gift of Speed website.

It will determine whether your site uses GZIP, Brotli, or no compression at all, and provide a few other metrics to help you decide what to do next. These metrics offer important insight, because you don’t only want to consider whether your site’s server uses the right “flavor” of compression.

2. Check Using Your Browser’s Developer Tools

Most developers will know that a browser offers some fantastic tools to help you with all sorts of web-related investigation and troubleshooting. One quick check you can make is whether your site (or a specific asset) uses Brotli compression.

For all major browsers such as Brave, Edge, Firefox, or Chrome, you can head to the Network > All screen.

At first, you won’t see anything relating to content headers — you’ll need to select an asset or request from the left-hand side. If you keep looking and scrolling down the list, you’ll see a panel open up that defaults to the Headers information.

Here, scroll down the output until you see the content-encoding: br line:

The Brave web browser's dev tools that shows a list of assets on the left (with

Brave’s dev tools, showing that Brotli compression is enabled for the site.

In short: If you see content-encoding: br, this tells you Brotli is active for that site.

How to Enable Brotli Compression for Your Site

We’ll show you a number of different ways to enable Brotli compression for your site. The first will be the approach we recommend for most WordPress sites not using Kinsta — and the last is what we recommend for every site reading the first approach!

1. Use a WordPress Plugin

Caching is one use case for plugins, and there are lots around. However, not all will let you enable Brotli compression, so you’ll need to choose wisely and be prepared to switch your preferred solution.

Before you undertake any changes to a site, remember to make a full backup in case you need to restore later. For this method, we’ll use W3 Total Cache because it’s straightforward to find the right setting.

You’ll need to head to the Performance > Browser Cache page within WordPress:

The WordPress dashboard, showing the

The W3 Total Cache “Browser Cache: link.

This screen shows two settings. The one you’ll want to choose is Enable HTTP (brotli) Compression:

The W3 Total Cache

Enabling Brotli compression within W3 Total Cache.

However, this isn’t going to be suitable for every site and situation. For example, Kinsta optimizes its server for fast, high-performant, reliable hosting. As such, there are a number of plugins that you won’t need, and some others are even banned from use on Kinsta sites.

In these cases, you’ll want to take another approach.

2. Enable Brotli on the Server

When it comes to choosing a server type, Nginx vs Apache is a long-standing battle that (for now) the former is winning. Regardless, both server types can enable Brotli compression, and there are different approaches for each.

Before you look into the manual approach, there are a few prerequisites that you should know:

  • You’ll want to understand how to access your configuration files for your specific server.

  • Command line knowledge will be beneficial, especially when it comes to Apache servers. To run any commands, you’ll need to be a root user with sudo privileges.

  • You might need a text editor, but for quick alterations such as this, you should be fine.

  • In some cases, you’ll need your login credentials as a secure shell (SSH) user on the server itself. You can find these within your hosting control panel, or contact support to ask.

If you have any doubts about the manual approach, we’d recommend you look into another option, or reach out to your host for help. Regardless, we’ll give a brief overview of the process for each server in turn, starting with Nginx.

Nginx

To enable Brotli compression on Nginx servers, you’ll first need to find the nginx.conf file. It will be one of a few locations:

  • /usr/local/nginx/conf

  • /etc/nginx

  • /usr/local/etc/nginx

When you have the file open, add the following to the bottom:

brotli on;
brotli_static on;
brotli_comp_level 9; # You can change this from 1–11. 4–9 offers good performance balance.
brotli_types text/plain text/css application/javascript application/json image/svg+xml application/xml+rss;

This suite will turn on Brotli and use it to serve static files. The brotli_comp_level setting is one you can change depending on your use case and needs. Higher numbers offer better compression balanced with a less performant site.

Apache

Because Apache is flexible when it comes to configurations, you can enable Brotli compression without too much fuss.

To do this, follow these steps:

  1. Log into your server using a Command Prompt or Terminal application, as a root sudo user.

  2. Run the a2enmod brotli command to turn on the compression.

  3. Within either Apache VirtualHost or your server configuration, add the AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript line to set the right file types you’d like to compress.

While Apache doesn’t support static compression, you can change the level of Brotli compression you offer using the BrotliCompressionQuality LEVEL-NUMBER line. However, you’ll need to replace the “LEVEL-NUMBER” place holder with a number between 1–11.

3. Use a Supporting Web Host

The most straightforward way to enable Brotli compression for your site is to ensure your host does this by default. Kinsta offers Brotli compression as standard thanks to its integration with Cloudflare’s CDN.

A drawing of a blue cylindrical server stack, in front of a blue sphere surrounded by thin orange rings, connected to the sphere with dots.

Cloudflare’s CDN page.

The Kinsta CDN is powered by Cloudflare’s infrastructure on all plans — so every site uses Brotli compression without you needing to enable it.

You’ll want to check whether your chosen host offers Brotli compression, and to what level you need to configure it. In order to run the most performant, stable, and secure site, good hosting is essential.

Summary

Data compression is a necessary component of developing and using the modern web. File sizes can skyrocket due to the rich and complex file types you’ll use to piece together a website. All of them need some form of compression.

The typical approach has been GZIP up ’til now, but there’s a new kid on the block.

Brotli compression bases its technology on the same foundation as GZIP, but includes some performance-enhancing benefits. As we discussed, it uses context mapping to process a compression request faster, and a dictionary that uses dynamic population. This is much larger than what GZIP can offer, and also lets mobile users benefit from the compression too.

The good news is that every Kinsta site can benefit from Brotli compression because of our unique Cloudflare integration. This means your Kinsta-hosted site is faster than the competition using GZIP and loads rapidly for those on smaller devices.

0
Subscribe to my newsletter

Read articles from Kinsta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kinsta
Kinsta

Power your web projects with Kinsta - the fastest, most reliable hosting for WordPress sites, applications, databases, and static sites. Kinsta is for all projects, from small blogs to Fortune 500 customers. Trusted by 120,000+ businesses from 128 countries around the globe. The only continent we haven't reached yet is Antarctica. Get started today at kinsta.com