CakeSession::_startSession - Slow on Elasticache CakeSession::_startSession - Slow on Elasticache php php

CakeSession::_startSession - Slow on Elasticache


This issue appears to have been caused by session locking, something I wasn't even aware existed.

This article explains how and why Session Locking exists:https://ma.ttias.be/php-session-locking-prevent-sessions-blocking-in-requests/

What's important is that memcached has session locking turned on by default.

In our case, we don't use Sessions for much other than Authentication, our application doesn't use the session information for storing User State (like a shopping cart would), so we simply disabled session locking with the php.ini setting:

memcached.sess_locking = 0

Since making this change, we've seeing a huge improvement in response times (~200ms average to ~160). This is especially noticeable on AJAX-heavy pages which load a lot of data concurrently. Previously it seems these requests were being loaded sequentially however they're now all serviced simultaneously, the difference in speed is incredible.

While there are likely some edge cases we'll uncover over the coming weeks/months as a result of turning off session locking, this appears to be the cause of the issue, and this change seems to have stopped the problem from occurring.


You need to debug in decoupled way to find out which layer is causing problems.

It can be Cake, AWS infrastructure, network latency...

Run this small PHP script and tell us the time it took.

// memcache$m = microtime( true );$memcache_obj = new Memcache;$memcache_obj->connect('myhost.cache.amazonaws.com', 11211);printf('%.5f', microtime( true ) - $m) ;// memcached.$time = microtime( true );$m = new Memcached();$m->addServer('<elasticache node endpoint>', 11211);$m->set('foo', 100);var_dump($m->get('foo'));printf('%.5f', microtime( true ) - $time) ;

If time is OK, the problem will be Cake.

However being honest here, I fairly certain the problem is ElastiCache Cluster.

Try to point to and end-point of a node and not the end-point of ElastiCache Cluster and let me know how ti goes.


We had similar problem of site becoming slow after moving sessions to Memcached on AWS (EC2 and Elasticache/Memcached). Following changes fixed the problem.

php.ini - session.lazy_write = Offmemcached.ini - memcached.sess_locking = Off

Now site is working fine, with expected speed.

But I am wondering if there is any adverse effects of turning off these settings?