All Products
Search
Document Center

Alibaba Cloud Service Mesh:Configure HTTP compression for inter-service calls with ASMCompressor

Last Updated:Mar 11, 2026

When microservices communicate over HTTP, 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.

ASMCompressor supports two compression algorithms:

AlgorithmCompression ratioSpeedBest for
GzipGoodFast compression and decompressionBoth static and dynamic content. Widely supported by browsers and web servers.
BrotliHigher than GzipSlower compression, faster decompressionStatic content such as HTML, CSS, and JavaScript files.

Prerequisites

Step 1: Record the baseline response size

Before enabling compression, record the uncompressed response size. This baseline lets you measure the compression ratio later.

  1. Get the IP address of the ingress gateway.

    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, find the value of Service address.

  2. Open http://<ASM-gateway-address>/productpage in a browser.

  3. Open the browser developer tools and check the response size for the productpage request. The response size is approximately 5.5 KB without compression.

    Response size before compression (5.5 KB)

Step 2: Enable Gzip compression

Create an ASMCompressor resource to enable Gzip compression on the ingress gateway.

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

    For more information about the fields, see ASMCompressor field descriptions.

    Expand to view the 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-direction compression
        common_config:
          enabled:
            default_value: false
            runtime_key: NOT_EXISTENT_KEY
      response_direction_config:   # Enable response-direction 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. Apply the configuration:

    kubectl apply -f ingressgateway-gzip.yaml

Step 3: Verify Gzip compression

  1. Open http://<ASM-gateway-address>/productpage in a browser.

  2. Open the browser developer tools and check the response size for the productpage request. The response size drops from 5.5 KB to approximately 1.8 KB, confirming that Gzip compression is active.

    Response size after Gzip compression (1.8 KB)

Step 4: Enable and verify Brotli compression

This step adds a test route for the /helloworld path and applies Brotli compression to it.

Add a test route

  1. Add the following directResponse block to the spec.http section of the Bookinfo virtual service (bookinfo.yaml):

    - 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 complete bookinfo.yaml file after modification:

    View the complete 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. Apply the virtual service:

    kubectl apply -f bookinfo.yaml

Create the Brotli ASMCompressor resource

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

    For more information about the fields, see ASMCompressor field descriptions.

    Expand to view the ingressgateway-brotli.yaml file

    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-direction 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. Apply the configuration:

    kubectl apply -f ingressgateway-brotli.yaml

Verify Brotli compression

  1. Send a request with the Accept-Encoding: br header and save the response:

    curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/helloworld > out.br
  2. Install the Brotli command-line tool:

  3. Decompress the saved response:

    brotli -d out.br

    If decompression succeeds and the output file contains the expected content, Brotli compression is working correctly.

Step 5: Disable compression for a specific route

By default, the ASMCompressor resource applies compression to all matching responses. To disable compression on a specific route while keeping it enabled for others, use per_route_configs.

This example disables compression on the /helloworld route while keeping it active for /productpage.

Confirm that compression is active on all routes

Run the following commands to compare compressed and uncompressed response sizes for both paths:

curl http://<ASM-gateway-address>/helloworld > helloworld_no_compress.html
curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/helloworld > helloworld_compress.html
curl http://<ASM-gateway-address>/productpage > productpage_no_compress.html
curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/productpage > productpage_compress.html

Check the file sizes:

ls -lh

Expected output:

-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

Both paths return smaller responses when compression is requested.

Add a route name to the virtual service

To target a specific route in the per-route configuration, assign a name to the route in the virtual service.

Add name: helloworld-route to the /helloworld route in bookinfo.yaml:

View the 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
      name: helloworld-route    # Route name used by per_route_configs to match this route
    - 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

Add a per-route override to ASMCompressor

Update the ASMCompressor resource to disable compression on the helloworld-route:

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-direction 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:            # Route-level overrides
  - disabled: true
    route_match:
      vhost:
        route:
          name: helloworld-route    # Must match the route name in the VirtualService
  workloadSelector:
    labels:
      istio: ingressgateway

Verify the per-route override

  1. Send a compressed request to the /helloworld path:

    curl -H "Accept-Encoding: br" http://<ASM-gateway-address>/helloworld > helloworld_route_disabled.html
  2. Compare the file sizes:

    ls -lh

    Expected output:

    -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

    The /helloworld response is now 324 B (same as the uncompressed size), confirming that compression is disabled for this route. The /productpage response remains compressed at 1.3 KB.