Recommend a jetty solution for clustered session management?
Jetty version 9.4.19.v20190610 Java version java 8 Question We have a clustered web service running on Jetty. Session is sticky in the sense that the initial login can go to any node, but the subsequent requests all go to the same node where login happened. A user can log in on different devices/browsers and have active sessions on multiple nodes in the cluster.
We are trying to find a way to enforce concurrent session limit. For example, only allow a user to have at most 3 activate sessions at any time, if a user goes over the limit, we will invalidate the oldest session. Since a user can have active sessions across nodes, killing the oldest session can require invalidating session(and cache if there's any) on a different node.
Is there any recommendation on how to handle session invalidation across nodes?
What we have tried:
We were using DefaultSessionCache, and found that we were not able to invalidate cache across nodes. Then we switched to NullSessionCache + killing the oldest session directly in the session store. But we have seen in some cases that a race condition revives a session that just got deleted in session store. (Example: a long-running request come in before its session got deleted, after session complete, this request writes the session back to store again)
Thanks for any help in advance!
@janbartel would you be able to help take a look at this question, thanks!
Well, there have been some fixes in more recent 9.4 versions of some race conditions that can happen with sessions and concurrent requests, so you should try upgrading to the latest and retesting with the NullSessionCache and deleting the session directly in the store. However, if you have multiple concurrent requests for the same session on the same node, each request will have its own copy of the session, so you can't guarantee the outcome of writes, including session invalidation.
You could try extending NullSessionCache/whichever SessionDataStore you're using to do a read from the store before a write: if the session has been saved before (ie lastSaveTime > 0) but doesn't exist in the store then it must have been invalidated.
Alternatively, you might want to step outside of session mechanism entirely and do your own specific handling of this authentication lifecycle using a distributed transaction manager.
@janbartel thanks for the reply! I'm now trying to make it check if lastSaveTime > 0 before writing to store, will update here once I have anything
just want to update that we were able to resolve the issue by relying on the lastSaveTime. Thanks!
@hanlinlisumo Can you expound on your solution? I am sort of facing a similar issue