Quick revision
The short answer
Users are logged out because each Tomcat instance keeps HttpSession objects in its own memory, so a request that the load balancer sends to another instance finds no session. Spring Session fixes this by replacing the container's session with one stored in a shared store such as Redis, through a servlet filter, without changing application code. We add spring-session-data-redis and a Redis connection, and Spring Boot configures the rest; we usually set the timeout and a namespace.
What to remember
In this answer, we saw that a servlet session lives in the memory of one instance, so four instances behind a load balancer lose it whenever a request lands elsewhere, and sticky sessions only hide that. Spring Session solves it with SessionRepositoryFilter, which swaps the storage behind HttpSession for a shared repository. With Boot we add spring-session-data-redis and a Redis connection, set spring.session.timeout and a namespace per application, make attributes serializable or register a JSON serializer, and keep the JDBC store or stateless tokens in mind as alternatives.
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.
Users are logged out because each Tomcat instance keeps HttpSession objects in its own memory, so a request that the load balancer sends to another instance finds no session. Spring Session fixes this by replacing the container's session with one stored in a shared store such as Redis, through a servlet filter, without changing application code. We add spring-session-data-redis and a Redis connection, and Spring Boot configures the rest; we usually set the timeout and a namespace.
Why this question is asked
Scaling from one instance to several is often the moment a web application meets its first distributed-systems bug, and session loss is the classic one. Interviewers ask it because the answer shows whether we understand where a session actually lives and how Spring can swap that storage without touching our controllers. In this answer, we will recap how a servlet session works, see why four instances break it, look at how Spring Session plugs in a shared store, configure Redis step by step, and cover serialization and the alternatives.
1A Quick Recap of HttpSession
HTTP itself is stateless: each request stands alone. To remember a user between requests, the servlet container creates a session, an object that holds attributes such as the logged-in user or a shopping cart. The container sends the browser a cookie, JSESSIONID by default, with the session id, and on every later request it uses that id to find the session again.
Notice where the session is kept: in a map inside the memory of that one Tomcat process. With a single instance this is invisible. Spring Security, for example, stores the SecurityContext, the object that says who is logged in, as a session attribute, so the user stays logged in as long as the session is found.
2Why Four Instances Log Users Out
Behind a load balancer, the first request may go to instance A, which creates the session and stores the login. The next request may go to instance C. Instance C receives the cookie, looks up the id in its own memory, finds nothing, and treats the user as anonymous. To the user it looks random, because it depends on which instance each request happens to hit.
Sticky sessions, where the load balancer sends a user always to the same instance, only hide the problem. When that instance is restarted during a deployment or removed by autoscaling, all its users are logged out at once, and the load is spread unevenly because heavy users stay pinned to one node.
8 messages in order. Play them, or step through them one at a time.
Instance C has never heard of session 9f3, so the user appears logged out.
3How Spring Session Replaces the Container Session
Spring Session registers a servlet filter, SessionRepositoryFilter, early in the filter chain. A filter is code that runs before our controllers on every request. It wraps the request so that when anything calls request.getSession(), it gets a session loaded from a SessionRepository instead of the container's map. At the end of the request, the filter saves any changes back to the repository.
The reason this is transparent is that our code, Spring Security and Spring MVC all keep using the standard HttpSession interface. Only the storage behind it changes. Every instance now reads and writes the same store, so it no longer matters where the load balancer sends a request.
- Controllers and Spring Securitycall
request.getSession()as before - SessionRepositoryFilterwraps the request and the response
- RedisSessionRepositoryloads and saves sessions by id
- Redisshared by all four instances
4 layers, from the top down. Step through them one at a time.
4Configuring Redis Step by Step
With Spring Boot we need one dependency and a connection. Boot sees spring-session-data-redis on the classpath and auto-configures the filter and the repository.
Notice the namespace. If several applications share one Redis, each needs its own prefix, otherwise they could read each other's sessions or clean up each other's keys. The cookie name changes to SESSION by default, which is a small detail worth knowing when debugging in the browser. In older Boot versions we had to set spring.session.store-type; current versions pick the store from the classpath, so check your version if you see that property in an old guide.
5Serialization of Session Attributes
Because the session now leaves the JVM, every attribute must be turned into bytes. By default Spring Session uses JDK serialization, so attribute classes must implement Serializable; storing an object that does not fails when the session is saved.
JDK serialization is also fragile across releases, because a changed class may no longer read old bytes. Many teams therefore register a JSON serializer as a bean named springSessionDefaultRedisSerializer, for example a GenericJackson2JsonRedisSerializer configured with Spring Security's Jackson modules, and keep only small, stable values in the session, such as ids rather than whole entities.
6Alternatives to Redis
Spring Session also offers a JDBC store, spring-session-jdbc, which keeps sessions in two tables of a relational database we already run. It is simpler to operate but adds load to the database, and expired sessions are removed by a scheduled cleanup job. The other route is to stop keeping server state at all and move to stateless tokens, such as OAuth 2 access tokens. That removes the shared store but makes logout and revocation harder, so it is a bigger design change than a storage swap.
7Follow-up questions
What happens to existing sessions when Redis restarts?
It depends on how Redis is run. If Redis has no persistence, every session is gone and every user must log in again. With RDB snapshots or the AOF log, Redis reloads the data, and sessions changed since the last write to disk may be lost. During the restart itself, requests that need the session fail with connection errors, which usually means 500 responses. For production we run a replicated, managed Redis with automatic failover so that a single node restart is not an outage.
Do we still need sticky sessions?
No. With a shared store any instance can serve any request. Some teams keep stickiness for cache warmth, but it should not be required for correctness.
What does the application code need to change?
Usually nothing, apart from making session attributes serializable. Controllers still use HttpSession or @SessionAttributes, and Spring Security works unchanged.
8Conclusion
In this answer, we saw that a servlet session lives in the memory of one instance, so four instances behind a load balancer lose it whenever a request lands elsewhere, and sticky sessions only hide that. Spring Session solves it with SessionRepositoryFilter, which swaps the storage behind HttpSession for a shared repository. With Boot we add spring-session-data-redis and a Redis connection, set spring.session.timeout and a namespace per application, make attributes serializable or register a JSON serializer, and keep the JDBC store or stateless tokens in mind as alternatives.
Open with
The random logouts happen because each Tomcat instance keeps HttpSession in its own memory. When the load balancer sends the next request to a different instance, that instance does not know the session id from the cookie, so the user looks anonymous.
Then, if they want more
Sticky sessions would hide it, but users are still logged out when an instance restarts or scales down. Spring Session solves this by registering a servlet filter, SessionRepositoryFilter, that wraps the request so getSession returns a session loaded from a shared repository, and saves changes at the end of the request.
The application, Spring MVC and Spring Security keep using the normal HttpSession API. To use Redis, I add spring-session-data-redis with the Redis starter and set the spring.data.redis connection properties; Spring Boot auto-configures the filter and repository. I set spring.session.timeout, for example 30 minutes, and spring.session.redis.namespace so several applications can share one Redis safely.
Attributes have to be serializable, since the default is JDK serialization, and I would rather register a JSON serializer bean named springSessionDefaultRedisSerializer and keep only small values like ids in the session. Alternatives are the JDBC store, or moving to stateless tokens if we want no server state at all.
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
- Spring Boot reference
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 "A Spring MVC application uses HttpSession and is scaled from one instance to four behind...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/79c0bc49e6/