Use EWMA for latency-based load balancing
Service Mesh (ASM) version 1.21 introduces the peak EWMA (exponentially weighted moving average) load balancer. This algorithm calculates a score for each pod based on a moving average of metrics, such as static weights, latency, and error rates, for load balancing. When a backend service experiences a traffic surge, ASM uses the peak EWMA algorithm to monitor the peak load and real-time response time of each pod. This enables flexible traffic distribution to better manage traffic spikes. This topic explains how to configure and use the exponentially weighted moving average (EWMA) algorithm for latency-based load balancing.
Background
Service Mesh (ASM) provides several common load balancing algorithms, including round robin, least request, and random. These algorithms are suitable for most business scenarios and provide adequate performance. However, these algorithms select backends based only on static rules, ignoring the real-time status and performance of the backend pods.
For example, if a backend pod responds slowly because other applications consume resources on its host, the default load balancing algorithm may continue sending it traffic. This leads to increased request latency or even timeouts, while other pods remain idle. If the load balancer could intelligently avoid the poorly performing pod and route more traffic to idle ones, it would significantly reduce the application's overall error rate and response latency.
Prerequisites
-
You have an ASM instance of version 1.21 or later. For more information, see Create an ASM instance.
-
You have added a Container Service for Kubernetes (ACK) cluster to the ASM instance. For more information, see Add a cluster to an ASM instance and Upgrade an ASM instance.
A kubectl client is connected to the cluster. For more information, see Connect to an ACK cluster using kubectl.
Procedure
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose . On the page that appears, click Create from YAML.
-
Enter the following sample content and click Create. This sample YAML specifies the
PEAK_EWMAload balancing algorithm for the simple-server service in the default namespace.apiVersion: networking.istio.io/v1beta1 kind: DestinationRule metadata: name: simple-server namespace: default spec: host: simple-server.default.svc.cluster.local trafficPolicy: loadBalancer: simple: PEAK_EWMA # Use the ASM PEAK_EWMA load balancer.
Example
Example overview
This example uses the simple-server application as the server and the sleep application as the client that generates test traffic. The simple-server.default.svc.cluster.local service, which is part of the simple-server application, acts as the server. This service includes two deployments with different configurations:
-
simple-server-normal: This deployment is configured with a response latency of 50 ms to 100 ms.
-
simple-server-high-latency: This deployment simulates a scenario where some workloads of a service experience increased latency.
Step 1: Enable ASM metrics
To demonstrate the effect of the peak EWMA load balancer, this example enables ASM metrics. This allows you to observe the change in overall service response time before and after the algorithm is enabled. For more information about how to enable metrics and collect them in Managed Service for Prometheus, see Collect metrics in Managed Service for Prometheus.
Step 2: Deploy the environment
-
In your KubeConfig environment for the ACK cluster, create a file named sleep.yaml with the following content.
Run the following command to deploy the sleep application.
kubectl apply -f sleep.yaml -
Create a file named simple.yaml with the following content.
Run the following command to deploy the simple-server-normal and simple-server-high-latency applications.
kubectl apply -f simple.yaml
Step 3: Run a baseline test
This section generates baseline data by using the default LEAST_REQUEST load balancing algorithm.
-
Run the following command to start the test. This command makes 100 calls to the
/helloendpoint of the simple-server service:kubectl exec -it deploy/sleep -c sleep -- sh -c 'for i in $(seq 1 100); do time curl simple-server:8080/hello; echo "request $i done"; done'The expected output is as follows:
hello this is port: 8080real 0m 0.06s user 0m 0.00s sys 0m 0.00s request 1 done hello this is port: 8080real 0m 0.09s user 0m 0.00s sys 0m 0.00s request 2 done ...... hello this is port: 8080real 0m 1.72s user 0m 0.00s sys 0m 0.00s request 100 done -
After the command finishes, on the Mesh Management page, click the name of your ASM instance. In the left-side navigation pane, choose . On the Cloud ASM Istio Service tab, enter the following filter conditions:
-
Namespace:
default -
Service:
simple-server.default.svc.cluster.local -
Reporter:
destination -
Client Workload
Namespace: default -
Client Workload:
sleep -
Service Workload Namespace:
default -
Service Workload:
simple-server-normal + simple-server-high-latency
-
-
Click the Client Workloads drop-down menu and view the Incoming Request Duration By Source panel.

The results show that the P50 response time for requests from the
sleepapplication to thesimple-serverservice is 87.5 ms, while the P95 response time increases significantly to 2.05 s. This is because the higher latency ofsimple-server-high-latencyincreases the overall response time of the service.ImportantThe test results are theoretical values obtained in a controlled experimental environment. Actual results may vary depending on your specific workloads and environment.
Step 4: Configure peak EWMA and re-run the test
Create a DestinationRule to apply the peak EWMA load balancing algorithm to the simple-server service.
-
Follow the steps in Procedure and use the following YAML content to create the DestinationRule.
apiVersion: networking.istio.io/v1beta1 kind: DestinationRule metadata: name: simple-server namespace: default spec: host: simple-server.default.svc.cluster.local trafficPolicy: loadBalancer: simple: PEAK_EWMA -
In your KubeConfig environment for the ACK cluster, run the test command again.
kubectl exec -it deploy/sleep -c sleep -- sh -c 'for i in $(seq 1 100); do time curl simple-server:8080/hello; echo "request $i done"; done' -
Comparing the results with the metrics from Step 3 shows that the P90, P95, and P99 response times decrease significantly. This is because the peak EWMA load balancer detects the higher latency of the simple-server-high-latency workload and reduces its load balancing weight. As a result, more requests are routed to the lower-latency simple-server-normal workload. This significantly reduces the overall request latency for the service.
