📄 resin-clustering.xtp
字号:
traffic for <var>2:load</var> and <var>3:store</var>. Smart load-balancerscan avoid the network traffic of <var>2</var> and <var>3</var>.</p><figure src="same_srun.gif"/><p>Host C decodes the session id, <var>caaMbyA</var>. Since it ownsthe session, Host C gives the session to the servlet with no workand no network traffic. For a read-only request, there's zerooverhead for cluster sessions. So even a semi-intelligent loadbalancer will gain a performance advantage. Normal browsers will havezero overhead, and bogus AOL browsers will have the non-stickysession overhead.</p><p>A session write saves the new serialized session to disk(<var>2:save</var>) and to Host D (<var>3:backup</var>).<var>always-save-session</var> will determine if Resin can take advantageof read-only sessions or must save the session on each request.</p></s2><s2 title="Disk copy"><p>Resin stores a disk copy of the session information, in the locationspecified by the <var>path</var>. The disk copy serves two purposes. The first isthat it allows Resin to keep session information for a large number ofsessions. An efficient memory cache keeps the most active sessions in memoryand the disk holds all of the sessions without requiring large amounts ofmemory. The second purpose of the disk copy is that the sessions are recoveredfrom disk when the server is restarted.</p></s2><s2 title="Failover"><p>Since the session always has a current copy on two servers, the loadbalancer can direct requests to the next server in the ring. Thebackup server is always ready to take control. The failover willsucceed even for dumb load balancers, as in the non-sticky-sessioncase, because the srun hosts will use the backup as the new owningserver.</p><p>In the example, either Host C or Host D can stop andthe sessions will use the backup. Of course, the failover will workfor scheduled downtime as well as server crashes. A site couldupgrade one server at a time with no observable downtime.</p></s2><s2 title="Recovery"><p>When Host C restarts, possibly with an upgraded version of Resin,it needs to use the most up-to-date version of the session; itsfile-saved session will probably be obsolete. When a "new" sessionarrives, Host C loads the saved session from both the file andfrom Host D. It will use the newest session as the currentvalue. Once it's loaded the "new" session, it will remain consistentas if the server had never stopped.</p></s2><s2 title="No Distributed Locking"><p>Resin's cluster sessions does not lock sessions. For browser-basedsessions, only one request will execute at a time. Since browsersessions have no concurrently, there's no need for distributedlocking. However, it's a good idea to be aware of the lack ofdistributed locking.</p></s2></s1><s1 title="<persistent-store>"><p>Configuration for persistent store usesthe <a href="resin-tags.xtp#persistent-store">persistent-store</a> tag.</p></s1><s1 title="File Based"><ul><li>For single-server configurations</li><li>Useful in development when classes change often</li></ul><p>Persistent sessions are configured in the <var>web-app</var>.File-based sessions use <var>file-store</var>.</p><example><web-app xmlns="http://caucho.com/ns/resin"> <session-config> <file-store>WEB-INF/sessions</file-store> </session-config></web-app></example><p>Sessions are stored as files in the <var>file-store</var>directory. When the session changes, the updates will be written tothe file. After Resin loads an Application, it will load the storedsessions.</p><p>File-based persistence is not useful in multi-serverenvironments. Although a network filesystem such as NFS will allow allthe servers to access the same filesystem, it's not designed for thefine-grained access. For example, NFS will cache pages. So if oneserver modifies the page, e.g. a session value, the other servers may not seethe change for several seconds.</p></s1><s1 title="Distributed Sessions"><p>Distributed sessions are intrinsically more complicated than single-serversessions. Single-server session can be implemented as a simple memory-basedHashtable. Distributed sessions must communicate between machines to ensurethe session state remains consistent.</p><p>Load balancing with multiple machines either uses <var>sticky sessions</var> or<var>symmetrical sessions</var>. Sticky sessions put more intelligence on theload balancer, and symmetrical sessions puts more intelligence on the JVMs.The choice of which to use depends on what kind of hardware you have,how many machines you're using and how you use sessions.</p><p>Distributed sessions can use a database as a backing store, or they candistribute the backup among all the servers using TCP.</p><s2 title="Symmetrical Sessions"><p>Symmetrical sessions happen with dumb load balancers like DNSround-robin. A single session may bounce from machine Ato machine B and back to machine B. For JDBC sessions, the symmetricalsession case needs the <var>always-load-session</var> attribute described below.Each request must load the most up-to-date version of the session.</p><p>Distributed sessions in a symmetrical environment are required to makesessions work at all. Otherwise the state will end up spread across the JVMs.However, because each request must update its session information, it isless efficient than sticky sessions.</p></s2><s2 title="Sticky Sessions"><p>Sticky sessions require more intelligence on the load-balancer, butare easier for the JVM. Once a session starts, the load-balancer willalways send it to the same JVM. Resin's load balancing, for example, encodesthe session id as 'aaaXXX' and 'baaXXX'. The 'aaa' session will always goto JVM-a and 'baa' will always go to JVM-b.</p><p>Distributed sessions with a sticky session environment add reliability.If JVM-a goes down, JVM-b can pick up the session without the usernoticing any change. In addition, distributed sticky sessions are moreefficient. The distributor only needs to update sessions when they change.So if you update the session once when the user logs in, the distributedsessions can be very efficient.</p></s2><s2 title="always-load-session"><p>Symmetrical sessions must use the 'always-load-session' flag toupdate each session data on each request. always-load-session is onlyneeded for jdbc-store sessions. tcp-store sessions use a more-sophisticatedprotocol that eliminates the need for always-load-session, so tcp-storeignores the always-load-session flag.</p><p>The <var>always-load-session</var> attribute forces sessions to check the store foreach request. By default, sessions are only loaded from persistentstore when they are created. In a configuration with multiple symmetricweb servers, sessions can be loaded on each request to ensure consistency.</p></s2><s2 title="always-save-session"><p>By default, Resin only saves session data when you add new valuesto the session object, i.e. if the request calls <var>setAttribute</var>.This may be insufficient when storing large objects. For example, if youchange an internal field of a large object, Resin will not automaticallydetect that change and will not save the session object.</p><p>With <var>always-save-session</var> Resin will always write the sessionto the store at the end of each request. Although this is less efficient,it guarantees that updates will get stored in the backup after eachrequest.</p></s2></s1><s1 title="Database Based"><p>Database backed sessions are the easiest to understand. Session datagets serialized and stored in a database. The data is loaded on thenext request.</p><p>For efficiency, the owning JVM keeps a cache of the session value, soit only needs to query the database when the session changes. If another JVMstores a new session value, it will notify the owner of the change sothe owner can update its cache. Because of this notification, the databasestore is cluster-aware.</p><p>In some cases, the database can become a bottleneck.By adding load to an already-loadedsystem, you may harm performance. One way around that bottleneck is to usea small, quick database like MySQL for your session store and save the "BigIron" database like Oracle for your core database needs.</p><p>The database must be specified using a <var><database></var>.The database store will automatically create a <var>session</var> table.</p><p>The JDBC store needs to know about the other servers in the clusterin order to efficiently update them when changes occur to the server.</p><example title="JDBC store"><resin xmlns="http://caucho.com/ns/resin"><cluster id="app-tier"> <server-default> <http port="80"/> </server-default> <server id="app-a" address="192.168.2.10" port="6800"/> <server id="app-b" address="192.168.2.11" port="6800"/> <database jndi-name="jdbc/session"> ... </database> <persistent-store type="jdbc"> <init> <data-source>jdbc/session<data-source> </init> </persistent-store> ... <web-app-default> <session-config> <use-persistent-store/> </session-config> </web-app-default> ...</cluster></resin></example><p>The persistent store is configured in the <server> with<a href="resin-tags.xtp#persistent-store">persistent-store</a>.Each web-app which needs distributed sessions must enablethe persistent store with a<a href="webapp-tags.xtp#session-config">use-persistent-store</a>tag in the session-config.</p><deftable><tr> <td>data-source</td> <td>data source name for the table</td></tr><tr> <td>table-name</td> <td>database table for the session data</td></tr><tr> <td>blob-type</td> <td>database type for a blob</td></tr><tr> <td>max-idle-time</td> <td>cleanup time</td></tr></deftable><example>CREATE TABLE persistent_session ( id VARCHAR(64) NOT NULL, data BLOB, access_time int(11), expire_interval int(11), PRIMARY KEY(id))</example><p>The store is enabled with <use-persistent-store> in the session config.</p><example><web-app xmlns="http://caucho.com/ns/resin"> <session-config> <use-persistent-store/> <always-save-session/> </session-config></web-app></example></s1><s1 title="Cluster Sessions"><p>The distributed cluster stores the sessions across thecluster servers. In some configurations, the cluster storemay be more efficient than the database store, in others the databasestore will be more efficient.</p><p>With cluster sessions, each session has an owning JVM and a backup JVM.The session is always stored in both the owning JVM and the backup JVM.</p><p>The cluster store is configured in the in the <cluster>.It uses the <server> hosts in the <cluster> to distributethe sessions. The session store is enabled in the <session-config>with the <use-persistent-store>.</p><example><resin xmlns="http://caucho.com/ns/resin"> ... <cluster id="app-tier"> <server id="app-a" host="192.168.0.1" port="6802"/> <server id="app-b" host="192.168.0.2" port="6802"/> <persistent-store type="cluster"> <init path="cluster"/> </persistent-store> ... </cluster></resin></example><p>The configuration is enabled in the <var>web-app</var>.</p><example><web-app xmlns="http://caucho.com/ns/resin"> <session-config> <use-persistent-store="true"/> </session-config></web-app></example><p>The <srun> and <srun-backup> hosts are treated as a clusterof hosts. Each host uses the other hosts as a backup. When the sessionchanges, the updates will be sent to the backup host. When the host starts, itlooks up old sessions in the other hosts to update its own version of thepersistent store.</p><example title="Symmetric load-balanced servers"><resin xmlns="http://caucho.com/ns/resin"><cluster id="app-tier"> <server-default> <http port='80'/> </server-default> <server id="app-a" address="192.168.2.10" port="6802"/> <server id="app-b" address="192.168.2.11" port="6803"/> <persistent-store type="cluster"> <init path="cluster"/> </persistent-store> <host id=''> <web-app id=''> <session-config> <use-persistent-store="true"/> </session-config> </web-app> </host></cluster></resin></example></s1><s1 title="See Also"><ul><li><a href="config-sessions.xtp">Distributed Sessions</a></li><li><a href="tcp-sessions.xtp">Distributed Sessions with Cluster Store</a></li><li><a href="cluster-tags.xtp#persistent-store"><persistent-store></a></li></ul></s1> </body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -