Sticky Sessions

Cookie-based affinity, source IP hashing, JSESSIONID pinning, and how sticky sessions conflict with horizontal scaling.

Overview

Overview Sticky sessions (also called session affinity) ensure that a client's requests are consistently routed to the same backend server. This is necessary when server-side session state is stored in-process (in memory) rather than in a shared external store. If a subsequent request lands on a different server, that server has no knowledge of the session and the user experiences an authentication failure or lost cart.

Cookie-based affinity is the most common implementation. The load balancer inserts a cookie (e.g., AWSALB on AWS ALB) into the first response. On subsequent requests, the load balancer reads this cookie and routes to the pinned backend.

Source IP hashing is an alternative that requires no cookie insertion. The client IP is hashed to select a backend. This is weaker: clients behind NAT share one IP and all land on the same backend. IP changes (mobile roaming, VPN) break the affinity.

The fundamental problem with sticky sessions is that they undermine horizontal scaling. If a pinned backend is removed (deployment, failure, scale-in), all its pinned clients lose their session. Sticky sessions also complicate blue-green and canary deployments.

The correct long-term solution is to externalize session state to a shared store (Redis, DynamoDB) so any backend can serve any request. Sticky sessions should be treated as a temporary workaround, not an architecture goal.

Key concepts

  • Cookie-Based Affinity
  • Source IP Hashing
  • JSESSIONID Pinning
  • Externalized Session State

Interview questions

  • How do you enable sticky sessions on an AWS ALB, and what are the tradeoffs?
  • A team complains users are being logged out during deployments. What causes this and how do you fix it?

Sign up free to read the full topic

More Networking topics

Start free on Cariara · Coding problems · System design studio