Managing connections via datastax/php-driver

403 Views Asked by At

How do I manage the total number of connections with the datastax/php-driver?

We're running into an issue with excessive TCP connections, and we suspect it's related to how this driver works.

We've moved off YACassandra PDO driver onto this one. One of the biggest issues I'm discovering is the connection pool will connect to all servers within a cluster for each HTTP thread.

We have 4 boxes in our cluster. That's 4 open persistent connections, per Apache child. I suspect this is a culprit to our troubles.

Historically, we've used YACassandra PDO, which only used 1 connection per thread.

How do we optimize this? Is there anyway to stop datastax/php-drive from doing cluster discovery?

1

There are 1 best solutions below

4
Fero On

The driver will automatically discover nodes in the cluster and based on the load balancing policy establish connections to each node when the session (connection) is established. To limit the connection to a particular host you can utilize the whitelist policy; however it is not recommended as this defeats the benefits or routing requests in the event of down/unavailable hosts. The whitelist policy has its place, but mainly for testing purposes in my opinion.

Another issue that might be occurring is with forking. The problem is all the children and the parent processes share the same underlying sockets and there isn't a portable way for the php-driver to handle this. Here is an example of how you can handle this in your application code when forking:

$cluster = Cassandra::cluster()->build();

// fork!

if ($pid) { // parent process
    $session = $cluster->connect();

    // do parent stuff
} else { // child process
    $session = $cluster->connect();

    // do child stuff
}