Difficulty using memcached to store moodle session

Difficulty using memcached to store moodle session

by Daniel Tran -
Number of replies: 6

Hi,

I'm running moodle 3.1.3 with PHP 7 & MariaDB 5.5.x.

I setup an external memcached server to store moodle sessions.

The following $CFG were added to my config.php as:

$CFG->session_handler_class = '\core\session\memcached'; 

$CFG->session_memcached_save_path = 'external.memcached.server.IP:11211'; 

$CFG->session_memcached_prefix = 'memc.sess.key.'; 

$CFG->session_memcached_acquire_lock_timeout = 120; 

$CFG->session_memcached_lock_expire = 7200;

Upon login, i can see session file is not being written to /moodledata/sessions.  However, within a few seconds after login, I was logged out.  Am I missing some additional setup somewhere else ?.

Average of ratings: -
In reply to Daniel Tran

Re: Difficulty using memcached to store moodle session

by James McLean -

When you use Memcached sessions, no session file will be written to disk. There is however a record written to mdl_sessions to track the existence of the IDs, and the session data itself is written to Memcached.

If you're using Memcached for MUC also, Moodle has a bug/feature where every 15 minutes the MUC is cleared - if you're using the same memcached instance for your sessions, they're cleared too.

We run secondary instances of the Memcached service on 11212, and use that for sessions. That gets around the issue.

In reply to James McLean

Re: Difficulty using memcached to store moodle session

by Daniel Tran -
Thanks James. For MUC, i have an instance of memcached running on the webserver itself. For sessions, I point it to a dedicated memcached server running on a different machine. I've tried everything and it is not working right. I login, navigate around and would randomly get logged-out and prompted to login again because session has timed out.


Is there a way I can verify the session file is written to memcached?


In reply to Daniel Tran

Re: Difficulty using memcached to store moodle session

by Jörg Schulz -

Hi, 

1. Verify that you have the right syntax in your config.php 

$CFG->session_memcached_save_path = 'tcp://<MEMCACHED1>:11211,tcp://<MEMCACHED2>:11211';

2 .Play around with the session_memcached_acquire_lock_timeout = <default  120>;   /But we need to raise to 240 !!

3. Verify that you are not using the "session_memcached_prefix (memc.sess.key) instead maybe like < memc.prod.sess.key> " for other applications on your memcache server otherwise you can get those problems you descriped. 

4. Take a look on 

  • Site administration Plugins Caching Configuration

Do you have configured "store instances" ? 

If not , you should have at least one Store instance with the memcache plugin .

 

/joerg



In reply to Jörg Schulz

Re: Difficulty using memcached to store moodle session

by Daniel Tran -

Thank you  Joerg.

I will verify as you suggested.

Regarding the Site Administration -> Plugins -> Caching -> Configuration, I do have an instance that is being used for MUC.  Are you suggesting I should create another one for moodle sessions ?.  I thought moodle sessions don't go through the MUC's store instance.

In reply to Daniel Tran

Re: Difficulty using memcached to store moodle session

by Jörg Schulz -

Hi, 

Hm, good question about "sessions" don´t go through MUC. 

As i remember when we installed Moodle 2.9 there was a bug that we fixed by creating a new instance. 

After this we updated to 3.x , since we never had problems . 

Did you take a look on PHP and OPcache module. We don´t use php 7 yet just 5.5 - 5.6 . 

Did you use "varnish" och other accelerators on your site ? 

Check you iptables explicit to your memcache server . 

And btw , we use memcache and not memcache(d) ! 

/J

In reply to Daniel Tran

Re: Difficulty using memcached to store moodle session

by Daniel Tran -

Thank you all for your suggestions.

I finally track it down to some settings in my /etc/php.d/50-memcached.ini file.

They were set as:

memcached.sess_lock_wait_min = 0;

memcached.sess_lock_wait_max = 0;

I had to comment them out and everything worked as it should.

From phpinfo(), I can see the "local value" for those settings in memcached are now:

memcached.sess_lock_wait_min = 1000

memcached.sess_lock_wait_max =  120000

The "master" value are 1000 and 2000 respectively.

I'm not sure if i should adjust them or just leave them as is.