php session in Memcached

http://www.ducea.com/2009/06/02/php-sessions-in-memcached/

http://allurcode.com/2010/03/16/php-session-handling-with-memcache/

The moment a PHP application grows to run on more servers, normally people will see problems caused byPHP sessions. If the application is not persistent you are lucky and don’t care about this, but if not you will quickly see this regardless of how good the load balancer you use is handling stickiness (sending the users to the same real server), this will slowly become a major issue. There are various solutions that can be used to store PHP sessions in ashared location, but I want to present today one solution that is very simple to implement, yet very efficient and on the long term better suited than using a database backend for this: usingmemcache to store the sessions.

The pecl memcache php extension has supported for a long time the memcache session.save_handler, but with the release3.0.x (still in beta at this time) this brings in a set of interestingfeatures for us:- UDP support- Binary protocol support- Non-blocking IO using select()- Key and session redundancy (values are written to N mirrors)- Improved error reporting and failover handling

Installing the php memcache module is very simple and can be done either by using distribution repositories (the version we want to use 3.0.x will probably not be available) or by using pecl or manual compilation:

Using pecl:

1
pecl install memcache-3.0.4

or manually:

1
2
3
4
5
6
7
wget http://pecl.php.net/get/memcache-3.0.4.tgz
tar xvfz memcache-3.0.4.tgz
cd memcache-3.0.4
phpize
./configure
make
make install

Finally, we need to activate the module in php.ini. I normally prefer to create a new file for thismemcache.ini inside the include directory of the php build (for ex. in debian this is under/etc/php5/conf.d/memcache.ini) like this:

1
2
3
4
extension=memcache.so
memcache.allow_failover = 1
memcache.redundancy = 1
memcache.session_redundancy = 2

To use memcached to store the php sessions we will have to edit php.ini and replace the default file handler settings with something like:

1
2
3
4
; Use memcache as a session handler
session.save_handler = memcache
; Use a comma separated list of server urls to use for storage:
session.save_path="udp://<memcache_server>:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

or if we don’t want to use this serverwide, we can just define it inside a php block like this:

1
2
3
$session_save_path = "tcp://<memcache_server1>:11211?persistent=1&weight=1&timeout=1&retry_interval=15, tcp://<memcache_server2>:11211";
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', $session_save_path);

Note: as you can see I used in the above example tcp and udp also. Please be sure that your memcached server has udp support enabled if you want to use that. Also ensure that the web server can connect to the memcache server/port (use proper firewall rules to allow this) in order for this to work.

After restarting apache, it will start using memcache to store the php sessions. If redundancy is needed (why not?) we will probably want to use the internal php memcache support to save the sessions to more servers, or if you prefer you can use an external solution to replicate the memcached server data - repcached.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值