All Products
Search
Document Center

Alibaba Cloud Service Mesh:Configure HTTP compression with ASMCompressor

Last Updated:Jun 20, 2026

When microservices communicate, response payloads can consume significant bandwidth, especially for text-based content like JSON, HTML, and CSS. ASMCompressor lets you add compression filters at the proxy layer so that all services share a single, consistent compression configuration instead of implementing compression separately in each language or framework. This topic describes how to use ASMCompressor in ASM to define compression settings for inter-service calls.

Prerequisites

Feature overview

The proxy supports two compression algorithms: Gzip and Brotli.

  • Gzip is a widely used compression algorithm supported by many web servers and browsers, known for its high compression ratio and fast compression speed. It is typically used to compress both static and dynamic content.

  • Brotli is an advanced compression algorithm that provides a higher compression ratio and faster decompression speed, and is widely used on the web. Brotli is typically used to compress static content, such as HTML, CSS, and JavaScript files.

Brotli offers a higher compression ratio than Gzip but may compress more slowly.

Enable and verify Gzip compression

Step 1: Record the baseline response size

  1. Obtain the ingress gateway address.

    1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

    2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose ASM Gateways > Ingress Gateway.

    3. On the Ingress Gateway page, obtain the Service address.

  2. In your browser's address bar, enter http://{ASM_GATEWAY_ADDRESS}/productpage to access the Bookinfo application. Replace {ASM_GATEWAY_ADDRESS} with the address you obtained.

  3. Open your browser's developer tools and check the size of the response for the productpage request.

    The response size is 5.5 KB.

Step 2: Enable Gzip compression

  1. Create a file named ingressgateway-gzip.yaml with the following content.

    For a description of the fields, see ASMCompressor CRD reference.

    ingressgateway-gzip.yaml

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: ASMCompressor
    metadata:
      name: ingressgateway-gzip
      namespace: istio-system
    spec:
      compressor_library:
        gzip:
          compression_level: BEST_COMPRESSION
          compression_strategy: DEFAULT_STRATEGY
          memory_level: 9
          window_bits: 15
      isGateway: true
      portNumber: 80
      request_direction_config:  # Disable request compression.
        common_config:
          enabled:
            default_value: false
            runtime_key: NOT_EXISTENT_KEY
      response_direction_config:  # Enable response compression.
        common_config:
          content_type:
          - application/json
          - text/plain
          - text/html
          min_content_length: 100
        disable_on_etag_header: true
      workloadSelector:
        labels:
          istio: ingressgateway
  2. Run the following command to enable Gzip compression:

    kubectl apply -f ingressgateway-gzip.yaml

Step 3: Verify Gzip compression

  1. In your browser's address bar, enter http://{ASM_GATEWAY_ADDRESS}/productpage to access the Bookinfo application.

  2. Open your browser's developer tools and check the size of the response for the productpage request.

    The response size drops from 5.5 KB to 1.8 KB. This confirms that Gzip compression is enabled.

Step 4: Verify Brotli compression

  1. Modify the bookinfo VirtualService.

    1. Add the following content under spec.http in the bookinfo.yaml file.

      - directResponse:
              body:
                string: >-
                  Hello, world! Hello, world! Hello, world! Hello, world! Hello,
                  world! Hello, world! Hello, world! Hello, world! Hello, world!
                  Hello, world!
                   Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!
                    Hello, world! Hello, world! Hello, world! Hello, world!
              status: 200
            match:
              - uri:
                  prefix: /helloworld

      The updated YAML content is as follows:

      Modified bookinfo.yaml

      apiVersion: networking.istio.io/v1beta1
      kind: VirtualService
      metadata:
        name: bookinfo
      spec:
        gateways:
          - bookinfo-gateway
        hosts:
          - '*'
        http:
          - directResponse:
              body:
                string: >-
                  Hello, world! Hello, world! Hello, world! Hello, world! Hello,
                  world! Hello, world! Hello, world! Hello, world! Hello, world!
                  Hello, world!
                   Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!
                    Hello, world! Hello, world! Hello, world! Hello, world!
              status: 200
            match:
              - uri:
                  prefix: /helloworld
          - match:
              - uri:
                  exact: /productpage
              - uri:
                  prefix: /static
              - uri:
                  exact: /login
              - uri:
                  exact: /logout
              - uri:
                  prefix: /api/v1/products
            route:
              - destination:
                  host: productpage
                  port:
                    number: 9080
      
    2. Run the following command to apply the VirtualService:

      kubectl apply -f bookinfo.yaml
  2. Enable Brotli compression.

    1. Create a file named ingressgateway-brotli.yaml with the following content.

      For a description of the fields, see ASMCompressor CRD reference.

      ingressgateway-brotli.yaml

      apiVersion: istio.alibabacloud.com/v1beta1
      kind: ASMCompressor
      metadata:
        name: ingressgateway-brotli
        namespace: istio-system
      spec:
        compressor_library:
          brotli:
            quality: 5
            window_bits: 15
        isGateway: true
        portNumber: 80
        request_direction_config:  # Disable request compression.
          common_config:
            enabled:
              default_value: false
              runtime_key: NOT_EXISTENT_KEY
        response_direction_config:
          common_config:
            content_type:
            - application/json
            - text/plain
            - text/html
            min_content_length: 100
          disable_on_etag_header: true
        workloadSelector:
          labels:
            istio: ingressgateway
    2. Run the following command to enable Brotli compression:

      kubectl apply -f ingressgateway-brotli.yaml
  3. Verify that Brotli compression is enabled.

    1. Run the following command to save the response from the /helloworld path to the out.br file:

      curl -I  -H "Accept-Encoding: br" http://{ASM_GATEWAY_ADDRESS}/helloworld > out.br
    2. Install Brotli.

      • macOS: Run the brew install brotli command to install.

      • On Windows, see the installation instructions at brotli.

    3. Run the brotli command to decompress the out.br file:

      brotli -d out.br

      The creation of a decompressed out file with the correct content confirms that Brotli compression is enabled.

Step 5: Verify per-route configuration

  1. After the configuration in Step 4, compression is enabled for responses on all paths. You can set the gateway IP as an environment variable and run the following commands:

    export ASM_GATEWAY_IP=<your_gateway_ip>
    curl ${ASM_GATEWAY_IP}/helloworld > helloworld_no_compress.html
    curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/helloworld > helloworld_compress.html
    curl ${ASM_GATEWAY_IP}/productpage > productpage_no_compress.html
    curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/productpage > productpage_compress.html
  2. Run the ls command to compare the response sizes for these two paths before and after compression:

    ls -lh
    total 40
    -rw-r--r--@ 1 user  staff    37B  5 28 18:16 helloworld_compress.html
    -rw-r--r--@ 1 user  staff   324B  5 28 18:15 helloworld_no_compress.html
    -rw-r--r--@ 1 user  staff   1.2K  5 28 18:17 productpage_compress.html
    -rw-r--r--@ 1 user  staff   5.2K  5 28 18:16 productpage_no_compress.html
  3. In the VirtualService, add a name to the helloworld route. The modified bookinfo.yaml file is as follows:

    Modified YAML

    apiVersion: networking.istio.io/v1beta1
    kind: VirtualService
    metadata:
      name: bookinfo
    spec:
      gateways:
        - bookinfo-gateway
      hosts:
        - '*'
      http:
        - directResponse:
            body:
              string: >-
                Hello, world! Hello, world! Hello, world! Hello, world! Hello,
                world! Hello, world! Hello, world! Hello, world! Hello, world!
                Hello, world!
                 Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world! Hello, world!
                  Hello, world! Hello, world! Hello, world! Hello, world!
            status: 200
          match:
            - uri:
                prefix: /helloworld
          name: helloworld-route    # Specify a route name so the compression filter can match it.
        - match:
            - uri:
                exact: /productpage
            - uri:
                prefix: /static
            - uri:
                exact: /login
            - uri:
                exact: /logout
            - uri:
                prefix: /api/v1/products
          route:
            - destination:
                host: productpage
                port:
                  number: 9080
  4. Add a per-route configuration to the ASMCompressor and disable compression on the helloworld route. The modified ASMCompressor is as follows:

    apiVersion: istio.alibabacloud.com/v1beta1
    kind: ASMCompressor
    metadata:
      name: ingressgateway-gzip
      namespace: istio-system
    spec:
      compressor_library:
        brotli:
          quality: 5
          window_bits: 15
      isGateway: true
      portNumber: 80
      request_direction_config:  # Disable request compression.
        common_config:
          enabled:
            default_value: false
            runtime_key: NOT_EXISTENT_KEY
      response_direction_config:
        common_config:
          content_type:
          - application/json
          - text/plain
          - text/html
          min_content_length: 100
        disable_on_etag_header: true
      per_route_configs:  # Added per-route configuration.
      - disabled: true
        route_match:
          vhost:
            route:
              name: helloworld-route  # Enter the route name configured in the VirtualService here.
      workloadSelector:
        labels:
          istio: ingressgateway
  5. Run the following command to verify that compression is disabled for responses from the /helloworld path:

    curl -H "Accept-Encoding: br" ${ASM_GATEWAY_IP}/helloworld > helloworld_route_disabled.html
  6. Run the ls command again to check the current response size of the /helloworld path.

    ls -lh
    total 48
    -rw-r--r--@ 1 user  staff    37B  5 28 18:16 helloworld_compress.html
    -rw-r--r--@ 1 user  staff   324B  5 28 18:15 helloworld_no_compress.html
    -rw-r--r--@ 1 user  staff   324B  5 28 19:29 helloworld_route_disabled.html
    -rw-r--r--@ 1 user  staff   1.3K  5 28 19:30 productpage_compress.html
    -rw-r--r--@ 1 user  staff   5.2K  5 28 18:16 productpage_no_compress.html

    This confirms that route-level compression settings work as expected.