Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

After a deployment, users of a Spring Session application backed by Redis get 500 errors with SerializationException: Cannot deserialize, and the log mentions InvalidClassException for a class stored in the session. Explain the cause and how to make future deployments safe.

The sessions in Redis hold JDK-serialized objects written by the old version of the application. The new version changed one of those classes, its fields or its serialVersionUID, so Java refuses to read the old bytes and throws InvalidClassException, which Spring wraps in a SerializationException. In the short term we invalidate or flush the unreadable sessions; in the long term we store only small, stable data in the session, use a JSON serializer with versioned DTOs, and test that two versions can read each other's sessions.

Why this question is asked

This failure usually appears minutes after a deployment, hits only users who were already logged in, and disappears for anyone who clears their cookies, which makes it confusing the first time. Interviewers ask it because it tests whether we know that a session in Redis is data that outlives the code that wrote it. In this answer, we will recap how Spring Session stores attributes, reproduce the error, explain exactly why deserialization fails, stop the bleeding, and then make future deployments safe, including the Spring Security trap.

1A Quick Recap of How Sessions Are Stored

Spring Session keeps each session in Redis as a hash: some metadata fields such as the creation time, plus one field per session attribute. Each attribute value is turned into bytes by a RedisSerializer, and by default that serializer uses JDK serialization, the built-in Java mechanism that writes an object's class name, a version number for the class and all its fields.

That version number is the serialVersionUID. If a class does not declare one, the JVM computes it from the class's structure, so adding a field or a method changes it. When Java reads the bytes back, it compares the number in the stream with the number of the class it has now. If they differ, it refuses with java.io.InvalidClassException.

2Reproducing the Error

Suppose the old version stored a Cart object in the session, and the new version added a field to Cart without declaring a serialVersionUID.

Java
// version 1.4
public class Cart implements Serializable {
private List<CartLine> lines = new ArrayList<>();
}
// version 1.5 adds a field
public class Cart implements Serializable {
private List<CartLine> lines = new ArrayList<>();
private String couponCode;
}
// first request after the deploy, for a user logged in before it:
// org.springframework.data.redis.serializer.SerializationException: Cannot deserialize
// Caused by: java.io.InvalidClassException: com.shop.Cart; local class incompatible:
// stream classdesc serialVersionUID = 4213..., local class serialVersionUID = -8890...
Step throughWhy only existing users fail
BrowserApp v1.5Redis
GET /cart, SESSION cookie from before the deploy
HGETALL session
bytes written by v1.4
deserialize Cart: serialVersionUID differs
500 SerializationException

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

New sessions are written and read by v1.5 alone, so they work.

Notice that the new code is fine and the old data is fine. The failure lives in the gap between them. Every request from that user loads the session, hits the bad attribute and returns a 500, so the user cannot even reach the logout page.

3Stopping the Bleeding

The quickest fix is to remove the unreadable sessions and accept that the affected users log in again. If the application has its own namespace, we can delete just its keys without touching other applications.

YAML
# delete only this application's sessions (UNLINK frees memory in the background)
redis-cli --scan --pattern 'spring:session:shop:*' | xargs -n 500 redis-cli unlink
# never FLUSHALL on a Redis shared with other systems

A gentler option is a serializer that treats unreadable bytes as missing. We wrap the default serializer, catch the SerializationException on deserialize, log it and return null. The attribute then looks absent, so Spring Security sees no login and sends the user to the login page instead of a 500. Check how your Spring Session version handles a null attribute value before relying on it, and remove the wrapper once old sessions have expired.

4Making Future Deployments Safe

The lasting fix has three parts. First, store only small, stable data in the session: a user id or a cart id instead of the Cart entity, and load the rest from the database. Small values change rarely, and a session of a few hundred bytes is also cheaper to read on every request.

Second, stop using JDK serialization. We register a JSON serializer as a bean named springSessionDefaultRedisSerializer, and we store dedicated DTOs whose JSON can evolve: new fields are optional, removed fields are ignored on read, and a version field lets us migrate when a change is not additive.

Third, avoid putting framework objects whose classes change between library versions into the session, because their compatibility is outside our control.

Java
@Bean("springSessionDefaultRedisSerializer")
RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper base) {
ObjectMapper mapper = base.copy();
mapper.registerModules(SecurityJackson2Modules.getModules(getClass().getClassLoader()));
return new GenericJackson2JsonRedisSerializer(mapper);
}
// session attribute in Redis is now readable JSON:
// {"@class":"com.shop.session.CartRef","cartId":981,"version":1}

With Jackson 3 on Spring Boot 4 the class and module names differ, so check your version; the idea of a named JSON serializer bean stays the same.

5Rolling Deploys and the Spring Security Trap

A rolling deploy replaces instances one by one, so for several minutes version 1.4 and version 1.5 serve the same users. A session written by 1.5 may be read by 1.4 on the next request, and the other way round. So compatibility must work in both directions, which is the same rule we apply to database migrations.

Spring Security is the most frequent source of this failure, because it stores the SecurityContext, with the Authentication object and its authorities, in the session. Spring Security only promises that serialized forms are compatible within the same minor version, so an upgrade from one minor version to the next can make every logged-in session unreadable if we use JDK serialization. Check the release notes of your version. Planning framework upgrades as a forced re-login, or using the JSON modules, removes the surprise.

6Follow-up questions

How would you test cross-version session compatibility?

We add a CI job that starts the previous released version of the application against a Redis container, logs in and fills the session with typical data, then stops it and starts the new build against the same Redis. The test then makes requests with the saved cookie and asserts that the user is still logged in and the cart is intact. Running it in both directions, new writes and old reads, catches the problems a rolling deploy would hit. Testcontainers makes this practical in a normal build.

Would declaring serialVersionUID have prevented this?

It would have prevented this specific error, because Java would then read the old bytes and leave the new field at its default. But it only helps for compatible changes such as added fields. Renaming a field, changing its type or moving the class still breaks, so it is a patch, not a strategy.

Why not just make sessions very short?

Short sessions reduce how many users are affected, but they annoy users all the time and still break during the deploy window. Fixing the data format is better than limiting the damage.

7Conclusion

In this answer, we saw that Spring Session stores attributes with JDK serialization by default, so a changed class or serialVersionUID makes old sessions unreadable and every request from those users fails. We stopped the bleeding by deleting the application's session keys or by treating unreadable attributes as missing. Then we made deployments safe: small stable data such as ids in the session, a JSON serializer bean named springSessionDefaultRedisSerializer with versioned DTOs, no volatile framework objects, two-way compatibility for rolling deploys tested in CI, and special care with the SecurityContext on Spring Security upgrades.

What to say in the interviewAbout 90 seconds aloud

Open with

The cause is that the sessions in Redis contain objects serialized with JDK serialization by the previous release. The new release changed one of those classes, so its computed serialVersionUID no longer matches the one in the stored bytes, Java throws InvalidClassException, and Spring Session wraps it as a SerializationException.

Then, if they want more

Every request from a user who logged in before the deploy loads the session and fails, which is why only existing users see 500s.

In the short term I would delete this application's session keys under its namespace, using scan and unlink rather than flushing a shared Redis, or wrap the serializer so unreadable attributes are treated as missing and users are simply asked to log in again.

For the long term, I would keep only small, stable data in the session, like a user id or cart id instead of entities, and switch to a JSON serializer registered as a bean named springSessionDefaultRedisSerializer, with versioned DTOs that tolerate added and missing fields.

Because rolling deploys run two versions side by side, both must read each other's sessions, so I would add a CI test that creates sessions with the previous release and reads them with the new one.

And I would watch Spring Security upgrades closely, because the SecurityContext lives in the session and its serialized form is only guaranteed within a minor version.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4
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 "After a deployment, users of a Spring Session application backed by Redis get 500 errors...": https://scrivio-interview-library.netlify.app/spring-boot/spring-projects/791d958e21/