CMD Guide
HomeSystem DesignScalable Systems (Advanced Topics)

What Are Sticky Sessions Session Affinity and When Should I Avoid Them

Sticky sessions, also known as session affinity, is a load balancing technique that ensures all requests from a specific user are consistently routed to the same backend server throughout that user's session.

In simpler terms, the load balancer “sticks” a user to one server so that their session data (like login status or shopping cart) remains intact on that server.

Understanding Sticky Sessions (Session Affinity)

In a typical web application deployed on multiple servers, a load balancer distributes incoming requests across the servers for efficiency.

However, HTTP is a stateless protocol, which means each request is independent and does not remember previous interactions. This can lead to problems if user-specific data is stored in a single server’s memory.

For example, if you’re shopping online and adding items to your cart, you might suddenly find yourself logged out or your cart emptied if a new request gets routed to a different server that doesn’t have your session information.

Without Sticky Sessions
Without Sticky Sessions

Sticky sessions solve this problem by binding a user’s session to one server, ensuring continuity of data and experience.

To achieve session affinity, the load balancer needs a way to identify and remember which server a user is tied to.

Typically, this is done by assigning an identifier to the user, often via a cookie in the user’s browser or by tracking the client’s IP address.

On the user’s first request, the load balancer might set a special session cookie that encodes which server handled the request.

Thereafter, the load balancer reads this cookie on each request and routes the user to the same server that served them initially. (IP-based sticky sessions are another method, but are less reliable if multiple users share an IP or if a single user’s IP changes.)

With Sticky Sessions
With Sticky Sessions

This way, all session data (like your login state or cart contents) stays on one server, and the application doesn’t need to constantly share session information between servers.

An easy way to understand sticky sessions is by analogy: it’s like having a favorite waiter at a restaurant who always serves you and remembers your order.

Just as that waiter can provide personalized, consistent service because they know you, a specific server can handle all of a user’s requests and “remember” their session data.

This improves user experience by avoiding scenarios where information might get lost due to bouncing between different servers.

Benefits of Sticky Sessions

Sticky sessions exist to address real needs in session management.

Some key benefits include:

Drawbacks of Sticky Sessions

Despite their benefits, sticky sessions come with notable drawbacks that can impact scalability and reliability:

When Should You Avoid Sticky Sessions?

Given the above drawbacks, when is it better not to use sticky sessions?

In modern web architecture, the default approach is to design systems that don’t require session affinity unless absolutely necessary. You should consider avoiding sticky sessions in the following scenarios:

In summary, avoid session affinity unless you have a specific requirement for it in a stateful legacy application.

Many organizations initially use sticky sessions for convenience, but later discover it’s better to remove that dependency as they scale.

Alternatives to Sticky Sessions

If you decide to avoid sticky sessions, how do you maintain user session state across a cluster of servers?

Here are some alternative approaches that address the same problem in a more scalable way:

Each of these alternatives allows you to maintain user state without pinning users to a single server, thereby improving the overall scalability and fault tolerance of your application.

They require more initial setup than flipping on sticky sessions, but they are considered best practice for large-scale systems.

Cookie affinity vs IP-hash, and the load-skew trap

The two common ways a load balancer implements affinity fail in different ways. Cookie-based affinity sets a balancer-owned cookie (for example SERVERID=b3) on the first response and reads it on every later request; it is per-browser and precise, but it breaks when a client strips cookies, and the cookie should be marked Secure/HttpOnly so it cannot leak which backend a user is pinned to. IP-hash affinity needs no cookie — it hashes the client IP to a backend — but it collapses every user behind a corporate NAT or carrier-grade NAT (CGNAT) onto a single backend, and it re-pins a mobile client the moment its IP changes.

That NAT collapse is the concrete scalability risk. If a single enterprise or CGNAT egress IP accounts for, say, 15% of all requests, IP-hash pins that entire 15% to one backend; added to that node's fair share of the remaining traffic it can run well over 2× the load of its peers — and adding more backends does not rebalance it, because the hash still maps that one IP to one node. Cookie affinity avoids this because each browser is pinned independently.

Affinity also complicates deploys and scale-in. To remove a backend without dropping carts you must drain it: stop routing new sessions to it, keep serving already-pinned clients until their sessions expire or an affinity timeout elapses, then retire it. Long-lived connections (WebSockets, SSE) make this worse — a client can stay pinned for hours — so at fleet scale you need an explicit drain plan plus an affinity timeout. The combination to never ship is sticky sessions + in-memory-only session state + aggressive auto-scale-in: every scale-in event then silently drops the sessions on the retired node.

Scale-in, traced

4 backends, 100,000 active sessions (~25,000 pinned to each), and node b3 must retire. What happens depends entirely on the affinity mode and where the state lives:

The rule: the affinity mode determines the blast radius of one node's removal — external-store cookie affinity contains it to zero, in-memory contains the damage to one node's sessions but loses them, and naive IP-hash spreads the disruption to the whole fleet.

Drill ladder

L1 — Failure: The LB health check marks a sticky node unhealthy for 30 s, then healthy again. What did the users pinned there experience?
Trap: “Nothing — it came back.”
Bar: During the unhealthy window the LB re-routed them to nodes without their session (logged out / empty cart), and depending on the LB either re-pinned them there or bounced them back afterward — affinity converts a node blip into a user-visible state loss unless state is external.

L2 — Scale: A single CGNAT egress IP carries 15% of your traffic under IP-hash affinity. You add four more backends. What changes?
Trap: “More backends means the hot node's load spreads out.”
Bar: Nothing changes for the hot node's pinned share: the hash still maps that one IP to exactly one backend, so the 15% stays put — adding capacity cannot rebalance a hot NAT IP. The fixes are cookie affinity (per-browser pinning) or removing affinity entirely.

L3 — Cost/Simplicity: Why not just replicate every session to all nodes and skip both affinity and the external store?
Trap: “Session replication solves it.”
Bar: All-node replication is O(N) memory per session and a write-fanout on every session mutation — at fleet scale you pay N copies and N network writes for state that one node reads. An external store is O(1) per session with one network hop; replication only wins for tiny fleets or when that store hop dominates your latency budget.

🤖 Don't fully get this? Learn it with Claude

Stuck on What Are Sticky Sessions Session Affinity and When Should I Avoid Them? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **What Are Sticky Sessions Session Affinity and When Should I Avoid Them** (System Design) and want to truly understand it. Explain What Are Sticky Sessions Session Affinity and When Should I Avoid Them from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **What Are Sticky Sessions Session Affinity and When Should I Avoid Them** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **What Are Sticky Sessions Session Affinity and When Should I Avoid Them** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **What Are Sticky Sessions Session Affinity and When Should I Avoid Them** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes