Implement session affinity in an ASM gateway
Session affinity, also known as sticky sessions, is a load balancing technology that ensures all requests from the same user or session go to the same backend server. It suits applications that need to maintain user state, such as online shopping carts, login sessions, and personalization settings.
Prerequisites
-
A cluster is added to the ASM instance. For more information, see Add a cluster to an ASM instance.
-
An ingress gateway is created with port 80 exposed. For more information, see Create an ingress gateway.
-
The httpbin application is deployed. For more information, see Deploy the HTTPBin application.
Features
ASM supports the following two session affinity modes:
-
Consistent hashing-based session affinity (soft session affinity): Implemented based on consistent hashing algorithms. Envoy supports two consistent hashing algorithms: RingHash and Maglev. The load balancing implemented by consistent hashing algorithms is a form of soft session affinity; when endpoints change, session affinity fails for a small portion of requests. Session affinity based on consistent hashing achieves better load balancing, is completely transparent to clients, and is better suited to distributed systems.For information about how to configure consistent hashing-based load balancing, see LoadBalancerSettings.ConsistentHashLB.
-
Strongly consistent session affinity: Implemented based on the Service label
istio.io/persistent-session. Endpoint changes do not affect existing sessions, which guarantees strict session consistency. However, the client must pass a Cookie or Header, and long sessions may cause uneven load distribution. This mode is suitable for applications that require strict session affinity.
Preparations
Before you test the session affinity feature, complete the following preparations:
Scale out the httpbin application to multiple replicas
Use the KubeConfig of the data plane to scale out the httpbin application to three replicas for testing the session affinity feature.
kubectl scale deployment/httpbin --replicas 3
View the effect when session affinity is not enabled
-
In a browser, visit
http://${ASM gateway address}/status/418, and after the page loads, refresh the page multiple times.For information about how to obtain the ASM gateway address, see Obtain the address of an ingress gateway.
-
View the gateway logs.
-
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 Ingress Gateway page, click Log Center on the right side of the target gateway. On the Gateway Logs tab, add the search condition
and 418in the search box, and click Search & Analyze. On the Raw Logs tab in the lower-left corner, expand the upstream_addr index.The requests are distributed relatively evenly across the three httpbin pods.

-
Consistent hashing-based session affinity
Deploy a destination rule
Use the following content to create a destination rule. For instructions, see Manage destination rules.
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: httpbin
namespace: default
spec:
host: httpbin.default.svc.cluster.local
trafficPolicy:
loadBalancer:
consistentHash:
httpCookie:
name: sticky-session-key
ttl: 0s
Deploy this destination rule. If a request to the httpbin service through the gateway does not carry the Cookie, the gateway derives a hash value from the request's source and destination IP addresses and ports. The gateway also sets this Cookie when the request returns. If a subsequent request carries this Cookie, the gateway does not regenerate the value. This Cookie is used as the key input of the hash function in the consistent hashing algorithm, which then calculates the actual backend that is ultimately accessed.
The effectiveness of session affinity is guaranteed by the specific consistent hashing algorithm. In this example, no consistent hashing algorithm is explicitly configured, and the RingHash algorithm is used by default. ASM has supported the Maglev algorithm since version 1.16. Choose an algorithm based on your requirements.
View the effect after session affinity is enabled
-
In a browser, visit
http://${ASM gateway address}/status/333, and after the page loads, refresh the page multiple times.For information about how to obtain the ASM gateway address, see Obtain the address of an ingress gateway.
-
View the gateway logs.
-
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 Ingress Gateway page, click Log Center on the right side of the target gateway. On the Gateway Logs tab, add the search condition
and 333in the search box, and click Search & Analyze. On the Raw Logs tab in the lower-left corner, expand the upstream_addr index.All requests are forwarded to the same backend httpbin pod.

-
-
Open the developer tools of your browser, click Network, refresh the page, and then view and click the request that is sent.
The website has a Cookie. The name of the Cookie is the name that is configured in the DestinationRule. The ASM gateway implements session affinity based on this Cookie.

Strongly consistent session affinity
Strongly consistent session affinity is implemented by adding the istio.io/persistent-session label to a Service. ASM automatically enables Cookie-based session affinity for that Service. Unlike the consistent hashing mode, strongly consistent session affinity does not affect existing sessions when endpoints change.
-
Add the
istio.io/persistent-sessionlabel to the httpbin Service.Use the KubeConfig of the data plane to run the following command:
kubectl patch svc httpbin -n default -p '{"metadata":{"labels":{"istio.io/persistent-session":"sticky-session-cookie"}}}'The value of the label,
sticky-session-cookie, is the name of the Cookie that is used for session affinity. -
Send the first request to get the Cookie.
Access the httpbin service through the ASM gateway and save the Cookie in the response to a local file.
curl -c /tmp/cookies.txt -v http://${ASM gateway address}/headersThe response headers include
Set-Cookie: sticky-session-cookie=<value>, which indicates that the ASM gateway has assigned a session Cookie to this request. -
Send multiple requests that carry the Cookie to verify the effect of session affinity.
Send multiple requests by using the Cookie obtained in the previous step:
for i in $(seq 1 5); do curl -s -b /tmp/cookies.txt http://${ASM gateway address}/headers echo "---" doneCheck the gateway logs to see whether all requests are forwarded to the same backend httpbin pod. The verification method is the same as the log viewing method described in the Consistent hashing-based session affinity section.
If a Cookie does not work for your use case, use Header-based strongly consistent session affinity. Add the istio.io/persistent-session-header label to the Service. The value of the label is the name of the Header that is used for session affinity. Example:
kubectl patch svc httpbin -n default -p '{"metadata":{"labels":{"istio.io/persistent-session-header":"x-session-id"}}}'When you use the Header mode, the client must carry the specified Header in each request.
Comparison
The following table compares the two session affinity modes:
|
Comparison item |
Consistent hashing-based session affinity |
Strongly consistent session affinity |
|
Session stability |
Soft session affinity; some sessions become invalid when endpoints change |
Strong consistency; endpoint changes do not affect existing sessions |
|
Client dependency |
Transparent to clients; no additional configuration is required |
The client must pass a Cookie or Header |
|
Load balancing effect |
Better load balancing |
Long sessions may cause uneven load distribution |
|
Implementation method |
DestinationRule consistentHash |
Service label |
|
Applicable scenarios |
Distributed systems that can tolerate occasional session loss |
Applications that require strict session affinity |