Load Balancing Session Affinity Policy in ALB

This guide explains the various load balancing algorithms available in ALB and how to configure them to optimize traffic distribution across your application pods.

Overview

ALB supports multiple load balancing algorithms to distribute incoming traffic across backend pods. The choice of algorithm depends on your application requirements, such as session persistence, performance optimization, or even distribution of load.

Available Algorithms

Round Robin (Default)

  • Policy: rr
  • Description: Distributes requests sequentially across all available pods in a circular order.
  • Use Case: Best for stateless applications where each request can be handled by any pod.

Source IP Hash

  • Policy: sip-hash
  • Description: Consistently routes requests from the same source IP address to the same pod.
  • Behavior: Uses the first IP in X-Forwarded-For header if present, otherwise uses the client's source IP.
  • Use Case: Useful when client IP-based session persistence is required.
  • Policy: cookie
  • Attribute: cookie-name (defaults to JSESSIONID)
  • Description: Routes requests with the same cookie value to the same pod.
  • Behavior:
    • If the specified cookie is not present, ALB adds it to the response
    • Cookie format: timestamp.worker_pid.random_number
  • Use Case: Ideal for applications requiring session stickiness based on cookies.

Header-Based Affinity

  • Policy: header
  • Attribute: header-name
  • Description: Routes requests with the same header value to the same pod.
  • Use Case: Suitable for applications that need routing based on specific HTTP headers.

EWMA (Exponentially Weighted Moving Average)

  • Policy: ewma
  • Description: Routes traffic based on pod response times using the Power of Two Choices (P2C) algorithm with EWMA.
  • Behavior:
    • Maintains an EWMA score for each pod based on response times
    • Routes traffic to pods with lower EWMA scores
    • Scores decay exponentially over time
  • Use Case: Optimal for latency-sensitive applications
  • Reference: Twitter Finagle EWMA Documentation

Configuration Methods

1. Using Ingress Annotations

annotations:
  alb.ingress.cpaas.io/session-affinity-policy: "<algorithm>"  # rr | sip-hash | cookie | header | ewma
  alb.ingress.cpaas.io/session-affinity-attribute: "<attribute>"  # Required for cookie and header policies

2. Using ALB Frontend/Rule Custom Resource

spec:
  serviceGroup:
    session_affinity_policy: "<algorithm>"  # rr | sip-hash | cookie | header | ewma
    session_affinity_attribute: "<attribute>"  # Required for cookie and header policies

Best Practices

  • Choose Round Robin for stateless applications with uniform request patterns
  • Use Source IP Hash when client IP-based session persistence is required
  • Implement Cookie-based affinity for web applications requiring session stickiness
  • Consider EWMA for services with varying response times to optimize latency