Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo php replica connection very slow

This connects with no delay:

$connection = new MongoClient("mongodb://localhost:27017", array("replicaSet" => REPLICASET,'username'=>USER, 'password'=>PASSWORD, 'db'=>DATABASE));

However, this takes up to 10 seconds when I add all the hosts from the replica set. PHP driver (1.6.7)

$connection = new MongoClient("mongodb://".HOST_1.":27017,".HOST_2.":27017,".HOST_3.":27018", array("replicaSet" => REPLICASET,'username'=>USER, 'password'=>PASSWORD, 'db'=>DATABASE));

In the log the delay seems to be at each connection. Here is an excerpt from the log:

[22-Jun-2015 12:04:41 Australia/ACT] PHP Notice:  CON     FINE: Connecting to tcp://ec2-XX-XXX-XXX-XXX.ap-southeast-2.compute.amazonaws.com:27017 (ec2-XX-XXX-XXX-XXX.ap-southeast-2.compute.amazonaws.com:27017;launch;launch/launch/55a850dad20e018890350ece678dc293;15873) with connection timeout: 60.000000 in /home/user/public_html/test.php on line 52
[22-Jun-2015 12:04:47 Australia/ACT] PHP Notice:  CON     INFO: stream_connect: Not establishing SSL for ec2-XX-XXX-XXX-XXX.ap-southeast-2.compute.amazonaws.com:27017 in /home/user/public_html/test.php on line 52

When I ssh into the primary server and try connecting from the terminal its fast as well.

mongo --host "launch/host:27017,ec2-XX-XXX-XXX-XXX.ap-southeast-2.compute.amazonaws.com:27017,host2:27018" --authenticationDatabase launch -u USERNAME -p PASSWORD

Why is PHP taking so long to connect?


Jul 20 Update:

I've updated mongo on my primary server to 2.6.10 but it still takes 5 seconds for my testing page to load. This is how I'm attempting to connect:

error_reporting(E_ALL);
MongoLog::setLevel(MongoLog::ALL);
MongoLog::setModule(MongoLog::ALL);

try {
    $connection = new MongoClient("mongodb://host1:27017,ec2host1:27017,ec2arbiterhost:27017", array("replicaSet" => "setname",'username'=>USER, 'password'=>PASSWORD, 'db'=>DATABASE));

    echo 'connected';

} catch (MongoConnectionException $e) {
  die("Unable to connect to database [code: " . $e->getCode() . "]");
}

Any help would be appreciated.

like image 709
Castles Avatar asked Jun 22 '15 02:06

Castles


1 Answers

I do not see any hardware information in your question and in terms of performance in my attending mongo Q&A and talking with engineers working for mongo I understand that checking and choosing the correct hardware and physical location of hardware is a big factor in speeding things up.

1)Are the replicas in the same datacenter? And even if they are in the same datacenter is communication between the servers fast in terms of measuring the time for a ping and checking of the traceroute between each of the replicas (ie 1 to 2, 1 to 3, 2 to 3) there are a lot of transactions getting passed between the replicas to keep themselves up to date and if they are in different data centers that could be a factor. So a type of solution would be to ensure that the replicas are close enough and sometimes even in the same datacenter. Though there is something to be said for having replica in different datacenters for Partition tolerance. 2) ram and cpu of the hardware makes a difference perhaps increasing those would make a difference as well using SSD drives speeds things up in general.

Also, and as a side note, a type of "workaround" to the issue you are seeing is to pool connections to mongodb so that the connections are readily available but I realize that is not what you are asking, you are asking about the connection itself and why it is slow!

like image 141
aruuuuu Avatar answered Oct 22 '22 07:10

aruuuuu