Nginx + Memcached Load balancing for session sharing

Source: Internet
Author: User
Tags memcached nginx load balancing

Session sharing

We are doing site testing, usually need to save some basic user information, such as login will use the session, when the use of Nginx load balancing, when users browse the site will be assigned to a different server, at this time if the session after login on different servers, A remember the user's login status, but the next time the user request was assigned to b what to do? It is not possible for users to log in again. So to achieve session sharing.

Method

1.ip_hash, this is the Nginx polling method, when the user logs on to the site, will always be on this server there is no session problem

Advantages: no processing is required;

Cons: When a server accesses a large amount of traffic, load balancing is the equivalent of useless

2. Using memcached or Redis cache mode

3. Using database storage, when a server has a large amount of access, this will be a big pressure on the data

4. Asynchronous storage with memcached or Redis + db;

Nginx + Memcached Implementation of the session sharing load balancing, I used the second method;

Own a server, Nginx and memcached in a server;

In the server I deployed the same two sites, the code is the same, one port is 8080, the other is 8090;

1    Private Static ReadOnlyImemcachedmanager _memcachedmanager =NewMemcachedmanager ();2 3          Publicactionresult Index ()4         {5Random rd =NewRandom ();6session["Session"] = Rd. Next (1,100000);7 8             if(!_memcachedmanager.isexit ("Session"))9             {Ten_memcachedmanager.setvalue ("Session", session["Session"]);  One             } A  -Viewbag.message ="the current site is: 8080;session:"+ _memcachedmanager.getvalue<string> ("Session") +""; -  the             returnView (); -}

Operation Result:

Achieve my goal, to achieve a balanced load at the same time session sharing purposes.

Nginx In addition to the session sharing problems, there is a shared resource issues, such as uploading files, which requires load balancing a problem to solve;

Summarize

Whether it is installed Nginx or memcached, in this process, and did not encounter a deep pit, of course, memcached on the internet there are many examples, some basic installation can be quickly realized, deployed relatively simple after all is just a demo, I do not know what will be in the actual project, there will be a great difference;

This is just a place to study, of course, there are more things to study;

Share

 

Nginx + Memcached Load balancing for session sharing

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.