Quick revision
The short answer
We have three main options: sticky sessions on the load balancer, an external session store such as Redis through Spring Session, or removing server-side sessions in favour of stateless tokens. For most teams Spring Session with Redis is the best first move because it needs almost no code change, and whichever we pick, session contents should be small and serialisable.
What to remember
In this answer, we saw that in-memory sessions fail on Kubernetes because requests move between pods and pods are disposable. Sticky sessions hide the problem but lose sessions on restarts and unbalance load, Spring Session moves sessions to Redis or a database with almost no code change, and stateless tokens remove sessions at the price of harder revocation. In every case, small serialisable sessions make the system faster and safer.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We have three main options: sticky sessions on the load balancer, an external session store such as Redis through Spring Session, or removing server-side sessions in favour of stateless tokens. For most teams Spring Session with Redis is the best first move because it needs almost no code change, and whichever we pick, session contents should be small and serialisable.
Why this question is asked
A Spring MVC application that keeps data in HttpSession works perfectly on one server, because the session lives in that server's memory. Kubernetes changes the picture: we run several replicas, pods are restarted during deployments, and the autoscaler adds and removes pods. A request can land on any pod, and a pod can disappear at any time. Interviewers ask this design question to see whether we understand where session state lives and what each option costs in operations and security. In this answer, we will see why in-memory sessions break, look at sticky sessions, Spring Session and stateless tokens in turn, compare them, and finish with the rules that apply to all of them.
1Why In-Memory Sessions Break With Several Replicas
By default Tomcat keeps each HttpSession in a map inside the JVM. With three replicas, a user who logs in on pod A has a session only in pod A's memory. If the Kubernetes Service sends the next request to pod B, pod B finds no session for that JSESSIONID and the user looks logged out.
Even with one replica, a rolling deployment replaces the pod, and every in-memory session goes with it. So the problem is not only routing; it is that the state is tied to a process that Kubernetes treats as disposable.
2Option 1: Sticky Sessions
Sticky sessions, also called session affinity, make the load balancer send all requests from one user to the same pod. With an NGINX ingress we can use cookie-based affinity; a plain Kubernetes Service offers sessionAffinity: ClientIP. No application change is needed, which is why it is tempting.
But there is a catch. When that pod restarts during a deployment or crashes, its users lose their sessions. Load spreads unevenly, because new pods added by the autoscaler only receive new users while old pods keep their existing users. And draining a pod takes as long as the longest session. Sticky sessions are fine as a short bridge, but they keep the fragile part of the design.
3Option 2: Spring Session With Redis or a Database
Spring Session replaces the container's session store with an external one. It installs a servlet filter that wraps the request, so request.getSession() returns a session whose data lives in Redis, a JDBC database or Hazelcast. Our controllers keep calling getSession() and setAttribute() exactly as before; they do not know the store changed.
Any pod can now serve any request, because they all read the same store. Pods can restart freely, and the autoscaler works as intended. The cost is one network round trip to Redis per request that touches the session (usually under a millisecond in the same cluster) and a new piece of infrastructure to run and secure.
7 messages in order. Play them, or step through them one at a time.
Any pod can serve the user because the session lives in Redis.
Notice that the session is now serialised every time it is saved. Every object we put in it must be serialisable, either with Java serialization (implementing Serializable) or with a JSON serializer we configure. Spring Boot picks the store from the dependency on the classpath; check your version's documentation for the exact property names if a store needs extra settings.
4Option 3: Stateless Tokens
The third option removes server-side sessions entirely. After login the server issues a signed token, often a JWT, that carries the user's identity and roles. The browser sends it with each request and any pod verifies the signature without looking anything up.
This scales well, but it moves problems rather than removing them. A signed token is valid until it expires, so revoking it (when a user logs out or an account is disabled) needs short lifetimes plus refresh tokens, or a deny list, which brings back shared state. Tokens also cannot hold a large cart. And converting an application built around HttpSession means rewriting code, not changing a dependency.
5Comparing the Options and Choosing
For a team moving an existing application, I would start with Spring Session on Redis. It gives real horizontal scaling with a dependency and a few properties, and it keeps the security model the team already understands. Sticky sessions are acceptable for a few weeks while Redis is set up. Stateless tokens make sense for new APIs or when the team is already moving authentication to an identity provider.
| Option | Code change | Survives pod restart | Main cost |
|---|---|---|---|
| Sticky sessions | None | No | Uneven load, lost sessions on deploy |
| Spring Session + Redis | Dependency and config | Yes | Redis to run; serialisation |
| Spring Session + JDBC | Dependency and config | Yes | Database load per request |
| Stateless tokens | Rewrite auth and state | Yes | Revocation and token size |
Whichever option we choose, keep session data small and serialisable. Store ids, not whole entity graphs: a user id and a cart id instead of a User object with its orders. A 2 KB session costs little to send to Redis on every request; a 500 KB session full of cached search results makes every request slow and every deployment risky when class shapes change.
6Follow-up questions
What happens to sessions already in memory when we switch to Spring Session?
They are not migrated. On the first deployment with Spring Session, users with old in-memory sessions will need to log in again, and Spring Session also uses its own cookie name (SESSION by default). Plan the switch for a quiet period and tell users, or keep the old cookie name if a smooth change matters.
Why not use Tomcat's own session replication?
Tomcat clustering replicates sessions between nodes through multicast or static membership. It is awkward in Kubernetes, where pod addresses change constantly, and every node holds every session. An external store is simpler to operate and scales independently of the application.
How do we keep Redis from becoming a single point of failure?
Run Redis with replication and automatic failover, for example a managed Redis service or Redis Sentinel, and set sensible connection timeouts in the application. If Redis is briefly down, requests that need the session fail, so monitor it like any other critical dependency.
7Conclusion
In this answer, we saw that in-memory sessions fail on Kubernetes because requests move between pods and pods are disposable. Sticky sessions hide the problem but lose sessions on restarts and unbalance load, Spring Session moves sessions to Redis or a database with almost no code change, and stateless tokens remove sessions at the price of harder revocation. In every case, small serialisable sessions make the system faster and safer.
Open with
The core problem is that HttpSession lives in one pod's memory, and on Kubernetes the next request can go to another pod and pods get replaced on every deployment.
Then, if they want more
I see three options. Sticky sessions on the ingress need no code change, but users lose their session when their pod restarts, and load spreads badly when the autoscaler adds pods, so I would use them only as a short bridge. My usual choice is Spring Session with Redis.
It plugs in as a filter, so controllers keep calling getSession, but the data lives in Redis and any pod can serve any user. It costs a small round trip per request and Redis has to be run with failover.
The third option is stateless tokens such as JWTs, which scale very well but make revocation harder, since a token is valid until it expires, so I would need short lifetimes and refresh tokens. That is more of a rewrite and fits new APIs better.
Whatever we choose, I would shrink the session to ids rather than whole objects and make everything in it serialisable, because every request now pays to read and write it.
About this answer
- Last updated
- 12 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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
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 "You are moving a stateful Spring MVC application with HttpSession data to Kubernetes...": https://scrivio-interview-library.netlify.app/java/servlets/3d799b0c43/