Skip to the content.

1️⃣ What is a Sticky Session?

A sticky session (also called session affinity) is a load balancing technique where all requests from a particular client are routed to the same server for the duration of a session.

Goal: Keep the session consistent and avoid losing user-specific data.


2️⃣ How Sticky Sessions Work

  1. Client makes a request → hits load balancer
  2. Load balancer routes request to a server (e.g., Server A)
  3. Load balancer remembers that client X → Server A (usually via cookie or IP)
  4. All subsequent requests from the same client are routed to Server A
  5. Session is preserved without requiring shared storage

Mechanisms to Implement Sticky Sessions

Method Description
Cookie-based Load balancer sets a cookie identifying the server (e.g., SERVERID=A)
IP-based Routes requests based on client IP (less reliable for dynamic IPs)
Session token Token in the URL or header used to identify the server

3️⃣ Example


4️⃣ Pros and Cons

Pros

Cons


5️⃣ Sticky Sessions vs Stateless Architecture

Feature Sticky Session Stateless
Session storage Stored on specific server Stored in DB / cache (Redis)
Scalability Limited (depends on server load) Highly scalable
Fault tolerance Low (server failure loses session) High (any server can handle request)

6️⃣ Quick Analogy