Enable dynamic compression

Dynamic compression automatically compresses responses served by Media CDN. The size of the data sent over the network is reduced by 60% to 85% in typical cases.

The size reduction accelerates the download of important assets, such as stylesheets (CSS), scripts (JavaScript), and video manifests (HLS/DASH), which can significantly reduce page load and video start times.

Large live video playlists (manifests) have a significant amount of repeated data and fetches, including the host and path prefix of each segment, as well as the HLS or DASH playlist metadata. The faster the playlist loads or playlist updates can be downloaded, the less time that a client needs to wait to parse and start downloading the referenced video segments. HLS and DASH playlists often undergo a total size reduction of more than 90%.

For more information about the benefits of compressing responses, see the Web Fundamentals guide.

How dynamic compression works

When dynamic compression is enabled, compressible content that is served from the origin can be compressed before being delivered if the client accepts one of the supported compression algorithms (br or gzip).

Media CDN adds a Vary: Accept-Encoding header to all responses eligible for compression. For related information, see Incompressible content.

Additionally, if the request's Accept-Encoding header indicates a preference for compressed content by specifying br or gzip (and optionally including a nonzero q parameter), Media CDN does the following:

  • Removes the Content-Length header from the response; this is necessary to allow the response to be served as quickly as possible because the full content length is unknown until the entire response has been compressed. For HTTP/1.1 and earlier, Media CDN uses Transfer-Encoding: chunked in the response when it doesn't use Content-Length.

    After a response has been compressed and cached, Media CDN can include the Content-Length header in subsequent responses and set the value to the length of the compressed body content.

  • Sets Accept-Ranges to none. This informs clients that range requests for this resource are ignored.

  • Weakens any strong ETag response headers, as required by RFC 9110 section 8.8.3. For example, ETag: "xyzzy" is replaced with ETag: W/"xyzzy".

  • Sets the Content-Encoding header to br or gzip, which signifies the chosen compression algorithm.

    Media CDN chooses the best compression algorithm based on the anticipated compression ratio of the response and the compression speed or throughput.

    • Brotli compression is used if the client supports it, even if other compression algorithms have higher q values in the Accept-Encoding header.

    • HLS manifests are compressed using only gzip.

    Media CDN determines the compression level to balance the total download size and CPU cost on the client. Higher compression levels don't always benefit performance, especially on lower-powered mobile devices.

Configure dynamic compression

You can enable dynamic compression on routes serving requests.

Before you begin

Do the following:

Enable dynamic compression for a route rule

By default, the compression mode for a route rule is disabled.

Setting the mode to automatic enables dynamic compression for every eligible response. Further, it instructs Media CDN to automatically choose the best compression algorithm.

To enable dynamic compression, do the following:

Console

  1. In the Google Cloud console, go to the Media CDN page.

    Go to Media CDN

  2. To open the Details page of the service for which you want to configure a route rule, click the service name.

  3. To switch to the edit mode, click the Edit button.

  4. To navigate to the Routing section, click Next.

  5. To edit a host rule, click the arrow to expand it.

  6. To edit a route rule, click Edit on the respective row.

  7. In the Edit route rule pane, click Advanced configurations.

  8. Optional: For Route action, add a CDN policy item.

    A CDN policy lets Media CDN compress content once and serve it multiple times, which saves bandwidth and speeds up delivery.

  9. In the Dynamic compression section, select Enable compression.

  10. To save the route rule, click Save.

  11. To save your changes to the service, click Update service.

gcloud and YAML

  1. Export your Media CDN configuration into a YAML file. Use the gcloud edge-cache services export command.

    gcloud edge-cache services export SERVICE_NAME \
        --destination=FILENAME.yaml
    

    Replace the following:

    • SERVICE_NAME: the name of your service
    • FILENAME : the name of your YAML file
  2. In the route definition in the YAML file, under routeAction, set compressionMode to AUTOMATIC, as shown in the following sample:

    routing:
    hostRules:
    - hosts:
      - media.example.com
      pathMatcher: routes
    pathMatchers:
    - name: routes
      routeRules:
        - priority