Business Background
In web applications, in order to cope with large-scale access, cluster deployment of applications must be implemented. To implement cluster deployment, it is necessary to implement a session sharing mechanism to unify sessions between multiple application servers. Most mainstream web servers such as weblogic and tomcat adopt session replication and implement session sharing. But the problem is obvious: as the number of nodes increases, the performance loss caused by session replication will be increasingly worse. Especially when larger objects are stored in the session and the objects change quickly, the performance decrease is more significant. This limits the horizontal scaling of web applications. Another idea for session sharing is to centralize session management. The first thing that comes to mind is to use a database to centrally store sessions. However, the files needed to be stored in a database are of a great size, which will inevitably increase the burden on the database system. The data structure in the session is of the MAP type, which naturally matches the MAP data structure of the distributed cache.
Business Requirements
l Good scalability, web server with more than 100 nodes;
l The performance of session operations and the capacity can be expanded horizontally;
l Sharing session data supports a timeout mechanism.
Analysis
A single eSurfing Cloud DCS group supports 1000 web servers × connection pool connections with more than 100 threads. The back-end Redis storage node supports scaling to 1000 Redis nodes. The maximum storage capacity can support 500TB of data, and it supports setting timeout time by key.
Implementation idea: general implementation idea, independent of web container selection.
1. Use the filter to intercept sessionId in the cookie, construct a new HttpServletRequestWrapper object with sessionId, and pass it to subsequent applications;
2. Inherit and reconstruct HttpServletRequestWrapper and the HttpSessionWrapper class to cover the original methods related to session access, all implemented through the SessionService class;
3. Use HttpSessionSidWrapper to implement the httpsession interface to wrap the attribute access operations of the container's session;
4. Session object map<String, object>, the cache is map<String, byte[]>, the object in the session needs to be serialized into byte[].