Quick revision
The short answer
A class stored in the HTTP session changed in the new release, and because it never declared a serialVersionUID, Java computed a new one from the changed class. During the rolling update, old and new nodes pass serialized sessions to each other, and each side refuses the other's bytes because the version numbers differ. We prevent it by declaring serialVersionUID explicitly and making only compatible changes, and better still by keeping session data small and stored as JSON in an external store, with a test that reads data written by the previous release.
What to remember
In this answer, we saw that the InvalidClassException came from a session class whose computed serialVersionUID changed between releases, while old and new nodes exchanged serialized sessions during the rolling update. We fixed it in three layers: declaring the old value explicitly and making only compatible changes such as adding fields, moving session data to a small, explicit format such as JSON in an external store, and testing every release against data written by the previous one.
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.
A class stored in the HTTP session changed in the new release, and because it never declared a serialVersionUID, Java computed a new one from the changed class. During the rolling update, old and new nodes pass serialized sessions to each other, and each side refuses the other's bytes because the version numbers differ. We prevent it by declaring serialVersionUID explicitly and making only compatible changes, and better still by keeping session data small and stored as JSON in an external store, with a test that reads data written by the previous release.
Why this question is asked
Rolling updates are supposed to be the safe way to deploy: we replace nodes one at a time and users never notice. This failure breaks that promise in a quiet way, because the code is correct on each node and only the conversation between versions fails. Interviewers ask it to see whether we understand what Java serialization really writes, how it decides whether two class versions match, and how session data should be designed so deployments stay boring. In this answer, we will recap how session replication uses serialization, see where the version number comes from, reproduce the mismatch, and then look at three layers of prevention: an explicit serialVersionUID, a better session format, and a test that proves the upgrade works.
1A Quick Recap of Session Replication
An HTTP session holds data for one user between requests, such as a shopping cart or the steps of a form. In a cluster, any node may receive the user's next request, so the session must be available everywhere. Servlet containers such as Tomcat can replicate sessions between nodes, and libraries such as Spring Session can store them in Redis or a database. In both cases the session attributes have to become bytes, and by default those bytes are produced by Java serialization through ObjectOutputStream.
Java serialization writes, for each object, a class descriptor: the class name, a 64-bit number called the serialVersionUID, and the names and types of its fields, followed by the field values. When another JVM reads the bytes, ObjectInputStream loads its own copy of that class and compares the two serialVersionUIDs. If they differ, it refuses to continue and throws InvalidClassException.
2Where the Number Comes From When We Do Not Declare It
If a class does not declare serialVersionUID, the JVM computes one. It hashes the class name, its modifiers, its interfaces, and the signatures of its fields, constructors and methods. That means almost any change to the class gives a new number: adding a method, changing a method's parameter, or even a compiler adding a synthetic member for a lambda or an inner class. The values of the fields do not matter; the shape of the class does.
Notice that the new method does not change the data at all. A release-41 CartItem and a release-42 CartItem hold exactly the same two fields, and either version could read the other's bytes perfectly well. But the computed numbers differ, so Java treats them as unrelated classes. The numbers above are examples; the serialver tool that ships with the JDK prints the real value for any class.
3What Happens During the Rolling Update
Now picture a cluster of four nodes. We replace node 1 with release 42 while nodes 2, 3 and 4 still run release 41. A user's session was created on node 3, so its CartItem bytes carry the old number. When node 1 receives a replicated copy, or reads the session from the shared store, it fails. The traffic also flows the other way: sessions touched on node 1 are written with the new number, and the old nodes cannot read them either.
7 messages in order. Play them, or step through them one at a time.
The data was compatible; only the computed version numbers were not.
So during the whole rollout, which might take twenty minutes, users bounce between versions and lose their carts or get logged out. When the rollout finishes the errors stop, which is why this bug is often written off as a blip, until the next release changes a session class.
4Fix 1: Declare serialVersionUID and Keep Changes Compatible
The first fix is to declare the number ourselves, so that it only changes when we decide the data has changed incompatibly. The trick for a class that is already in production is to declare the value the old class was computing, which we get from serialver. That keeps every byte already stored readable.
With the number fixed, Java compares fields by name instead of rejecting the class outright. Adding a field is compatible: when new code reads old bytes, the new field gets its default value, null here, and when old code reads new bytes, it simply ignores the field it does not know. Adding or removing methods is harmless. But some changes are still incompatible even with a fixed number: changing a field's type, for example int quantity to long quantity, removing a field the old code relies on, or moving a field to another class in the hierarchy. For those we either keep both shapes readable with a custom readObject, or we treat it as a new format.
The @Serial annotation, available since Java 14, lets the compiler check that the field has the right name and signature, so a typo such as serialVersionUid is caught at build time.
5Fix 2: Keep Session Data Small and in an Explicit Format
A declared number reduces the risk, but it still depends on every developer remembering the rules for every class that ends up in the session. The stronger fix is to change what we store. A session should hold a few identifiers, such as the user id and a cart id, while the cart itself lives in a database or a cache with its own schema. Whatever must stay in the session should be written in a format with an explicit schema, such as JSON, in an external store like Redis.
With Spring Session, for example, we can replace the default JDK serializer with a JSON serializer by defining the serializer bean that Spring Session looks for; the exact bean name and serializer class depend on the Spring Session and Spring Data Redis versions, so check your version. JSON written by release 41 is plain text with field names, so release 42 can read it as long as it tolerates unknown and missing fields. And because the store is external, nodes no longer replicate sessions to each other at all.
6Fix 3: Test the Upgrade With Data From the Previous Release
The last layer is a test that proves compatibility instead of hoping for it. We keep a few session payloads written by the previous release as test resources, and the build reads them with the current classes. If someone adds a method to a class without a declared number, or changes a field type, the test fails before the release reaches the cluster.
We refresh the fixtures at each release, so the test always checks one step back, which is exactly what a rolling update needs. For JSON sessions the same idea applies: keep sample JSON from the previous release and read it with the new code.
7Follow-up questions
Why not just drain sessions before the update?
Draining, where we stop sending new users to an old node and wait for its sessions to expire, avoids mixed versions but makes a rollout take as long as the session timeout, often 30 minutes or more per node. It also does not help with sessions in a shared store, which every node reads. Fixing the format is cheaper than slowing every deployment down.
Does an IDE-generated serialVersionUID of 1L cause problems?
Not by itself. Any declared constant works, because the point is that it stays the same across releases. The only trap is declaring 1L on a class that was already in production without a declared value: old bytes carry the computed number, so the first release with 1L cannot read them. In that case copy the old computed value from serialver.
8Conclusion
In this answer, we saw that the InvalidClassException came from a session class whose computed serialVersionUID changed between releases, while old and new nodes exchanged serialized sessions during the rolling update. We fixed it in three layers: declaring the old value explicitly and making only compatible changes such as adding fields, moving session data to a small, explicit format such as JSON in an external store, and testing every release against data written by the previous one.
Open with
What happened is that a class stored in the session, say CartItem, did not declare a serialVersionUID, so Java computed one by hashing the class's structure, its fields and method signatures.
Then, if they want more
The new release changed the class, maybe just by adding a method, so the computed number changed. During a rolling update old and new nodes replicate or read each other's sessions, and ObjectInputStream compares the number in the bytes with the local class, sees a mismatch and throws InvalidClassException. Users lose their session while the rollout is in progress.
To fix it immediately, I would run serialver against the previous release to get the old computed value and declare exactly that value with @Serial, so existing bytes stay readable. From then on we only make compatible changes, like adding fields, which get default values when old data is read.
Longer term I would shrink the session to identifiers and store anything else as JSON in an external store such as Redis through Spring Session, so the format has visible field names and no node depends on another node's class versions.
And I would add a test that reads session payloads written by the previous release with the current code, refreshed at every release, so a compatibility break fails the build instead of failing in production.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 14
- 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
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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, a cluster with rolling updates sees InvalidClassException: local...": https://scrivio-interview-library.netlify.app/java/serialization/4207976ef5/