Skip to content

Session Affinity

Session Affinity (or Sticky Session) is a technique that routes requests from the same user to the same pod.

Table of Contents

  1. Session Affinity Overview
  2. Consistent Hash Based
  3. Cookie Based
  4. Header Based
  5. Practical Examples

Session Affinity Overview

Consistent Hash Based

HTTP Header Based

yaml
apiVersion: networking.istio.io/v1
kind: DestinationRule
metadata:
  name: reviews-session-affinity
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      consistentHash:
        httpHeaderName: "x-user-id"
yaml
apiVersion: networking.istio.io/v1
kind: DestinationRule
metadata:
  name: reviews-cookie-affinity
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      consistentHash:
        httpCookie:
          name: "session-id"
          ttl: 0s  # Cookie expiration time (0s = session cookie)

Source IP Based

yaml
apiVersion: networking.istio.io/v1
kind: DestinationRule
metadata:
  name: reviews-ip-affinity
spec:
  host: reviews
  trafficPolicy:
    loadBalancer:
      consistentHash:
        useSourceIp: true

References