Quick revision
The short answer
Before autoscaling, every piece of state that lives in one instance must move to a shared home. In order: move HTTP sessions to Spring Session with Redis, keeping session objects small and serializable; move uploads to object storage and keep only references in the database; review in-memory caches and singletons that hold user state; and make scheduled jobs run once across replicas. Then prove it by killing pods during real user flows and checking that nothing is lost.
What to remember
In this answer, we prepared a legacy Spring MVC application for Kubernetes with autoscaling by giving every kind of state a home that outlives pods, in order of risk: sessions to Spring Session with Redis with small serializable attributes, uploads to object storage with references in the database, caches and singletons reviewed for user state, and scheduled jobs made to run once. We then proved the result by killing pods during real user flows, with graceful shutdown and readiness probes, before enabling autoscaling.
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.
Before autoscaling, every piece of state that lives in one instance must move to a shared home. In order: move HTTP sessions to Spring Session with Redis, keeping session objects small and serializable; move uploads to object storage and keep only references in the database; review in-memory caches and singletons that hold user state; and make scheduled jobs run once across replicas. Then prove it by killing pods during real user flows and checking that nothing is lost.
Why this question is asked
Lifting a legacy application onto Kubernetes is easy; making it survive Kubernetes is the hard part. Kubernetes moves, restarts and removes pods as a matter of routine, and autoscaling adds and removes them every hour. An application that keeps carts in memory and files on local disk will lose both, many times a day. Interviewers ask this scenario to see whether we can find all the hidden state and order the work so the application keeps running during the migration. In this answer, we will see why the order matters, then go through sessions, uploads, caches and singletons, scheduled jobs and the testing that proves the result.
1Why Kubernetes Exposes Hidden State
On a long-lived server, the application ran as one process for months, so in-memory sessions and local files mostly worked. On Kubernetes, a pod can be replaced at any time: during each deployment, when a node is drained for maintenance, when it exceeds its memory limit, and whenever the autoscaler scales down. A pod's local filesystem is ephemeral, which means it disappears with the pod.
So every piece of state that lives in one pod will be lost regularly. Our job is to find each kind of state and give it a home that outlives pods. The order matters because we want the application to work at every step, and the riskiest losses, carts and uploads, come first.
- Run on Kubernetes with one replica, sticky sessionssafe starting point
- Sessions -> Spring Session with Rediscarts survive pod loss
- Uploads -> object storagefiles survive pod loss
- Review caches and singletonsno user state in memory
- Scheduled jobs run oncelock or clustered scheduler
- Kill pods during user flowsprove it
- Enable autoscalingonly now
7 steps in order. Play them, or step through them one at a time.
2Sessions to Spring Session With Redis
The cart lives in HttpSession today. Spring Session lets us keep that code and change only where sessions are stored. In a plain Spring MVC application without Boot, we add the Spring Session Redis module and enable it with @EnableRedisHttpSession, which registers a filter that replaces the container's session handling; in Spring Boot, adding the dependency is usually enough.
Notice the second change. Legacy applications often put large object graphs into the session, including JPA entities and service objects. Every attribute must be serializable to travel to Redis, and every request now reads the session over the network, so large sessions become slow. Keeping only ids in the session, and the cart itself in a database table, also means a cart survives the session expiring, which the business usually likes.
During the move, existing in-memory sessions are lost once, so we deploy at a quiet time or keep sticky sessions on the old version until its sessions expire.
3Uploads to Object Storage
Files written to local disk are lost when the pod goes, and a file uploaded through pod A is invisible to pod B. We store uploads in object storage, such as Amazon S3 or a compatible store, and keep only the object key and metadata in the database.
For large files, the application can hand the browser a pre-signed URL, a time-limited link that allows one upload or download directly to the store, so the bytes never pass through our pods at all. Existing files on the old server's disk are copied to the store with a one-off migration script before the switch.
4Caches and Singletons That Hold User State
This step needs a code review, because the state is not in an obvious place. Typical findings in legacy code are a static map of logged-in users, a singleton bean that keeps the last search per user, an in-memory counter of failed login attempts, or a cache of user permissions.
For each one we ask whether instances must agree. A cache of product categories that may be a minute stale on one pod is fine as a local cache with a short expiry. A failed-login counter that must count attempts across pods, or a permission cache that must reflect a revoked role immediately, has to move to Redis or the database, or be invalidated on every pod through a message when it changes.
5Scheduled Jobs That Run Once
With several replicas, every @Scheduled method runs on every pod, so the nightly invoice job sends three copies of every invoice. We make each job run once, for example with ShedLock, which takes a named lock in a database table before running, or by moving jobs to Quartz with its clustered JDBC store, or to a Kubernetes CronJob that runs a separate process.
We also check that jobs can survive interruption: a pod may be stopped half-way through a job, so the job must be able to resume or restart without duplicating its work, for example by marking each invoice as sent.
6Prove It by Killing Pods
The final step is evidence. With several replicas running, we script the important user flows, log in, add to cart, upload a document, check out, and while they run we delete pods one by one, roll out a new version and trigger scale-down. Then we check that no user was logged out, no cart item and no file disappeared and every job ran exactly once.
We also make sure pods stop politely. Graceful shutdown, server.shutdown=graceful in Spring Boot (the default in recent versions, check yours), lets in-flight requests finish when Kubernetes sends the stop signal, and a readiness probe removes the pod from the load balancer first. Only after these tests pass do we turn on the autoscaler.
7Follow-up questions
Can we skip Spring Session and keep sticky sessions?
As a first step, yes, but not with autoscaling. Every scale-down and every deployment logs out the users on the removed pods and empties their carts. Sticky sessions keep the old assumption alive while Kubernetes keeps breaking it.
What about WebSocket connections or long-polling?
Those connections are tied to one pod by nature and are lost when it goes. The client must reconnect automatically, and any messages the user should receive must be stored or published through a broker, not kept in the pod's memory.
How do you migrate existing sessions without logging everyone out?
Usually we accept one controlled logout at a quiet time, because in-memory sessions cannot be copied out reliably. If that is unacceptable, we run the old and new versions side by side with sticky sessions for the old one, send new sessions to the new version and let the old sessions expire before removing the old pods.
8Conclusion
In this answer, we prepared a legacy Spring MVC application for Kubernetes with autoscaling by giving every kind of state a home that outlives pods, in order of risk: sessions to Spring Session with Redis with small serializable attributes, uploads to object storage with references in the database, caches and singletons reviewed for user state, and scheduled jobs made to run once. We then proved the result by killing pods during real user flows, with graceful shutdown and readiness probes, before enabling autoscaling.
Open with
Kubernetes replaces pods all the time, on deployments, node drains and scale-down, and local disk is ephemeral, so every piece of state in one pod will be lost.
Then, if they want more
I would first run it with one replica and sticky sessions as a safe start, then move state out in order of risk.
First sessions: Spring Session with Redis, enabled with EnableRedisHttpSession in plain Spring MVC, and I would shrink the session to small serializable values like a cart id, keeping the cart itself in a table, because legacy sessions often hold entity graphs.
Second uploads: object storage like S3, with only the key in the database and pre-signed URLs for big files, plus a one-off copy of existing files.
Third a review of caches and singletons for user state, like a static map of users or a failed-login counter; anything that must agree across pods moves to Redis or the database or gets invalidation messages. Fourth scheduled jobs: ShedLock, clustered Quartz or a Kubernetes CronJob so they run once, and make them safe to interrupt.
Finally I would prove it: run scripted user flows while deleting pods and rolling out versions, with graceful shutdown and readiness probes, and only then enable autoscaling.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 legacy Spring MVC application keeps shopping carts in the HTTP session and stores...": https://scrivio-interview-library.netlify.app/spring-boot/scalability/3d63ac32dc/