Slow PHP Session


Slow PHP Session



A basic page page with just session_start(); loads just fine, but once I've set something, for example $_SESSION['pet']="dog";, the page load time is around 5 seconds.


session_start();


$_SESSION['pet']="dog";



I'm using AWS's memcached server and the connection time to it from the EC2 instance is really fast. I'm not sure where the slow down is coming from.



The session.save_handler is set to memcached and session.save_path is set to xxx.cfg.use1.cache.amazonaws.com:11211



phpinfo also displays Registered save handlers as files user memcache memcached



EDIT :
I uploaded test files to demonstrate the issue. The first file is simply session_start(); print_r($_SESSION); (http://rr915webapi.us-east-1.elasticbeanstalk.com/session.php). The second file is session_start();$_SESSION['pet']="dog";$_SESSION['name']="bob";(http://rr915webapi.us-east-1.elasticbeanstalk.com/session-set.php). After you load the second file, you can see the first takes a while longer to load than initially did.


session_start(); print_r($_SESSION);


session_start();$_SESSION['pet']="dog";$_SESSION['name']="bob";





Is the session stored in memcache or on disk?
– Lawrence Cherone
Jul 2 at 19:46





I've set the session.save_handler to memcached and session.save_path to the ElastiCache instance - Under session it lists Registered save handlers: files user memcache memcached
– austinhollis
Jul 2 at 19:50






Is there a way to check if it's truely using the memcached server?
– austinhollis
Jul 2 at 20:08





@austinhollis php.net/manual/en/function.session-save-path.php you can also just dump <?php phpinfo(); into a file and it will dump your config to a readable page.
– Neil Masters
Jul 2 at 20:15






@NeilMasters I did do that and didn't know if the order of Registered save handlers meant it was using something other than the session.save_path that is specified
– austinhollis
Jul 2 at 20:20




2 Answers
2



Some possibilities :



if your PHP server running your PHP code and your memcached server / cfg.use1.cache.amazonaws.com are hosted on different regions, it can explain all this time...



there seems to be a a bug in libmemcached 1.0.16...if you update to 1.0.18, will fix the problem, see https://github.com/iuscommunity/wishlist/issues/143 comments and https://bugs.launchpad.net/libmemcached/+bug/1589344





They're both in us-east-1, but they are in different zones. Would that still make a difference?
– austinhollis
Jul 3 at 1:40





I just moved the EC2 instance to us-east-1b to see if that would make a difference, but I'm still getting the same 5 seconds until the first byte
– austinhollis
Jul 3 at 2:08





i've updated my answer with libmemcached 1.0.16 issue...
– Pierre
Jul 3 at 5:39



By setting the following in the PHP ini file, the response time was reduced down to milliseconds.


session.lazy_write = 0
memcached.sess_locking = Off






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages