Hi,
Is there any good example how to manage session replication on AWS infrastructure (ElastiCahce Redis or RDS database or some other shared storage) during autoscaling up or down. We are using session stickiness with app load balancer (ALB) but we have trouble to manage session replication when, for example) one instance shutting down. I read a lot of blogs and always same problems with session serialization or locks (eg. ReentrantLock) etc.
Firs, we want to avod user to be loggedout. I didn’t find ant good example how to manage session replication with Vaadin Flow app.
Any help or code snippet regardnig session replication could help.
Vaadin uses a ReentrantLock instance that is stored in the HTTP session to ensure the UI state isn’t accessed by multiple parties at the same time. The lock is automatically acquired for regular request handling and when using UI.access(). Relying on a ReentrantLock only works when everything related to one session is isolated to one JVM. For a session that is replicated between multiple servers, concurrent access needs to be ensured separately. In particular, a distributed lock for a specific session must be acquired before even deserializing the session data.
All this means that generic “transparent” session replication solutions cannot be used. Instead, it would be necessary to build something specifically to Vaadin and also make changes to bypass the built-in use of ReentrantLock. Even with those changes, application development gets more complicated and the user faces increased latencies.
What this means in practice is that you need to study if your system has distributed lock system of some sort. And once you know the details, it is possible for them to customize Vaadin to use it. In nutshell you need to extend both VaadinServlet and VaadinServletService. In VaadinServlet you can set VaadinServlet to use your extended VaadinSrrvletService. The methods to get and set the lock are protected methods, which means that they can be overriden with your own implementation. How exactly you should implement the methods, that I cannot say in detail, it is very system specific. But this is the way which could potentially make it work if you know how to do it.
To get the idea I can link here how I did override those methods in Vaadin 8 (same can be done also in Vaadin 14) to use scoped locks in Liferay environment https://github.com/vaadin/framework/pull/11792/files So what you need is something similar, but different that fits to the specific environment of yours. So you need to understand in detail how the distributed locking works in the environment.
Apart from that I can say, that questions like this related to problems getting session replication working with Redis are rather common, see e.g. here
which indicates, that it is not trivial. Sometimes I’ve been in the impression that it is possibly not working in generic scenario, but I may be wrong.