Issues about session sharing in load balancing

Source: Internet
Author: User

--------------------------------------------------------

When doing load balancing the access page will distribute the access to different servers, the session is present on the server side, if the first access is distributed to a server, then the session will be saved to a server, Load balancer will be distributed to B server on re-access so the session information of the first visit will not get the previous session information. There are several ways to resolve this:

--------------------------------------------------------

(a) When using Nginx load Balancing can be configured Ip_hash to solve, Ip_hash technology can be a request for an IP to direct to the same back-end application server.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/87/3B/wKioL1fYo2fhbcRtAAAc-vlnDzE318.png "title=" 1.png " alt= "Wkiol1fyo2fhbcrtaaac-vlndze318.png"/>

This allows the same IP request to be distributed to the same back-end server.

Cons: If the server is down, then the user mapped to this server is depressed.

(ii) Synchronization session with database

This method is not advisable, mainly to put the session into the database, which will increase the burden of the database. For the time being, don't repeat ...

(c) The principle of synchronizing session data with cookies is as follows

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/87/3D/wKiom1fYqVnizzHSAACeL-ZPlho895.png "title=" Cookie sync session.png "alt=" Wkiom1fyqvnizzhsaacel-zplho895.png "/>

The code is as follows:

A server

<?php
Session_Start ();
if (isset ($_session[' username ')) {
Echo ' OK you can go next ';
}else if (isset ($_cookie[' username ')) {
Echo ' session is not in this server but the cookie is exist ';
$_session[' username ']=$_cookie[' username '];
}else{
Echo ' Cookie and session does\ ' t in this server ';
$_session[' username ']= ' Lampol ';
Setcookie (' username ', ' Lampol ', Time () +30*24*60*60);
}
Echo ' </br> ';
Echo ' This is A server ';
Echo ' </br> ';
Var_dump ($_session);

b Server

<?php
Session_Start ();
if (isset ($_session[' username ')) {
Echo ' OK you can go next ';
}else if (isset ($_cookie[' username ')) {
Echo ' session is not in this server but the cookie is exist ';
$_session[' username ']=$_cookie[' username '];
}else{
Echo ' Cookie and session does\ ' t in this server ';
$_session[' username ']= ' Lampol ';
Setcookie (' username ', ' Lampol ', Time () +30*24*60*60);
}
Echo ' </br> ';
Echo ' This is B server ';
Echo ' </br> ';
Var_dump ($_session);

And then start accessing

The first visit to Server B does not have session and cookie data and will then be saved to session and local cookie

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/87/3E/wKiom1fYs4HzktDJAAA7Jj6SYaI302.png "title=" 11.png "alt=" Wkiom1fys4hzktdjaaa7jj6syai302.png "/>

Refresh the second access distribution to the a server session does not exist but the cookie is locally present and the local cookie is synchronized to the B server's cookie

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/87/3B/wKioL1fYs-zjItdvAAA6z4R9wRU839.png "title=" 22.png "alt=" Wkiol1fys-zjitdvaaa6z4r9wru839.png "/>

Cons If you disable cookies then you're screwed.

(iv) Memcache or Redis storage session (take Memcache as an example)

Install the memcache and the client and the server, and then

<?php

Ini_set ("Session.save_handler", "memcache");
Ini_set ("Session.save_path", "192.168.1.142:11211");

The above configuration can be configured again in php.ini
Session_Start ();
$_session[' username ']= ' Lampol ';

This saves the session information in the Memache (Redis's same)

And then in the run

<?php
Ini_set ("Session.save_handler", "memcache");
Ini_set ("Session.save_path", "192.168.1.142:11211");
Session_Start ();
$mem = new Memcache;

$mem->connect (' 192.168.1.142 ', 11211);

$id =session_id ();

echo $mem->get ($id);

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/87/44/wKiom1fZEw_yBpOMAAAmiQJQIEI845.png "title=" 123. PNG "alt=" Wkiom1fzew_ybpomaaamiqjqiei845.png "/>

Gets the data in the memcache of the session

In fact, if you consider the session sharing problem, there is absolutely no need to save the session directly to the data stored in the memcache can

Distributed directly to the same Memcache server can be, in addition memcache can master from, later in the article to continue to explore this issue.


This article is from the "Ideal for Life" blog, please be sure to keep this source http://lampol.blog.51cto.com/11662628/1852600

Issues about session sharing in load balancing

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.