Course contentsShow
← Cloud and Infrastructure
Lesson 278 of 2,9895. Load Balancing and Traffic ManagementFree lesson

Session Affinity and Sticky Sessions

Configure session persistence to ensure clients are routed to the same backend for stateful applications.

Session Affinity and Sticky Sessions

What you'll learn: How to configure session persistence so returning clients always reach the same backend server, keeping their stateful sessions intact.


What Are Sticky Sessions?

Imagine visiting a help desk where Agent A knows your entire case history. If you're randomly redirected to Agent B on your next visit, you'd have to explain everything again. Session affinity (also called sticky sessions) solves this by ensuring a client consistently reaches the same backend server for the duration of their session.

Load balancers normally distribute requests across multiple servers for efficiency. But some applications store user data temporarily on a specific server (shopping carts, login sessions, uploaded files). Without stickiness, each request might hit a different server that doesn't have your data—breaking the user experience.


How It Works

When session affinity is enabled, the load balancer uses a tracking mechanism to remember which backend handled a client's first request. Common methods include:

  • Cookie-based stickiness: The load balancer inserts a special cookie (AWSALB for ALBs) in the response, then reads it on subsequent requests to route to the same target.
  • Duration-based: You set how long (e.g., 1 hour) the affinity lasts.
  • Application cookie: Use your app's own session cookie as the sticky identifier.

Configuration Essentials

For stateful applications, enable sticky sessions at the target group level. Specify:

  • Stickiness type (load balancer-generated or application-based)
  • Duration (1 second to 7 days)

This ensures users don't lose their cart mid-checkout or get logged out unexpectedly.


Key Takeaway:
Session affinity routes clients to the same backend server using cookies, preventing stateful data loss—critical for shopping carts, user sessions, and any app that stores temporary data server-side.