Search code examples
javahazelcastspring-session

Spring session lazy deserialization


I have the following situation, i have a microservice architecture with an api gateway and multiple downstream services, some of these have an independent session and this causes my system to throw expired session exception on random service calls.

Since we cannot rewrite these services from scratch we started by introducing hazelcast so that all services can share the same session.

the problem now is that when a service writes an object of a class that other services don't have in their classpath a deserialization exception is thrown.

to solve this i was thinking that if only the attributes that get accessed from a service get deserialized i could probably avoid the exception and everything should work fine.

Do you know if this is at all possible with spring session, or maybe can suggest another solution that would allow me solve the initial problem?

here is a sample project to reproduce my problem: https://github.com/deathcoder/hazelcast-shared-session


Solution

  • I believe I got what's happening: Spring-Session-Hazelcast by default store session updates locally until request completed & when request completed, before returning the response, send everything to the cluster using EntryProcessor. EntryProcessor requires object classes available on the member who stores that session record and since data is distributed, it's possible some other member stores a session created in another instance. According to what you're saying, not all nodes are identical, don't contain all classes & this causes serialization exception.

    What you can do, you can use User Code Deployment feature to deploy those missing classes to other members: https://docs.hazelcast.org/docs/3.11/manual/html-single/index.html#member-user-code-deployment-beta

    If you're changing object that you're storing in the session, you can set class-cache-mode to OFF to prevent not caching them but sending with each operation.

    Please try & let me know if this solves your problem.