Definition:
Sticky Session Architecture (also called Session Affinity) is a load balancing technique that routes all requests from a specific user to the same server for the duration of their session. This ensures that session data stored in server memory remains accessible throughout the user’s interaction with the application.
Key Properties:
- User-server binding: Each user “sticks” to one server for the duration of their session
- Session persistence: Session data stays in server memory
- Load balancer routing: Routes based on user identifier (cookie, IP, etc.)
- Stateful servers: Each server maintains user-specific state
- Simple implementation: No need for shared session storage
How It Works:
- User makes first request → Load balancer assigns them to a server
- Load balancer creates “affinity” (remembers the assignment)
- All subsequent requests from that user go to the same server
- Server keeps session data in local memory/cache
Example:
E-commerce website with shopping cart functionality
Initial Setup:
┌─────────────────┐
│ Load Balancer │
│ (with sticky │
│ session map) │
└─────────────────┘
│
┌────┴────┐
│ │
┌───▼───┐ ┌───▼───┐ ┌─────────┐
│Server A│ │Server B│ │Server C │
│ │ │ │ │ │
└────────┘ └────────┘ └─────────┘
Step 1: User's first request
User (session_id: abc123) → Load Balancer → Server A
Load Balancer remembers: abc123 → Server A
Step 2: User adds iPhone to cart
Request → Server A (stores cart: [iPhone])
Server A's memory: {abc123: {cart: [iPhone]}}
Step 3: User adds iPad to cart
Request (session_id: abc123) → Load Balancer → Server A (same server!)
Server A's memory: {abc123: {cart: [iPhone, iPad]}}
Step 4: User proceeds to checkout
Request (session_id: abc123) → Load Balancer → Server A
Server A has complete cart history: [iPhone, iPad]