spring-projects / spring-session

Spring Session
https://spring.io/projects/spring-session
Apache License 2.0
1.86k stars 1.11k forks source link

Migration of active sessions from one datasource to another #3008

Closed sp1986sp closed 4 months ago

sp1986sp commented 5 months ago

Problem Statement This pertains to a use case requiring the migration of Spring Session within a live system from one session data storage to another, seamlessly, without any system downtime.

Proposed Solution I propose the development of a Spring Session library enabling the migration of sessions between different data storage systems within a live environment, ensuring uninterrupted system operation. This solution involves connecting to two distinct data sources, one serving as the primary and the other as a secondary data store.

Allow me to illustrate this process using an example: Suppose your current service employs Redis as the data store for Session Management, and now you need to migrate to a Mongo datasource for the same service.

Outlined below are the stages of this migration process:

Stage 1 - During this initial phase, Redis functions as the primary source for sessions, while Mongo serves as a secondary store, exclusively for session writes. Sessions are served exclusively from Redis. Only existing session data and new sessions are created in both Redis and Mongo, ensuring uniformity in id and attributes. This stage aims to transfer all existing user sessions to Mongo, with each session access resulting in its duplication in both data sources. This stage continues until the max-inactive interval defined for the sessions is reached.

Stage 2 - In this stage, Mongo becomes the primary session store, with Redis retained as a secondary backup. This configuration ensures a seamless fallback to Redis in case of any issues, thereby preventing any user disruptions or logout incidents.

Stage 3 - Finally, in this stage, the secondary write data store is deactivated, leaving Mongo as the sole repository for serving sessions.

Session_Migration_Stages1

I propose integrating a library into Spring Session, enabling the execution of the aforementioned stages without disrupting the running service.

Current Scenario: At present, there exists no library tailored for migrating sessions seamlessly between different data sources.

Background: This necessity arose when we decided to transition from using Redis as our data source to utilizing Mongo for session management.

Additional Details: If the request looks justified, I can provide further insight into the idea and elaborate on our approach.

sp1986sp commented 5 months ago

Any feedback on this?

marcusdacoregio commented 4 months ago

Hi @sp1986sp, thanks for the report.

I propose the development of a Spring Session library enabling the migration of sessions between different data storage systems within a live environment, ensuring uninterrupted system operation.

While this is a good idea, I don't think this fits into the framework since the requirements of such migration might vary a lot. However, it would be great if you could create the library as an external project and, if it proves to be a common enough use case we can consider porting it into the framework.

sp1986sp commented 4 months ago

Hi @marcusdacoregio Thank you for your response

We have already published the library in this repo: https://github.com/aakash-24/Spring_Session_Migration If this came out as a common use case please consider it for porting into the framework

Only request is this can you please mention this library link in Spring documentation if somebody encounters this use case they can refer to this library.

Please let me know if this seems fine to you

sp1986sp commented 4 months ago

@marcusdacoregio Please can you update your thoughts for above comment