Video in progress

What is a sticky session and what are its drawbacks?

A sticky session means the load balancer always sends all requests from one user to the same server (usually based on a cookie). It lets you keep the session in server memory, but the load becomes uneven, and if the server dies, its users lose their sessions.

📖 Answer

Sometimes you need to quickly scale out an old application to several servers while the session sits in HttpSession. Sticky sessions help there: they work without changing the code.

But they come with plenty of problems:

  • "heavy" users can pile up on one server;
  • when a server restarts or is redeployed, those users get logged out;
  • a newly added server only gets new users, while the old ones stay on the old servers.

So treat it as a temporary fix. The right way is to move the session to a shared place:

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

After that the session lives in Redis and it no longer matters which server a request lands on.