Too many open files when using Memcached for sessions
Ok, I've found the solution thanks to this:
http://php.net/manual/en/memcached.construct.php (see @Tobias comment)
and this https://gist.github.com/K-Phoen/4327229 (see @cmenning comment)
I was using Memcached with a sessions.yml as follows:
session.memcached: class: Memcached arguments: persistent_id: %session_memcached_prefix% calls: - [ addServer, [ %session_memcached_host%, %session_memcached_port% ]]session.handler.memcached: class: Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcachedSessionHandler arguments: [@session.memcached, { prefix: %session_memcached_prefix%, expiretime: %session_memcached_expire% }]
It turns out that if you provide a persistent_id argument for Memcached it would persist the connection between requests. From php.net:
persistent_id
By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.
The problem is that if you use a persistent_id (Which is better for performance), you have to check if there are already servers added before calling addServer
, otherwise it would add a new server & create a new connection, even though it's the same server (it doesn't check for dupes. Something like this:
$instance = new Memcached($persistent_id); // Add server if no connections listed. if (!count($instance->getServerList())) { $instance->addServers($server); }
But for that you'd need to create a wrapper class of Memcached. The easy fix is just to comment out this lines, and avoid using persistent connections for the moment:
#arguments: # persistent_id: %session_memcached_prefix%
This solves the issue, even though it's not ideal.I hope it helps you to save some time and avoid a headache!Thanks everyone for your help.
We had this problem which we fixed by caching metadata in memcache in your config.yml
orm: default_entity_manager: default entity_managers: default: metadata_cache_driver: type: memcache host: localhost port: 11211 instance_class: Memcache query_cache_driver: type: memcache host: localhost port: 11211 instance_class: Memcache
Otherwise if you prefere to cache via apc it also fixes it, i tried both :
metadata_cache_driver: apc query_cache_driver: apc
Hope this helped, for more informations about this issue : https://github.com/FriendsOfSymfony/FOSUserBundle/issues/1062#issuecomment-29473883