Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

Sticky sessions, or session affinity, make a load balancer send all requests from one client to the same backend, usually so that server-side session data held in memory is found again.

How do load balancers implement stickiness, and what happens to a sticky user when their backend is removed?

Load balancers implement stickiness either with a cookie, their own cookie that names the backend or an application cookie such as JSESSIONID that they follow, or with a hash of the client IP address. Stickiness lasts for a set duration, and once it expires or the cookie is lost the user is balanced again. When the user's backend is removed by a deploy, scale-in or failure, the balancer sends them elsewhere and any session held in that server's memory is lost, so stickiness gives affinity, not durability.

Why this question is asked

Sticky sessions are one of those features that make an application work on day one and cause strange bugs on day one hundred. Users get logged out during deploys, carts empty themselves, and load spreads unevenly. Interviewers ask about it to see whether we understand what the balancer is really promising. In this answer, we will recap why stickiness exists, look at the cookie and address-based implementations, see what duration means, follow a sticky user whose backend disappears, and end with where session data should really live.

1A Quick Recap: Why Stickiness Exists

A classic Java web application stores session data, such as the logged-in user or a shopping cart, in an HttpSession held in the memory of the server that created it. The client only carries a session id, usually in the JSESSIONID cookie. If the next request goes to a different server, that server has no session with that id, and the user looks logged out.

Sticky sessions, also called session affinity, solve this by making the balancer send every request from the same client to the same backend. The application stays unchanged, which is why stickiness is so tempting.

2Cookie-Based Stickiness

The most common method uses a cookie. In the first form, the balancer inserts its own cookie that names or encodes the backend. On the first request it picks a backend normally, and in the response it adds a cookie such as SERVERID=app2. Later requests carry the cookie, and the balancer routes them to app2. Cloud balancers do the same with a generated cookie, for example AWSALB on an AWS Application Load Balancer. In the second form, the balancer follows an application cookie, such as JSESSIONID, and remembers which backend issued it, or the application appends a route suffix to the session id that the balancer reads.

Java
backend web
balance roundrobin
cookie SERVERID insert indirect nocache # balancer adds SERVERID to the response
server app1 10.0.1.11:8080 check cookie app1
server app2 10.0.1.12:8080 check cookie app2
# first response: Set-Cookie: SERVERID=app2
# next request: Cookie: SERVERID=app2 -> routed to app2
# indirect: the cookie is removed before the request reaches the application

Cookie stickiness follows the browser, not the network, so it works for users behind shared addresses and survives a phone moving from Wi-Fi to mobile data. Its limits are that clients must keep cookies, which some API clients do not, and that it only works at layer 7, where the balancer reads HTTP.

3Address-Based Stickiness

The second method needs no cookie. The balancer hashes the client's IP address and uses the hash to pick a backend, so the same address always lands on the same server. In NGINX this is the ip_hash directive. It works for any client, including those that ignore cookies, and even at layer 4.

But it is coarse. The balancer sees the address that reaches it, which may be a corporate NAT gateway or a mobile carrier gateway shared by thousands of users, and all of them land on one backend. And when a user's address changes, for example a phone switching networks, the hash changes and the user moves to another server, losing their session anyway.

Ways to implement stickiness
MethodHow it picks the backendMain weakness
Balancer cookie (insert)cookie names the backendclient must keep cookies
Application cookiefollows JSESSIONID or a route suffixapp and balancer must agree
Source IP hashhash of client addressNAT users pile up; address changes move users
Cookies follow the browser, IP hashes follow the network address.

4Duration: Stickiness Expires

Stickiness is not forever. A balancer cookie has a lifetime, for example one hour or one day, and when it expires the next request is balanced like a new one. A user who clears cookies, opens a private window or uses a client that drops cookies is also balanced again. Choosing the duration is a trade-off: long enough to cover a typical session, short enough that load rebalances over time. A one-day stickiness means a user's backend choice from this morning still decides their traffic tonight.

5What Happens When the Backend Disappears

Now the important case. The sticky user's backend is removed: a rolling deploy replaces it, autoscaling scales in, or the instance crashes. The cookie still says app2, but app2 is no longer in the pool, so the balancer picks another backend, typically sets a new cookie, and the user continues there. Routing recovers immediately. The session data does not: it lived in app2's memory, and app2 is gone. The user is logged out, the cart is empty, and a multi-step form starts again.

Connection draining softens the deploy case. When a backend is being removed, the balancer stops sending it new connections but lets existing requests finish for a drain period, for example 30 seconds to 5 minutes. That protects requests in flight, but it cannot keep a session alive after the server stops, unless the session times out on its own first.

Step throughA sticky user whose backend is removed
BrowserBalancerapp2app3
Cookie: SERVERID=app2
route to app2 (cart in memory)
deploy stops app2
Cookie: SERVERID=app2
app2 gone, pick app3
no session: logged out, empty cart

6 messages in order. Play them, or step through them one at a time.

The balancer recovers routing; the in-memory session is lost.

So the lesson is that stickiness gives affinity, not durability. It keeps a user on the same server while that server exists. Anything that must survive a deploy or a crash has to live in a shared store, such as Redis through Spring Session, or in a signed token the client carries. Once sessions are external, stickiness becomes an optional performance optimisation rather than a correctness requirement.

6Follow-up questions

What does connection draining do for sticky users during a deploy?

Draining, called deregistration delay on some balancers, stops new connections to the instance being removed while letting in-flight requests finish, for a set time such as 300 seconds. Some balancers also keep sending requests from existing sticky sessions to a draining target during that window. That gives in-progress work a chance to complete, but once the instance stops, its in-memory sessions are gone. Draining reduces failed requests; it does not preserve sessions.

If we use Spring Session with Redis, do we still need stickiness?

Not for correctness. Every instance can read any session from Redis, so the balancer can spread every request freely and deploys lose nothing. Some teams keep a short stickiness to improve cache locality, but it is optional, and turning it off is what lets new instances take load immediately.

7Conclusion

In this answer, we saw that stickiness keeps a client on one backend so in-memory session data is found again. Balancers do it with their own cookie, by following an application cookie, or by hashing the client IP, each with its weaknesses. Stickiness has a duration, after which the user is balanced again. When the backend is removed, routing recovers but the in-memory session is lost, so stickiness gives affinity, not durability, and important session data belongs in a shared store.

What to say in the interviewAbout 80 seconds aloud

Open with

Sticky sessions make the balancer send every request from one client to the same backend, usually because the session lives in that server's memory. There are two main ways to do it.

Then, if they want more

With cookies, the balancer either inserts its own cookie naming the backend, like HAProxy's cookie insert mode or the AWSALB cookie on an AWS load balancer, or it follows an application cookie such as JSESSIONID.

With addresses, it hashes the client IP, like NGINX ip_hash, which needs no cookie but puts everyone behind one NAT address on one server and moves users whose address changes. Stickiness always has a duration, and when it expires or the cookie is lost, the user is balanced again.

The interesting case is when the backend goes away in a deploy, a scale-in or a crash. The balancer simply picks another server, so routing recovers immediately, but the session in the old server's memory is gone, so the user is logged out or loses their cart. Connection draining only lets in-flight requests finish.

So I would say stickiness gives affinity, not durability: anything that matters goes into a shared store like Redis via Spring Session, and then stickiness becomes an optional optimisation.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do load balancers implement stickiness, and what happens to a sticky user when their...": https://scrivio-interview-library.netlify.app/architecture/load-balancing-networking/caa6e72ce6/