Scrivio Interview library
ScenarioPrincipal6 min read · updated 12 November 2026

A large legacy web application stores significant state in HTTP sessions on application servers with session replication. You are moving it to containers over three years. What is your session strategy?

I would plan in three stages: first move sessions to an external store such as Redis with Spring Session or the server's own equivalent so containers can come and go, then shrink what the session holds by moving durable data into databases, and finally make services stateless with tokens where the security requirements allow. Throughout, I would plan for serialisation breaking when classes change and measure session size and count so each stage is driven by numbers.

Why this question is asked

Large legacy applications often use the HTTP session as a general-purpose cache and scratch pad: wizards keep half-filled forms there, search results are stored for paging, and whole user profiles sit in memory. Application server session replication made this work across a cluster, so nobody noticed the cost. Containers break that assumption, because instances are short-lived and replication between them is awkward. Interviewers ask this scenario at senior level to see whether we can sequence a long migration so that each step delivers value and lowers risk. In this answer, we will look at why replicated sessions do not move cleanly, then walk through the short, medium and long term stages, the serialisation trap, and how measurement steers the plan.

1Why Replicated Sessions Do Not Survive the Move

Session replication on servers such as WebLogic, WebSphere or a Tomcat cluster copies each session to one or more other nodes, so a node can fail without users losing state. It relies on stable cluster membership and on nodes that live for months.

In containers, instances start and stop many times a day, addresses change, and the orchestrator does not know that a pod holds the only copy of thousands of sessions. Replicating large sessions also costs network and memory, and that cost grows with every node. So the first goal is to separate session state from the life of the application instance.

2Short Term: Move Sessions to an External Store

In the first months I would move sessions out of the JVM without changing what they contain. For a Spring application, Spring Session with Redis replaces the container's session store behind the HttpSession API, so the application code does not change. For an application still on a full application server, the server's own external session option (for example a Redis or database session manager) does the same job.

This stage unlocks the container move: pods become disposable, rolling deployments no longer log users out, and we can turn off replication. It also exposes the real size of sessions, because every save now crosses the network.

Step throughThree-year session strategy
  1. MeasureSession count, size, attribute types
  2. Year 1: external storeSpring Session + Redis; no code change
  3. Year 2: shrink contentsDurable data to databases; ids only
  4. Year 3: stateless where it fitsTokens for APIs and new services
  5. Keep measuringSize, count, latency at each release

5 steps in order. Play them, or step through them one at a time.

Each stage is useful on its own, so the plan survives a change of priorities.

3Medium Term: Shrink What the Session Holds

Once sessions are external, large sessions hurt every request. A 300 KB session read and written on each request, at 2,000 requests per second, is around 600 MB per second of traffic to Redis in each direction. That number usually makes the case for the second stage on its own.

So we go through the attributes and move each one to where it belongs. Saved drafts and wizard progress go into database tables keyed by user, so they also survive logout. Cached reference data goes into a proper cache shared by all users. Search results are recomputed or stored with a short expiry. What remains in the session should be small: the user id, a few preferences and a CSRF token. Each module can be cleaned up separately, which fits a three-year timeline.

4Long Term: Stateless Services Where They Fit

With small sessions, some parts can drop server-side sessions entirely. APIs used by mobile apps or other services can authenticate with tokens from an identity provider using OAuth 2 and OpenID Connect. New services carved out of the monolith start stateless.

But stateless is not automatically better. Tokens are hard to revoke before they expire, and regulated applications may need instant logout or tight session control. For browser applications a small session in Redis plus a session cookie is often still the safer design. So the goal for year three is stateless where it fits the security requirements, not stateless everywhere.

5Planning for Serialisation Problems

Once sessions are stored outside the JVM, they are serialised and read back by other instances, possibly running a newer release. During a rolling deployment version 1 and version 2 run side by side and read each other's sessions. If a class in the session changed shape, Java serialization can throw InvalidClassException, and the user sees an error or loses the session.

Java
public class WizardState implements Serializable {
private static final long serialVersionUID = 1L; // fixed, not generated
private Long customerId;
private String step;
// Added in release 2.4; old sessions read it as null
private String promoCode;
}
// Without a fixed serialVersionUID, release 2.4 reading a 2.3 session:
// java.io.InvalidClassException: local class incompatible

The defences are a fixed serialVersionUID on every session class, changes that only add nullable fields, a JSON serializer for session attributes instead of Java serialization (more tolerant of change and readable when debugging), and a test that reads sessions written by the previous release. Shrinking sessions in the medium stage also shrinks this risk, because fewer classes live there.

6Measuring as We Go

The plan should be steered by numbers, not opinions. Before the first step we record the number of active sessions, the average and 99th percentile session size, and which attributes take the most space. We can collect this with a session listener or by sampling the external store. After each release we compare. Targets such as average session under 5 KB and no attribute over 50 KB make the medium stage concrete and tell us when a module is done.

7Follow-up questions

Would you rewrite everything to stateless tokens first?

No. A big-bang rewrite of authentication and state handling across a large legacy application is high risk and delivers nothing until it is finished. Moving sessions to an external store first delivers the container migration quickly, and the later stages can proceed module by module.

How do you find out what is in the sessions today?

Add a temporary HttpSessionAttributeListener or a filter that records attribute names and serialised sizes on a sample of requests, or inspect sessions in the external store once they are there. The result is usually a short list of a few attributes that take most of the space.

What if the external store fails?

Users who need their session will see errors until it recovers, so the store needs replication and failover and should be monitored like a database. Keeping sessions small also makes recovery faster, because less data has to be rebuilt or reloaded.

8Conclusion

In this answer, we saw that replicated in-memory sessions do not fit short-lived containers. The short-term step moves sessions to an external store without code changes, the medium-term step moves durable data to databases so sessions become small, and the long-term step makes services stateless where security allows. Serialisation compatibility and steady measurement of session size and count keep each stage safe.

What to say in the interviewAbout 80 seconds aloud

Open with

I would treat this as three stages, each useful on its own. In the first months I would move sessions out of the JVM into Redis using Spring Session, or the application server's external session manager if it is not on Spring yet.

Then, if they want more

That needs almost no code change and makes containers disposable, so we can drop server replication. In the second stage I would shrink the sessions.

Legacy apps keep drafts, search results and whole profiles there, and once every request reads the session over the network that is expensive, so durable data moves into database tables and the session keeps ids and a few small values.

In the third stage, services that fit, such as APIs and new services, become stateless with tokens from the identity provider, but I would keep small server-side sessions where we need instant logout or tight control.

Along the way I would plan for serialisation: during rolling deployments two releases read each other's sessions, so I want fixed serialVersionUIDs, additive changes, ideally JSON serialisation, and a test that reads old sessions. And I would measure session count and size from day one so every stage has a target.

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 "A large legacy web application stores significant state in HTTP sessions on application...": https://scrivio-interview-library.netlify.app/java/servlets/60e16db8e1/