Session loss across multiple servers is fixed by moving session state out of individual application instances and into a shared, centralized session store that every server can read and write.
Key Points: • A distributed cache like Redis holds session data centrally instead of each server keeping its own in-memory session. • Spring Session integrates with Spring Boot to transparently replace the default HttpSession implementation with one backed by Redis. • Configuring spring.session.store-type=redis routes all session reads and writes through the shared store automatically, with no controller code changes needed. • This removes the need for sticky sessions on the load balancer, so any instance can serve any request. • It also means sessions survive an instance restart, deployment, or autoscaling event instead of being lost.
Example: Before the fix, a user's cart would disappear if the load balancer routed their next request to a different server; after adding Spring Session with Redis, every server reads the same session data, so the cart persists no matter which instance handles the request.
Code Example:
# application.properties
spring.session.store-type=redis
spring.redis.host=redis-cluster
spring.redis.port=6379Interview Tip: A concise interview answer is:
"I'd centralize session storage using Spring Session backed by Redis, so every instance reads and writes the same session data instead of keeping it in memory. That removes the need for sticky sessions and means restarts or scaling events don't lose anyone's session."