Sticky sessions

Topic 5 of 8 in Week 2: Scaling, traffic and estimation8 min readBeginnerFree course

Overview

If an app keeps session data in a server’s memory, every request from that user must go back to the same server. Load balancers can do that with sticky sessions, also called session affinity. It’s a useful tool, but often a sign that state is in the wrong place. This lesson explains how it works and when it’s justified.

How stickiness works

Sticky sessions. Asha’s requests carry the cookie srv=A and Ben’s carry srv=C. The load balancer reads the cookie and always sends Asha to Server A, which holds her session, and Ben to Server C, which holds his. Server B serves other users. Stickiness helps with in-memory sessions, local caches and WebSocket reconnects; it hurts through uneven load, lost sessions when a server dies and slower draining during deploys.

The balancer needs a way to recognise a user and remember their server:

  • Balancer cookie: on the first response, the balancer adds a cookie naming the chosen server (for example srv=A or an encoded ID). Later requests carry it, and the balancer routes accordingly. This is the most common approach for HTTP.
  • Application cookie: the balancer maps an existing session cookie (such as JSESSIONID) to a server.
  • IP hash: route by a hash of the client’s IP address. It needs no cookies but breaks when many users share one IP (offices, mobile carriers) or when a user’s IP changes as they move between networks.
  • Hash of a user or key: route by user ID in a header or path; useful between internal services.

Stickiness usually has a duration: after the cookie expires, the user may be balanced to a different server.

Why teams use it

  • Legacy apps that keep sessions or carts in memory and can’t be changed quickly.
  • Local caches: sending a user to the same server makes per-server caches more effective.
  • Multi-step workflows that keep temporary data on one server, such as chunked uploads.
  • Long-lived connections: WebSockets and streaming connections stay on one server by nature.

What it costs

  • Uneven load. If a few heavy users land on one server, it stays overloaded even when you add servers, because existing users don’t move.
  • No protection from failure. If the server dies, its sessions die with it. Stickiness only hides the in-memory state problem while everything is healthy.
  • Slower deploys and scale-in. To remove a server gracefully you must wait for its sticky users to finish, or accept that they lose their sessions.
  • Autoscaling helps less. New servers only get new users; the existing hot spots remain.
  • Shared IPs and NAT skew IP-hash routing badly.

The better default

For most web services, make app servers stateless (week 1) and keep sessions in a shared store such as Redis or in signed tokens. Then:

  • any server can serve any request,
  • servers can die or be replaced without users noticing,
  • load spreads evenly and autoscaling works.

If you still want the cache benefits of affinity, treat it as soft affinity: route by user ID when convenient, but make sure every server can handle every user correctly, just a little slower.

Long-lived connections are different

A WebSocket connection stays on the server that accepted it for its whole life, so “stickiness” is built in. The design questions become:

  • How does a message for user X reach the server holding X’s connection? (A registry such as Redis mapping user → gateway, plus a pub/sub channel.)
  • What happens when that server dies? (Clients reconnect, to any server, and the registry is updated.)

You’ll build exactly this in the chat case study.

In an interview

If you draw stateless servers, say so, and you won’t need stickiness. If the interviewer asks about it, explain the trade-off in one breath: “Sticky sessions would let us keep sessions in memory, but a server failure would log those users out and load would be uneven, so I’d rather store sessions in Redis.”

Recap

Sticky sessions route each user to the same server using a cookie or a hash. They make in-memory state work while servers are healthy, at the cost of uneven load, fragile failover and slower deploys. Prefer stateless servers with shared session storage, and use affinity only as an optimisation or for long-lived connections.

Exercise

Sticky or not?

For each system, say whether you would use sticky sessions and why: (a) a REST API for a shopping site where sessions are stored in Redis, (b) a WebSocket chat gateway where each server holds open connections, (c) an old monolith that stores the shopping cart in server memory and can't be changed this quarter, (d) a service where each server keeps a large local cache of user profiles to cut database reads.

Text
(a)
(b)
(c)
(d)

Summary

  • Sticky sessions (session affinity) send all of a user's requests to the same server.
  • Balancers implement stickiness with a cookie they set, an existing session cookie, or a hash of the client IP or user ID.
  • Stickiness is a workaround for in-memory state; it doesn't protect that state when a server dies.
  • Costs: uneven load, lost sessions on failure, slower deploys and scale-in, and trouble with clients behind shared IPs.
  • Prefer stateless servers; use affinity only as a performance optimisation or for long-lived connections.