Skip to main content

Sticky Sessions Explained: The Story of Two Waiters (Load Balancer Session Affinity)

Updated 8 min readMarkdown

By Devang Pandya · Cloud Tech by Devang · Cloud • DevOps • AI — Made Simple

This post is the full breakdown for the Two Waiters Reel. If you came here after commenting STICKY, welcome!

The story in the Reel: Sharma ji runs a busy dhaba with two waiters. Raju is running between six tables. Pappu is standing free with zero tables. Sharma ji asks, "Why?"

Raju says, "Sir, everyone's orders are in my notebook!" And sure enough, Neha at table 4 calls out, "Raju bhaiya! Ek aur butter naan!" Pappu is right next to her, but she won't ask him, because only Raju knows her order.

Then Sharma ji has a worse thought: "What if Raju goes home... and takes the notebook with him?" Every order is gone, and every customer has to order again.

That is exactly what sticky sessions do in a load-balanced system. Below is the real tech, the problems it causes, the better design and how to answer it in an interview.


1. What is a sticky session (session affinity)?

A load balancer normally spreads requests across a pool of servers. With sticky sessions (also called session affinity), the load balancer makes sure that every request from the same user goes to the same server for the life of the session.

Why would anyone want that? Because the app keeps the user's session state in the memory of one server:

  • login state (who you are)
  • shopping cart
  • multi-step form or checkout progress
  • in-memory caches tied to that user

If the next request went to a different server, that server wouldn't have the session, and the user would look logged out or see an empty cart. So the load balancer "sticks" the user to the server that has their data.

Dhaba System
Raju and Pappu Server A and Server B
Customer at a table User / client
Raju's notebook Session data in Server A's memory
"Only Raju knows my order" Session affinity
Raju goes home with the notebook Server crash: sessions lost
Kitchen board Shared session store (Redis / DB)

There are two common ways a load balancer decides that "this user belongs to Server A".

On the first response, the load balancer (or the app) sets a cookie that identifies the server. On every later request, the browser sends the cookie back, and the load balancer routes to that server.

1st request:   User ──> LB ──> Server A
               User <── LB <── Server A   Set-Cookie: srv=A
next requests: User ──(Cookie: srv=A)──> LB ──> Server A  (always)
  • Load-balancer-generated cookies: for example AWS ALB's AWSALB cookie, or NGINX sticky cookie.
  • Application cookies: the LB follows a cookie the app already sets (such as a session ID).
  • A cookie usually has a duration / TTL, after which the user can be balanced again.

Pros: precise per-user stickiness, works even when many users share one IP. Cons: only works for clients that keep cookies (browsers, not always APIs or mobile SDKs).

IP hash (source-IP affinity)

The load balancer hashes the client's IP address and maps the hash to a server, for example NGINX ip_hash or a Layer 4 "source IP" affinity.

Pros: no cookies, works for any TCP traffic. Cons: thousands of users behind one corporate NAT or mobile carrier IP all land on the same server, and a user whose IP changes (Wi-Fi to 4G) loses their server and their session.

3. The problems with sticky sessions

Problem 1: uneven load

Stickiness overrides the balancing algorithm. Heavy users, long sessions or one big NAT pool stay pinned to one server. That's Raju with six tables and Pappu with zero: one server at 99% CPU while another sits at 2%.

Autoscaling makes it worse. When you add new servers, existing users stay stuck to the old ones, so the new capacity barely helps until sessions expire.

Problem 2: failover loses the session

If the session only lives in Server A's memory and Server A crashes, gets redeployed or is scaled in, every session on it is gone. Users are logged out, carts are empty, and half-filled forms disappear. That's Raju going home with the notebook.

Problem 3: harder deployments and scaling

  • Rolling deployments must drain each server and wait for sticky users to leave.
  • Scale-in can't simply kill an instance without hurting users on it.
  • Debugging gets harder because behaviour depends on which server a user is pinned to.

4. The better approach: stateless servers + a shared session store

Sharma ji's fix is simple: "From today, every order goes on the kitchen board. Any waiter can serve any table."

In system terms: keep the application servers stateless and move session state out of server memory into a shared store that every server can read.

            ┌──────────── Load Balancer (round robin / least connections) ────────────┐
            ▼                              ▼                                          ▼
       App server A                  App server B                               App server C
       (stateless)                   (stateless)                                (stateless)
            └──────────────────────────────┬──────────────────────────────────────────┘
                                           ▼
                           Shared session store: Redis / DB

Common options:

  • Redis / Memcached (for example Amazon ElastiCache): fast, in-memory, with TTL per session key. The most common choice.
  • A database (DynamoDB, PostgreSQL): durable, a bit slower, good when sessions must survive anything.
  • Stateless tokens (JWT): the session data travels in a signed token, so the server stores nothing. Remember to plan for revocation and token size.

What you gain:

  • Even load: any server can take any request, so the load balancer can truly balance.
  • Safe failover: if a server crashes, the next request goes to another server, which reads the session from Redis. Nothing is lost.
  • Easy scaling and deploys: add or remove servers freely, with no draining of sticky users.

Trade-offs to mention: one extra network hop per request (usually under a millisecond with Redis in the same region), and the session store itself must be highly available (Redis replication / cluster mode, Multi-AZ).

5. When sticky sessions are still OK

Sticky sessions aren't "wrong", they're a tool with a cost. Use them when:

  • Legacy apps keep session state in memory and can't be changed quickly.
  • WebSockets or other long-lived connections, where the connection itself lives on one server (and some protocols, like Socket.IO's polling fallback, need affinity during the handshake).
  • Local caches where hitting the same server gives a much better cache hit rate.

Even then, keep a short TTL on the affinity cookie, enable connection draining, and don't rely on stickiness for correctness: plan for the server going away.

6. How to answer in an interview

Q: What are sticky sessions, and would you use them?

"Sticky sessions, or session affinity, mean the load balancer sends all requests from one user to the same server, usually with a cookie or an IP hash, because the session lives in that server's memory. It's simple, but it causes uneven load, and when that server crashes or is scaled in, those users lose their sessions. My default is stateless app servers with session state in a shared store like Redis or a database, so any server can handle any request, and failover and autoscaling just work. I'd only use sticky sessions for legacy apps or WebSockets, with a short TTL and connection draining."

Follow-up questions to be ready for:

  • Cookie vs IP hash? Cookie is precise per user; IP hash breaks behind NAT and when IPs change.
  • How does AWS ALB do it? Target-group stickiness with a duration-based LB cookie (AWSALB) or an application-based cookie.
  • What if Redis goes down? Replication and automatic failover (ElastiCache Multi-AZ), sensible timeouts, and a fallback such as re-authentication.
  • JWT vs server-side sessions? JWT removes the store but makes revocation harder; server-side sessions in Redis are easy to revoke.

One-line summary: sticky sessions pin a user to one server; the better default is stateless servers plus a shared session store.


Preparing for a Cloud, DevOps or SRE interview? With 14+ years in Cloud & DevOps, I help engineers get interview-ready. Let's connect on a Free 15-min Cloud & DevOps Interview Guidance Call: what to study, how to answer system-design questions like this one, plus a CV and roadmap check 👉 topmate.io/cloudtechbydevang

Follow @cloudtechbydevang on Instagram for more everyday tech, simply explained.

About the author

  • I help engineers learn cloud, DevOps, SRE and AI in simple English, crack interviews and follow clear roadmaps. 14+ years building and running platforms on AWS, GCP, Kubernetes and Terraform. One topic a day.

No comments yet. Be the first to comment.