Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Persistent connections exhaust connections to memcached server #100

Closed
OndraM opened this issue Sep 5, 2016 · 5 comments
Closed

Persistent connections exhaust connections to memcached server #100

OndraM opened this issue Sep 5, 2016 · 5 comments
Assignees

Comments

@OndraM
Copy link

OndraM commented Sep 5, 2016

Hi,
when persistent connection to memcached is used, it behaves incorrectly and creates more and more persistent connections to memcached, which may lead to memcache connection limit exhausting in a short while.

The problem was introduced in #83 with version 1.3.0.

The issue is in MemcachedDefinition:

        if (isset($config['persistent_id']) === true) {
            $connDef->addrgument($config['persistent_id']);
        }

        foreach ($config['servers'] as $host => $server) {
            $connDef->addMethodCall('addServer', array($host, $server['port']));
        }

The addMethodCall('addServer', ...) adds new server(s) to the connection pool. This is not a problem without the persistent connection, because the pool is created only for the one request and the connection is closed shortly afterwards.

However, when the connection is created as persistent, servers are added to already existing pool, so after a while, you ends with many (same) memcached servers in the connection pool and new persistent connection being created to them every time new server from the pool is chosed. As the connections are created as persistent and new servers are added every request, it easily reaches almost any connection limit the memcached server has.

The addServer() should be IMHO called only if the Memcached instance does not contain the server setup yet (eg. count($memcached->getServerList()) is 0).

@alcaeus
Copy link
Member

alcaeus commented Nov 9, 2018

The addServer() should be IMHO called only if the Memcached instance does not contain the server setup yet.

This isn't all that easy, since we create the service and the calls are only executed when the service is instantiated. I'm not sure why the \Memcached class allows adding a server to the pool multiple times, this definitely sounds like a bug in the extension. I'd expect the following code to only produce two connections, not three:

$connection = new \Memcached();
$connection->addServer('localhost:123');
$connection->addServer('localhost:234');
$conncetion->addServer('localhost:123');

The only way we could fix this is by extending the \Memcached class and adding that logic ourselves, which I'd only consider an option if this is indeed confirmed to be wanted behaviour by the memcached extension.

@fredericch
Copy link

fredericch commented Nov 17, 2018

Actually it is not a bug. When you have persistent connection, you should only add your servers once. See the first user comment here: http://php.net/manual/en/memcached.construct.php#93536

@OndraM
Copy link
Author

OndraM commented Nov 19, 2018

True, I also believe this is not a bug of the memcached extension, but incorrect behavior of the Cache Bundle.

See also other comments in the manual, like this one:

(...) This is why you do not want to call ->addServers() every time your script runs, because ->addServers() does not check for dups and will add hundreds or thousands of connections to the memcached daemon proc from the Apache proc(s).

It is also documented behavior in the manual:

The same server may appear multiple times in the server pool, because no duplication checks are made.

@alcaeus
Copy link
Member

alcaeus commented Dec 4, 2018

Thanks for confirming this. I'll see if I can come up with a workaround.

@alcaeus alcaeus self-assigned this Dec 4, 2018
@alcaeus alcaeus added the Bug label Dec 4, 2018
@alcaeus alcaeus added Won't Fix and removed Bug labels May 29, 2019
@alcaeus
Copy link
Member

alcaeus commented May 29, 2019

Closing here for the reason stated in #156.

@alcaeus alcaeus closed this as completed May 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants