TOMCAT7 session sharing based on Redis

Source: Internet
Author: User
Tags failover memcached redis serialization

At present, to enable the web to adapt to large-scale access, the application of the cluster deployment needs to be implemented. Cluster is the most effective solution is load balancing, and the implementation of load-balanced users are likely to be assigned to a fixed server, so we first need to solve the unity of the session to ensure that regardless of the user's request is forwarded to which server can guarantee the normal use of users, That is, the sharing mechanism of session needs to be implemented.

Under the cluster system to achieve session unification has the following several schemes:

(1) Session replication sharing between application servers (such as Tomcat with session sharing)

(2) session sharing based on cache DB caching


Session replication sharing between application servers (Tomcat's own feature)

Session replication sharing, mainly refers to the cluster environment, multiple application server synchronization between sessions, so that the session to maintain consistent, external transparency. If one of the server fails, according to the principle of load balancing, the Web server (Apache/nginx) will traverse the search for available nodes, distribution requests, because the session has been synchronized, it can ensure that the user's session information will not be lost.


The disadvantage of this scenario is that the technology is complex and must be done between the same middleware (e.g., tomcat-tomcat). The performance penalty for session replication can increase rapidly. Especially when large objects are kept in the session, and the objects change faster, the performance decreases significantly. This feature limits the level of Web application expansion. Session content serialization (serialize) consumes system performance. The session content is synchronized to the member through the broadcast, will cause the network traffic bottleneck, even is the intranet bottleneck.

Session sharing based on Memcached caching

Even with CACHEDB access to session information, the application server accepts new requests to save session information in cache DB, and when the application server fails, the Web server (Apache/nginx) traverses the search for available nodes, distributing requests, When the application server discovers that session is not native memory, it looks in cache db and, if found, replicates to the local machine, which enables session sharing and high availability.

There are open source MSM to address the session sharing between Tomcat: Memcached_session_manager (MSM)

http://code.google.com/p/memcached-session-manager/
A highly available Tomcat session sharing solution, in addition to the fast reading of session information from native memory (for sticky sessions only), and the use of memcached access sessions for high availability. feature support TOMCAT6, TOMCAT7 support viscous, non-viscous session no single point of failure to handle tomcat failover handles memcached failover plug-in session serialization allows sessions to be saved asynchronously. To increase the response speed the session is written back to memcached JMX Management & Monitoring

The shortcomings of the scheme: Memcache supported data structure compared to a single memcache memory must be large enough, otherwise there will be a user session from the cache to be purged from the cache to periodically refresh the cached server failure, the memory of the Memcache data will be lost

In order to solve the problem based on Memcache, the following solution is proposed:

Session sharing based on Redis caching

Combined with the above analysis, the Redis is responsible for storing the session data, and the session manager we implement will be responsible for the session lifecycle management. Using Redis's own key expiration time wit, we no longer need to refresh regularly and do additional processing.

TOMCAT8 is not supported until 2015-09-20, open source git address: Https://github.com/jcoleman/tomcat-redis-session-manager

Because we use Redis to store the session, the premise is that Redis has been installed and configured to complete. (This article does not perform Redis installation configuration instructions)

Use steps:

1, copy Tomcat-redis-session-manager-1.2.jar, Jedis-2.6.1.jar, Commons-pool2-2.2.jar three jar packages to tomcat7/lib.

2. Add the following in Tomcat's Conf/context.xml file (or added in Server.xml's context block):[HTML] View plain copy <valve classname= "Com.radiadesign.catalina.session.RedisSessionHandlerValve"  />    <manager classname= "Com.radiadesign.catalina.session.RedisSessionManager"                        host= "localhost"  <!--  Optional, default is "localhost"  -->                       port= "6379"   <!--  Optional, default is   "6379"  -->                       database= "0"  <!--  Optional, default is   "0"  -->                       maxinactiveinterval=  <!--  Optional, default is   "-->"   (unit: seconds)  />   3, HeavyOpen Tomcat7 and view session_id in Redis, as follows: [HTML]View plain copy 127.0.0.1:6379> keys wj* 1) "WJSESSIONID:6D5B0E0FD89E3A170B8BC5B8C112D3FD" 2) "wjsessionid:9546b26d 78c99e8f0bf785535e319271 "3" wjsessionid:839a35cfe17e900a81f50d629c104d2f "4" Wjsessionid : 1c287c797cf00c82bbbf37a617a3b55c "5" wjsessionid:fa2822c5021139641760754242f73393 "6)" WJSESSIONID: E904369e5b24d39b4e25515d50650ea6 "127.0.0.1:6379>


Here's a special note:

Git open source project is to store SessionID as key directly in Redis, as follows: [HTML] view plain copy 1) "6D5B0E0FD89E3A170B8BC5B8C112D3FD" 2) "95 46b26d78c99e8f0bf785535e319271 "3" 839a35cfe17e900a81f50d629c104d2f "4" "1c287c797cf00c82bbbf37a617a3b55c" 5) "FA28 22c5021139641760754242f73393 "6" "E904369e5b24d39b4e25515d50650ea6"

I made a little change to the project.

The reasons for the modification include the following:

1, the project also has the use Redis to do other data storage, directly uses such key to store in the Redis, intuitively cannot distinguish which key is uses for the session sharing.

2, the project includes several services (web, WAP, CMS), each service needs to do load balanced session sharing, so it can not distinguish which sessions belong to which service.

3, it is difficult to count the number of online users per service.


I have a different sessioncookiename for each of my services in the cluster deployment (web = Wjsessionid, wap = Mjsessionid, CMS = Cmsjsessionid)

Do not know how to configure, here briefly, directly in the tomcat7/conf/server.xml of the bottom of the context to add Sessioncookiename configuration can: [HTML] view plain copy <context docbase= "F:\workspace\web" path= "" reloadable= "false" Sessioncookiename= "Wjsessionid"/>


My modified items have been shared to: http://download.csdn.net/detail/catoop/9122857 (Project is a MAVEN project)

Where Tomcat-redis-session-manager\target\tomcat-redis-session-manager-1.2.jar can be used directly.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.