Memcached for Load Balancing Sessions
A few days ago, I wrote a article to introduce Apache Load balancing. At the end of that article, I mentioned using memcached to accomplish sharing session among load balanced machines. Today, I will get into this.
First you need have memcached installed and running on all load balanced machines. This’s not that hard, you can download memcached and find some usefully information at memecached.org
After that, edit your php.ini file on all load balanced machines,
1. use memcache as session save handler instead of files (default)
;session.save_handler = files session.save_handler = memcache
2. memcached is installed identically on all load balanced servers, you can use one of them to save session data. So the server which hosts session data, you can set session save_path to localhost,
session.save_path="tcp://localhost:11211"
3. On other servers, you can set session save path to the server which hosts session data,
session.save_path="tcp://192.168.8.1:11211"
Here, 192.168.8.1 is the IP of the server, you can also use server name.
Important
- memcache is just a handler of php session in this case, the php still control other activities of session, like session cookie’s lifetime, garbage collection, etc.
- you have to turn off session auto start in order to use memcache.
Advanced
As you see, in the above example, we only use one server to save session data. If that server is down, then there will be no place to save session data. So the better solution will be using multiple servers to save session data.
On server1 (ex: IP: 192.168.8.1), add the following line into php.ini,
session.save_path="tcp://192.168.8.1:11211, tcp://192.168.8.2:11211"
On server2 (ex: IP: 192.168.8.2), add the following line into php.ini,
session.save_path="tcp://192.168.8.1:11211, tcp://192.168.8.2:11211"
If you have more balanced servers, just need to add server IP to save_path and add same line to each server. You would also add the following into your php.ini, in order to turn on failover support.
memcache.hash_strategy = consistent memcache.allow_failover = 1
Important
- The order of save_path list must be the same on each server, however the first one on the list does not have the highest priority, the session data is saved evenly on each member.
- In this case, ”failover” is not transparent. What it support is that when one server goes down, session can be saved to another server, but all the data saved on the ‘dead’ server will completely lost.
- It’s our experience that we didn’t set memcache hash startegy, and seems everything also worked fine. Based on phpslacker (his site is down by this writing), memcached. hash_strategy should be set to consistent to use failover. Regarding php.net, Controls which strategy to use when mapping keys to servers. Set this value to consistent to enable consistent hashing which allows servers to be added or removed from the pool without causing keys to be remapped. Setting this value to standard results in the old strategy being used.
Above is pretty much what we did on our load balanced websites, and it works very well. If it’s critical to you that one server down causes session data lost, you may try to add the following to your php.ini file,
memcache.session_redundancy=2
This makes sessions data being saved to 2 servers. If you have multiple servers, you may change this number accordingly. Because every server actually hosts the same session files all the time, when one memcache daemon failure, the session files will not lost. You may need a recent version of the PHP memcache client to enable memcache redundancy. More details of this method, you can find at this grate post by Tom.
Reference/credit: Tom, phpslacker




[...] I will show you how to use memcached to accomplish share sessions among different machines in the next tutorial. Previous: Follow and Index Next: GoDaddy SSL registration [...]
[...] wrote an article about using PHP memcache as session handler. Recently, I experienced some duplicate key issue and would like to share what I learn from [...]
Great article, but in version 3.0.4 WARNING !!
put memcache.session_redundancy=3
instead of memcache.session_redundancy=2
There is a bug in this version.
AArrrrrgh, time spent to find what didn’t work !