Session must hear out the development of synchronized, but as not been engaged in practice is another matter.
Such as Sina, Sohu these portals, more than just tens of thousands of daily visitors, but no one site why not crash? But Zan a one of a small station on the 10,000 daily visitors dare, nice bar, the server will definitely storm out. Why is the portal will be fine, because they do cluster and do load balancing, caching done, do a static build, do the cache; but they do after these technologies, Session on how to synchronize it?
This paper to analysis, several commonly used synchronization Session:
1, using the database synchronization session
Doing synchronized multi-server session, if you must use this method if there are two methods:
1, with a low-end computer store build a database of specialized web server session, or, to the special database built in file server, users access web server, the database will be to check what this special session of the situation in order to achieve session synchronization purposes.
2, this approach is to store session tables and other database tables put together, if the Mysql has done a cluster of words, each node must have this form, mysql, and this session's data table to the table real-time synchronization .
Note: Use the database to synchronize the session, will increase the burden on the database, the database would have a place that is prone to bottlenecks, if the session is also inside the database, much worse. The above two methods, first method is better to put the table session open to independent, to reduce the real burden of the database
2: The synchronization session cookie
session is a file stored on the server side of the situation, cookie is a file there is the client situation, how to synchronize it? Method is simple to generate the session the user to access the page into the cookie which is to cookie for the transfer stations. You visit the web server A, create a session cookie put it inside, you are assigned access to the web server B, this time, web server B first determines the server has no such session, and if not, to see the client in the cookie which have no such session, if no, explain session really does not exist, if the cookie inside, put inside the cookie to the web server synchronization sessoin B, so that you can achieve the synchronization of the session.
Note: This method implementation is simple and convenient, it would not increase the burden on the database, but if the client lost the cookie ban, then the session will be impossible to synchronize, so that site will cause losses; cookie security is not high, although it has added a secret, but can still forged.
3: Using memcache synchronization session
memcache can do distributed, without this function, he does not do the session can be used simultaneously. He can be web servers memory together, as a "memory pool", no matter which server generated sessoin can put this "memory pool", the other can be used.
Advantages: in this way to synchronize the session, will not increase the burden on the database, and secure than using cookie greatly improved, the session into memory inside, than to be read from the file much faster.
Disadvantages: memcache memory is divided into many kinds of specifications of the storage block, a block will have size, this approach also determines, memcache can not use the memory will result in memory fragmentation, memory block, if less, will produce memory overflow.
The above three methods are feasible first method, the speed that most affect the system, not recommended;
The second method, the effect is good, but as the existence of potential safety problems;
The third method, personally feel that the third method is the best recommendation we use
Usually chosen as the portal to achieve the third method, the benefits do not have to say, but the difficulty is in Memcache configuration, if it is bigger centers, or recommend the use of a third method, short-term pay in exchange for long-term stability , why not?